Link to home
Start Free TrialLog in
Avatar of Erwin Pombett
Erwin PombettFlag for Switzerland

asked on

from an image received in json string how to get the image back ?

Hello,

i receive images from a json result. this images are in a string.
i succeed to transform the json object received into  a C# object.

now the c# object has the string that represents the images.

how to get back to the image ?

thank you in advance.
Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

Given that you have converted image to bytes and then to string before sending as shown below, you can use the client code shown below to convert it back to image.

private string CreateBase64Image(byte[] fileBytes)
{
Image streamImage;
using (MemoryStream ms = new MemoryStream(fileBytes))
{
streamImage = ScaleImage(Image.FromStream(ms));
}
using (MemoryStream ms = new MemoryStream())
{
streamImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return Convert.ToBase64String(ms.ToArray());
}
}

And in client side,

$('#image1').attr('src', 'data:image/png;base64,' + imageString);
Avatar of Erwin Pombett

ASKER

Hello karrtik,
thank you for your answer.

i dont understant what to do with your code.


this is the kind of object i have after deserializing with newtonsoft.json

public class picture
{
  public string name { get; set }
  public string image {get; set}
}


do i have to convert first my string (image) to bytes[] ?  and then use your function ?

could you be more verbose to explain please...i'm having hard time to figure out this.

thank you in advance.
Hi toshi, the first part is the sending side where image is converted to bytes and then string. On the receiving side, is the client side in my comment, where the string received is converted back to image. If you want, if you give me some time, I can build a sample and share it with you.
sure !

i'll be glad to receive a sample.

i have to send the images as a response to a webservice call.
my images can be jpg or png.


thanks
SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial