Windows Phone 8 C# convert base64 to image binding this resul - c#

I'm trying to convert base64 string to image and bind the result.
This is my xaml :
<Image Source="{Binding image64}">
To be sure my base64 string is correct i did that :
public BitmapImage image64
{
get
{
**//Convert my path img to Base64.**
byte[] bytes = System.Text.UTF8Encoding.UTF8.GetBytes(image);
string base64String = System.Convert.ToBase64String(bytes);
MessageBox.Show("Base 64 String :[" + base64String + "]");
//Convert my img base64 to img.
byte[] fileBytes = Convert.FromBase64String(base64String);
using (MemoryStream ms = new MemoryStream(fileBytes, 0, fileBytes.Length))
{
ms.Write(fileBytes, 0, fileBytes.Length);
BitmapImage bitmapImage = new BitmapImage();
**bitmapImage.SetSource(ms);**
return bitmapImage;
}
}
}
This code don't work in my case because of setSource. I found this "solution" here :
similar question 1
similar question 2
But they don't work in my case, i think it's because they didn't use the binding. And i don't have any idea to fix it...
Sorry for my english, and i hope someone can help me :)

I found this solution, it works perfectly.
byte[] filebytes = Convert.FromBase64String(image);
MemoryStream ms = new MemoryStream(filebytes, 0, filebytes.Length);
BitmapImage mimage = new BitmapImage();
mimage.SetSource(ms);
return mimage;

Related

How to Convert Base64 String into Png Image Mvc5 C#?

I am Trying to Convert a Base64 encoded string to A Png Image, But the Code Shows Exception of Parameter is Not Valid on Image.FromStream(). After Debugging i cam up with this error on MemoryStream Object "ReadTimeout = 'ms.ReadTimeout' threw an exception of type 'System.InvalidOperationException'" . I am stuck, Is there any Solution or Alternative to Convert String to Png in C#?
Here is My code
string code = "string";
var databytes = Encoding.Unicode.GetBytes(code);
var base64 = Convert.ToBase64String(databytes);
Byte[] Bytes = Convert.FromBase64String(base64);
//Stream bytes
MemoryStream ms = new MemoryStream(Bytes, 0, Bytes.Length);
//convert image
Image newImage = Image.FromStream(ms);
newImage.Save("~/Content/");
Try this:
byte[] bytes = Convert.FromBase64String(base64);
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
You can convert Base64 string into png in this way:
byte[] bytes = Convert.FromBase64String(base64);
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
image.Save("~/Content/", System.Drawing.Imaging.ImageFormat.Png);
}

Windows Phone 8.1 get image from base64 string

How can i get an Image from a base64 string on Windows Phone 8.1?
I know how to decode the image if that helps?
var imageBytes = Convert.FromBase64String(base64StringWithImageData);
var ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
var decoder = await BitmapDecoder.CreateAsync(BitmapDecoder.PngDecoderId, ms.AsRandomAccessStream());
But nowhere have i found how i can get a Image from any of that.
All of the solutions i have found are for wfp or pre win phone 8.1.
I need this to work in a windows store app.
If possible i don't want to save the image to disk first and then load it.
Background: I get Images (customer signatures) from the server in custom objects where images are stored as base64 encoded strings. So the images aren't stores on a website or on disk.
Update
The answer that was suggested doesn't work for me :(
I changed it a little to get it to compile. The code below is from the suggested answer.
var imageBytes = Convert.FromBase64String(_orderEntity.Signature);
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
ms.Write(imageBytes, 0, imageBytes.Length);
var bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(ms.AsRandomAccessStream());
signatureImage.Source = bitmapImage;
}
But that code throws a "The component cannot be found. (Exception from HRESULT: 0x88982F50)"
Google sais that means that the image format is unknown :(
This is (one of) the strings i use as input:
"iVBORw0KGgoAAAANSUhEUgAAAYwAAACQCAIAAACUOMtJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAGpMAABqTAfnkTrYAABEeSURBVHhe7Z0J0JXTH8d/xVspWkSLmLK1UKORbUojMqVlZI+xJYRRg7xpTGWZIUwhGcxkKWRLiiJjD61ka6hRSZZUKr2RpaT3/z1u/+ut995znufe53nuOef5nmlMo+c+55zP79zv/Z1zfud3alRWVgoLCZAACdhKoKatDWO7SIAESEARoEhxHJAACVhNgCJltXnYOBIgAYoUxwAJkIDVBChSVpuHjSMBEqBIcQyQAAlYTYAiZbV52DgSIAGKFMcACZCA1QQoUlabh40jARKgSHEMk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EkiUAEUqUdysjARIICwBilRYYnyeBEggUQIUqURxszISIIGwBChSYYnxeRIggUQJUKQSxc3KSIAEwhKgSIUlxudJgAQSJUCRShQ3KyMBEghLgCIVlhifJwESSJTA/wBcyHVBDaExMgAAAABJRU5ErkJggg=="
I can decompile that string just fine in both a windows Form app:
public static Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
var imageBytes = Convert.FromBase64String(base64String);
var ms = new MemoryStream(imageBytes, 0,
imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
var image = Image.FromStream(ms, true);
return image;
}
and a wpf app:
public static Image Base64ToImage(string base64String)
{
try
{
byte[] binaryData = Convert.FromBase64String(base64String);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = new MemoryStream(binaryData);
bi.EndInit();
Image img = new Image();
img.Source = bi;
return img;
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
return new Image();
}
So why cant i get it to work on Windows Phone??
I found a solution that works!
var imageBytes = Convert.FromBase64String(base64String);
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{
using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
{
writer.WriteBytes((byte[])imageBytes);
writer.StoreAsync().GetResults();
}
var image = new BitmapImage();
image.SetSource(ms);
}
Found the solution here: Load, show, convert image from byte array (database) in Windows Phone 8.1
and there is a the solution here: http://www.codeproject.com/Tips/804423/Conversion-between-File-Byte-Stream-BitmapImage-an

Convert images to PNG after reading media item from Sitecore

This is code that i have now implemented:
string image = String.Empty;
ImageField imageField = itm.Fields["image"];
MediaItem mediaItem = imageField.MediaItem;
if (mediaItem != null)
{
Stream stream = mediaItem.GetMediaStream();
Image img = Image.FromStream(stream);
Bitmap bmp = new Bitmap(img, 188, 122);
ImageConverter converter = new ImageConverter();
Byte[] bytes = (byte[])converter.ConvertTo(bmp, typeof(byte[]));
stream.Read(bytes, 0, bytes.Length);
image = "data:" + mediaItem.MimeType + "," + Convert.ToBase64String(bytes);
}
return image;
What i would like to do is to convert all images that come through, into PNGs. The problem that i have right now, is that GIF images that i read from Sitecore, after all the conversion that you see above, render as black images.
I tried some implementations but the result seems to be the same.
Could anyone please help?
Best regards, Marius.
It seems that the actual problem was that i had an additional line of code that was messing things up, and i am talking about this one: stream.Read(bytes, 0, bytes.Length);
This my final code that is currently working:
string image = String.Empty;
ImageField imageField = itm.Fields["image"];
MediaItem mediaItem = imageField.MediaItem;
if (mediaItem != null)
{
Stream stream = mediaItem.GetMediaStream();
Image img = Image.FromStream(stream);
Bitmap bmp = new Bitmap(img, 188, 122);
ImageConverter converter = new ImageConverter();
Stream pngStream = new MemoryStream();
bmp.Save(pngStream, ImageFormat.Png);
Byte[] bytes = (byte[])converter.ConvertTo(Image.FromStream(pngStream), typeof(byte[]));
image = "data:image/png;base64," + Convert.ToBase64String(bytes);
}
return image;
I hope that it's helpful for somebody.
Have a great day.

Converting image to base64

I have the following code to convert image to base64:
private void btnSave_Click(object sender, RoutedEventArgs e)
{
StreamResourceInfo sri = null;
Uri uri = new Uri("Checked.png", UriKind.Relative);
sri = Application.GetResourceStream(uri);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(sri.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
MemoryStream ms = new MemoryStream();
wb.SaveJpeg(ms, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100);
byte[] imageBytes = ms.ToArray();
base64 = System.Convert.ToBase64String(imageBytes);
}
And the following code to get Bitmap image form base 64:
public static BitmapImage base64image(string base64string)
{
byte[] fileBytes = Convert.FromBase64String(base64string);
using (MemoryStream ms = new MemoryStream(fileBytes, 0, fileBytes.Length))
{
ms.Write(fileBytes, 0, fileBytes.Length);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(ms);
return bitmapImage;
}
}
So when i convert and deconvert it is blank.
I know that deconverter works, because, when i give him exact string:
string base64="iVBORw0KGgoAAAANSUhEUgAAACQAAAAkCAQAAABLCVATAAACH0lEQVR42q3WoZKrMBQGYGRkIpHEoY9DMrh1nUGtzxPcGV7gCsTaK3iBCqa2ipmrVqLrWrmytjL3nBwoEGD30ja/6JaSj/wp3SEIXjpUoB+Oeg0zpoR+NsyoDVOgi39cbYHAy4MQTc0wOYZepxRBUkn9UxxEiNnXxyYwd6w/438hSddHJilv1tqv664Shle1DeJaJihPV9uNQ+NWBRK2QVSr+GjtaFzOIpdjKFShnoY+Gv0N0u0OVLexY48NQ+68JchdpQu/o1piVMu6faJdwjNWIAYyl55bqGUtbndO53TzCIpUpCkdlEm+V3J3Ir8r3uops2+FkTmvx832IGJwN97xS/5Ti0LQ/WLwtbxMal2ueAwvc2c8CAgSJip5U4+tKHECMlUzq2UcA9EyROuJi6/71dtzWAfVcq0Jw1CsYh13kDDteVoirE+zWtLVinQ8ZAS5YlVlvRHWfi3pakUQL0OOwmp/W/vN6Gt5zBIkzEezxnCtMJsxDIECTYmhp3bej4HHzaalNMyAnzE0UBKp6Z1Do2pwd3JkAH6CxlTs/bZOZ661yMwhohDLQqREMWz8UAvWoUQleggehG5dSPUbv28GJlnKHGJsqPi7vuG/MGTyCGslOtkCOayrGOa/indajdudb6FUpXoepgiLHIIMriddyzrkMBhGAqlOH4U2hKCT2j0NdU8jFbzpZ3LQlh9srPqEQ1Y9lEP2CVa99KHvH8mnrGGdl9V9AAAAAElFTkSuQmCC";
Which is my Checked.png converted in online converter. It decompreses perfectly.
And this is my base64, which i get by converting:
"/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAAkACQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRA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Xhj/AIQTw9o+tyafrms6PpYB/Fiy7Tjn8aKluV2yY/2RRQB7r8CtS+E3h34wfCvxD8efBvib4hfBXRfHnhLVfir4E8G+ILfwv4o8YeALLWLO58UeHtD1+5t7mLTNQ1TSUurSKY/ZJZPN8i31XRLiWLWLL/W48Q/Bf/gmX/wXm/4Jg6V8PfgkPhtqfwYT4ey+EfgFrOleDorLxX+xt8SNA8Jnwz4Ot4fh9pmseE9d8E6x8M2t9Jt9T+HEOveHvD/jvwbY2+l2uq6n4D8Q6Vrd3/kEQxfuYiMcxp3PdB+Fff8A/wAE7v8AgpB+1H/wTJ+Omn/Gv9mzxpLp9veT6ba/Ez4Wa1NdXXwy+L/hayunmPhzxx4eS4ijnmhjuL1dC8UaebXxX4Unvry58P6tZi+1GG7AP9Q3/gpJ+2p+zz/wQi/4JraUnww8MeHNE1Lwp4PsfgV+x58FbcKIvEPju20OSDR9R1i3iaG81Dw34RiSbx98U/Ed3PBe69Ik1nc6w3jPxro7ah/kLfEnx946+L/xB8b/ABV+JviXVvG3xF+I/ivXvG/jrxdrlws+r+JfFfibU7nV9c1nUZQsStdX+o3dxcyCKKG3jMnlW0ENvHFGv6ff8Fgf+CqPxQ/4K0ftQQfHDxdoEnw7+HHgrwxZeCfgx8HY/EDeI7H4f6AUhv8AxNfXeqrY6Tba34q8X+JjcalrmvpoumS3OmWnhnw+8Mtj4YsGH5ReT9PzNAHF6guy4K4wdi5HXrmip9YXbesP+mcfr6UUAdHb+NTFbwQ3Hhnw5qE0MUcT310fEUd1ciKNI0e4Fh4hsrRptqDzJY7WOSeQvNO0szvI0v8AwnEX/QneFf8Av74t/wDmroooAP8AhOIv+hO8K/8Af3xb/wDNXSjxzECD/wAIb4UODnBl8XYPsceLAcH2IPoRRRQByWp6g+p3kl21taWm8KqW1lE0UEUaDaiKZZJriUgcGa6uLi4fjzJnwMFFFAH/2Q=="
My problem is that string which i get as base64 from my code - is incorrect *What i did wrong?*
What about trying:
public static BitmapImage base64image(string base64string)
{
byte[] fileBytes = Convert.FromBase64String(base64string);
using (MemoryStream ms = new MemoryStream(fileBytes))
{
Image streamImage = Image.FromStream(ms);
context.Response.ContentType = "image/jpeg";
streamImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
return streamImage;
}
}
I agree with Alexei that your code for reading the image in does look a little strange. I've recently written some code for a similar task that I was doing which might point you in the right direction:
string fileContent = null;
/* Check the file actually has some content to display to the user */
if (uploadFile != null && uploadFile.ContentLength > 0)
{
byte[] fileBytes = new byte[uploadFile.ContentLength];
int byteCount = uploadFile.InputStream.Read(fileBytes, 0, (int)uploadFile.ContentLength);
if (byteCount > 0)
{
fileContent = CreateBase64Image(fileBytes);
}
}
private string CreateBase64Image(byte[] fileBytes)
{
Image streamImage;
/* Ensure we've streamed the document out correctly before we commit to the conversion */
using (MemoryStream ms = new MemoryStream(fileBytes))
{
/* Create a new image, saved as a scaled version of the original */
streamImage = ScaleImage(Image.FromStream(ms));
}
using (MemoryStream ms = new MemoryStream())
{
/* Convert this image back to a base64 string */
streamImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return Convert.ToBase64String(ms.ToArray());
}
}
not an answer: more of a long comment ... OP states that decoding code works perfectly fine, also it looks suspicios. Also code assumed to be verified to work on PNG images, but saving code explicitly produces valid JPG with SaveJpeg call...
Your code that creates stream for reading looks strange - you create stream over existing byte array, than write the same bytes into that stream, and that pass that stream without seeking back to 0 to some method.
Potential fix (assuming BitampImage can accept JPG stream):
don't call Write at all as stream already have the bytes you want
set ms.Position = 0 after writing to the stream.
Note: I'm not sure if it is OK to dispose stream that is a source for BitmapImage, you may need to remove using too.

Convert base64 string to image in C# on Windows Phone

I have a base64 string and I want convert that to an image and set the Source of an Image control to the result of that.
Normally I would do that using Image.FromStream, similar to this:
Image img;
byte[] fileBytes = Convert.FromBase64String(imageString);
using(MemoryStream ms = new MemoryStream())
{
ms.Write(fileBytes, 0, fileBytes.Length);
img = Image.FromStream(ms);
}
However, the Image.FromStream method does not exist on Windows Phone, and a casual search only turns up results that depend on that method.
You can use a method like this:
public static BitmapImage base64image(string base64string)
{
byte[] fileBytes = Convert.FromBase64String(base64string);
using (MemoryStream ms = new MemoryStream(fileBytes, 0, fileBytes.Length))
{
ms.Write(fileBytes, 0, fileBytes.Length);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(ms);
return bitmapImage;
}
}
Add an image to your XAML, such as this:
<Image x:Name="myWonderfulImage" />
You can then set the source, like this:
myWonderfulImage.Source = base64image(yourBase64string);

Categories

Resources