Results 1 to 4 of 4

Thread: Convert byte[] into image using ASP.Net

  1. #1
    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

  2. #2
    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;
    }

  3. #3
    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.

  4. #4
    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);

Similar Threads

  1. Convert 2D image into 3D image using Silverlight 4
    By Thenral in forum Windows Software
    Replies: 5
    Last Post: 18-04-2011, 10:45 AM
  2. Can I convert MBM image to JPG...Is this possible?
    By Bon-hwa in forum Operating Systems
    Replies: 4
    Last Post: 11-11-2010, 12:34 AM
  3. Convert image byte [] using java
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 02-02-2010, 03:38 AM
  4. Convert Vmware Image to Virtualbox Image
    By rashmi_ay in forum Windows Software
    Replies: 5
    Last Post: 23-12-2009, 02:56 PM
  5. Replies: 4
    Last Post: 20-05-2009, 01:32 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,549,556.33767 seconds with 16 queries