Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 12-06-2009
Member
 
Join Date: Oct 2008
Posts: 93
Convert byte[] into image using ASP.Net

I was able to convert my images to Byte[] but not able to get it convet it into the Byte[] to image, i have number of images stored in my database and they are in the JPG format. Is there any best way to convert these pictures from Byte[] to Image. I've got the image in the var Image:

byte[] Image;

I have tried some code for this in ASP.net but when i execute this code it gives me list of error messages.

Regards
Reply With Quote
  #2  
Old 12-06-2009
Member
 
Join Date: Jan 2009
Posts: 143
Re: Convert byte[] into image using ASP.Net

To access to the image you need to create a MemoryStream from the byte array and then call Image.FromStream,

Convert Image to byte[] array:

Code:
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
 MemoryStream img = new MemoryStream();
 imageIn.Save(img,System.Drawing.Imaging.ImageFormat.Gif);
 return img.ToArray();
}
or you may use this sample code

Code:
Image MemoSt;
DataPhoto = new Byte[File.ContentLength];
MemoSt=Image.FromStream(DataPhoto);
And to Convert byte[] array to Image:

Code:
public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream img = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(img);
     return returnImage;
}
Reply With Quote
  #3  
Old 12-06-2009
Member
 
Join Date: Jan 2009
Posts: 99
Re: Convert byte[] into image using ASP.Net

If you have not properly coded your image flushing then data is not being recognized as an image format. and to check that you may try using a System.Drawing.Bitmap method instead of an Image, to see if that will work you just need to pass 'data' into the constructor (Dim b As New Bitmap(ms) ),Even if doing this changes doesn't work, you should need to post your code that converts from a file to bytes to see if the problem lies there. and this problem might be related to the improper flushing of your images.
Reply With Quote
  #4  
Old 12-06-2009
Member
 
Join Date: Dec 2008
Posts: 161
Re: Convert byte[] into image using ASP.Net

While searching on the internet i read in one blog that you need to convert a byte array to back to an image so that it can be displayed in a PictureBox control, you can use the following function:

Code:
public Image ByteArrayToImage(byte[] data)
{
MemoryStream bipimag = new MemoryStream(data);
Image imag = new Bitmap(bipimag);
return imag;
}
And to access that you can use this as follows:

pictureBox2.Image = ByteArrayToImage(data);
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Convert byte[] into image using ASP.Net"
Thread Thread Starter Forum Replies Last Post
Convert 2D image into 3D image using Silverlight 4 Thenral Windows Software 5 18-04-2011 10:45 AM
Can I convert MBM image to JPG...Is this possible? Bon-hwa Operating Systems 4 11-11-2010 12:34 AM
Convert image byte [] using java Miles Runner Software Development 5 02-02-2010 03:38 AM
Convert Vmware Image to Virtualbox Image rashmi_ay Windows Software 5 23-12-2009 02:56 PM
help! How to convert bootable diskette image and bootable CD image to bootable flash/usb stick? Vicious Windows XP Support 4 20-05-2009 01:32 PM


All times are GMT +5.5. The time now is 05:09 PM.