Avatar of Erwin Pombett
Erwin Pombett
Flag 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.
JSONC#

Avatar of undefined
Last Comment
Karrtik Iyer

8/22/2022 - Mon
Karrtik Iyer

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);
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.
Karrtik Iyer

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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Erwin Pombett

ASKER
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
kaufmed

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Karrtik Iyer

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.