1

I am dynamically creating controls in c# code of my asp.net website. I want to create a image control and display a image which is derived from byte array got from wcf service. I tried to convert the byte array in to image and save in a location with the following code but it does'nt work. Can any one help me!

 public System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
{
    MemoryStream ms = new MemoryStream(byteArrayIn);
    System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
    return returnImage;
}
Annamalai S
  • 127
  • 2
  • 17
  • http://stackoverflow.com/questions/4444421/convert-a-byte-array-to-image-in-c-sharp-after-modifying-the-array – GeorgesD Jan 07 '13 at 09:33
  • Check this: http://stackoverflow.com/questions/7390983/convert-from-binarydata-to-image-control-in-asp-net – Kapil Khandelwal Jan 07 '13 at 09:36
  • No need to first convert the byte stream into an image just to save to disk. Just save all the data directly to your location. –  Jan 07 '13 at 10:05

3 Answers3

5

You're just missing the Save method:

public string byteArrayToImage(byte[] byteArrayIn)
{
  string saveLocation = "<Path to save image to>"; // e.g. c:\mywebsite\image23.png
  MemoryStream ms = new MemoryStream(byteArrayIn);
  System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
  returnImage.Save(saveLocation);
  return saveLocation;
}

Then use Server.MapPath to pass the url of the saved file location to an asp:Image control.

I should just mention that if you already have a byte array - instead of creating an image object just for saving the bytestream - just put the bytestream inside the ImageUrl of the asp:image control like this:

image1.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteArrayIn);
Blachshma
  • 17,097
  • 4
  • 58
  • 72
  • @Annamalai - It should work fine, please show us how you set the URL after receiving the string. Anyways, I added another option for you which doesn't require saving the file to the disk. – Blachshma Jan 07 '13 at 09:48
  • Base64 encoded images for data uri's must be on a single line for some browsers. IIRC Convert.ToBase64String split the lines. –  Jan 07 '13 at 10:06
  • In any case, data uri's cannot be cached so this is not recommended due to performance penalty. –  Jan 07 '13 at 10:08
  • @Ken-AbdiasSoftware Haven't encountered any problems with ToBase64String causing line splitting but good to know if that ever comes up. About the performance penalty, saving all the images to the disk (and only storing a string URL in the DB) is *always* the best practice, but in cases such as OPs - it's not always possible – Blachshma Jan 07 '13 at 10:18
4

Thats nice. There is no need of creating an image object. This worked for me:

image1.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteArrayIn);
Mad Dog Tannen
  • 7,129
  • 5
  • 31
  • 55
2

Try the following

File.WriteAllBytes(@"C:\test.jpg", BYTE_ARRAY_OF_IMAGE);
Behroz Sikander
  • 3,885
  • 3
  • 22
  • 36