image.jpg byte array to writeablebitmap through base64 WP8 - arrays

I'm trying to convert a .jpg or .png file to a writeable bitmap. I obtain the image in another source and convert to a base64 encoding. After removing the packaging, I have a width, height, and base64 data. I then use:
var base64 = dataurl.Substring(dataurl.IndexOf("base64,") + 7);
binData = Convert.FromBase64String(base64);
This gives me the binary data of my image. The problem really comes in that I'm writing this for windows phone 8, so I'm limited in what libraries and methods I can use. The obvious choice is:
using (var stream = new MemoryStream(binData, 0, binDta.Length, true, true))
{
var wbp = new WriteableBitmap(1,1).LoadJpeg(stream);
}
but I'm getting a System.ArgumentException from the WriteableBitmap library. Any ideas that work on Windows Phone 8?

You should make sure the base64 decoded data is really a valid format like JPG and PNG.
Then I'd recommend you try the WriteableBitmapEx FromStream method:
var wbp = new WriteableBitmap(1, 1).FromStream(stream);

Related

Save image from base64 encoded string in SQL Server database as a png file

I have a table column with string data beginning with 0xFFD8FFE000.... and ending with FFD9. Each row is similar in start and end, but different between.
If I inspect the resulting HTML around the image, I learn that it displays as a PNG. My end goal is a scripted export that will convert the string to a file. I am looking for info, links included, to send me on my way.
You have a few options. All of them include some amount of coding. There isn't a built in way for SQL Server to export base64 encoded strings as files.
However you can....
Write a CLR that will do it from the TSQL / database side. The relevant C# code you would need in the CLR is something like this:
File.WriteAllBytes(#"c:\yourfile", Convert.FromBase64String(yourBase64String));
This is courtesy of this link: Base64 encoded string to file
Or write a quick console application to run through each of your rows and pull it out. Using the same code snippet above for C# or any other number of languages that are documented out on google. Another method a quick google search found is:
public Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0,
imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
}
Courtesy of here: http://www.dailycoding.com/Posts/convert_image_to_base64_string_and_base64_string_to_image.aspx

Is it possible to convert a Windows bitmap with an Alpha channel to a MagickImage?

Using Magick.Net, Is it possible to convert (in memory) a Windows bitmap with an Alpha channel to a MagickImage? When I try the following, it fails with .net formats Format32bppPARgb and Format32bppARgb, but works fine with Format24bppRgb. The error message is "no decode delegate for this image format `XWD'".
bmp = New System.Drawing.Bitmap(400, 300, PixelFormat.Format32bppPARgb)
img = New MagickImage(bmp)
You are getting this exception due to a bug in ImageMagick. It reads the stream and tries to determine the format. It incorrectly decides that the format is XWD instead of BMP. I submitted a patch to the GIT repository of ImageMagick to fix this. Your code will work in Magick.NET 7.0.0.0018 that has not been released at the time of writing.

WPF: How to save a GIF file in a string or file from an URL?

I try to retrieve the content of a .gif located on an URL into a string -and eventually save it to disk-, ran from a WPF appliactions using the webbrowser. After trying dozens of solutions I am not further than I was yesterday. I thought that the code below would do the job, but the saved string cGif contains...the url itself. From http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.gifbitmapdecoder.aspx I then thought I needed GifBitmapEncoder instead, but the sample suggests that this creates an empty gif instead of a downloaded one.
(PS: what I basically want to do is to retrieve the GIF bytes straight from the WPF webbrowser but I haven't found anything working there either)
private void GifSave()
{
var uri = new Uri(#"http://www.archivearts.com/GIRAFFE2.gif");
var Img = new GifBitmapDecoder(uri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
string cGif = Img.ToString();
}
Modified from a sample on the GifBitmapDecoder MSDN page:
// Open a Stream and decode a GIF image
var uri = new Uri(#"http://www.archivearts.com/GIRAFFE2.gif");
GifBitmapDecoder decoder = new GifBitmapDecoder(uri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
BitmapSource bitmapSource = decoder.Frames[0];
Then use the CopyPixels() method of the BitmapSource to copy the pixel data into an array. Then convert the array into a string?
I haven't done this, I just took a look at MSDN to get some hints.

Convert memory stream to BitmapImage?

I have an image that was originally a PNG that I have converted to a byte[] and saved in a database. Originally, I simply read the PNG into a memory stream and converted the stream into a byte[]. Now I want to read the byte[] back and convert it to a BitmapImage, so that I can bind a WPF Image control to it.
I am seeing a lot of contradictory and confusing code online to accomplish the task of converting a byte[] to a BitmapImage. I am not sure whether I need to add any code due to the fact that the image was originally a PNG.
How does one convert a stream to a BitmapImage?
This should do it:
using (var stream = new MemoryStream(data))
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = stream;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
bitmap.Freeze();
}
The BitmapCacheOption.OnLoad is important in this case because otherwise the BitmapImage might try to access the stream when loading on demand and the stream might already be closed.
Freezing the bitmap is optional but if you do freeze it you can share the bitmap across threads which is otherwise impossible.
You don't have to do anything special regarding the image format - the BitmapImage will deal with it.
using (var stream = new MemoryStream(data))
{
var bi = BitmapFrame.Create(stream , BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnLoad);
}

How to save BitmapImage / WriteableBitmap in png format (Silverlight/windows phone)?

How can i save an image (BitmapImage / WriteableBitmap) in png format using Silverlight for windows phone?
Take a look at ImageTools on codeplex. That supports silverlight encoding PNG. I'm not sure whether this will work with or can be compiled for windows phone 7.
You could try the following code.
This code worked for me. Before you try, make sure that your writablebitmap has a transparent background (You can check by assigning to a image controller image source). If not, make the background transparent from the controller it was coming from.
var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
var file = await localFolder.CreateFileAsync("temp.png", CreationCollisionOption.ReplaceExisting);
using (var ras = await file.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.None))
{
WriteableBitmap bitmap = imageSource;
var stream = bitmap.PixelBuffer.AsStream();
byte[] buffer = new byte[stream.Length];
await stream.ReadAsync(buffer, 0, buffer.Length);
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ras);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96.0, 96.0, buffer);
await encoder.FlushAsync();
}

Resources