How To Get System.Drawing.Image Obj Image from Web Service to Web Site

I have Web service for making BarcodeImage. It return Image in form of property of class ReturnData like,

public class ReturnData
{
  public Image BarcodeImage {get;set;}
  public bool IsError  {get; set;}
  public string ErrorDesc { get;set;}

}

Now I add service reference into my website. The all things are worked but when I assign return Image value than it gives error for invalid conversion

like

 System.Drawing.Image retImage = ReturnData.BarcodeImage;

(Here ReturnData is object of WebService ReturnData class. I am not writing all stuff)

Please how i get this image from Web service


I think you need to convert the image into a byte array first before sending it to the. On the receiving side, conversion from byte array to image.

One more way is to convert the image into a base 64 string when sending and then from base64 to image when receiving.

Below code is from here:

Image to Base64 String

public string ImageToBase64(Image image,
  System.Drawing.Imaging.ImageFormat format)
{
  using (MemoryStream ms = new MemoryStream())
  {
    // Convert Image to byte[]
    image.Save(ms, format);
    byte[] imageBytes = ms.ToArray();

    // Convert byte[] to Base64 String
    string base64String = Convert.ToBase64String(imageBytes);
    return base64String;
  }
}

Base64 String to Image

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;
}