Byte[] to Bitmap in ASP.net

Creating byte array from a file:

        String aFile = @”C:\\TEST\\chapter-ex_muji-t-shirt.jpg”;
        Byte[] bytes = null;

        FileStream fileStream = new FileStream(aFile, FileMode.Open, FileAccess.Read);
        bytes = new byte[(int)fileStream.Length];
        fileStream.Read(bytes, 0, (int)fileStream.Length);

using the above code, you can convert a file to Byte[].

Now , if you have the Byte[], you can convert that byte into bitmap,

        Bitmap thumbBitmap;
        ImageConverter ic = new ImageConverter();
        Image img = (Image)ic.ConvertFrom(bytes);

        thumbBitmap = new Bitmap(img);
        if (thumbBitmap != null)
        {
            Response.ContentType = “image/Jpeg”;
            thumbBitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            thumbBitmap.Dispose();
        }

So you can convert Byte[]  to your desired bitmap.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s