Avatar of Shiva_Thogiti
Shiva_Thogiti

asked on 

Need Assistance.. Convert byte[] to Image or Bitmap

Hi experts,
I stored an image in SQL Server 2005 in Image datatype. My requirement is to get the image stored in database and show it in a web page. I tried to convert byte[] to Image and byte[] to Bitmap.

I'm getting an exception
Parameter is not valid.
byte[] image = (byte[])dataset.Tables[0].Rows[0][0];
if (image.Length > 0)
        {
            MemoryStream astream = new MemoryStream();
            astream.Write(image, 0, image.Length);
            //System.Drawing.Image newImage=System.Drawing.Image.FromStream(astream);
            Bitmap newimage = new Bitmap(astream);
            Response.ContentType = "image/jpeg";
            newimage.Save(newfolder);
            Image2.Attributes.Add("src", newfolder);
        }

Open in new window

.NET ProgrammingASP.NETEditors IDEs

Avatar of undefined
Last Comment
Jaime Olivares
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Hello Shiva_Thogiti,

You don't actually need to do any of that! All you need to do is set the contenttype and do a binarywrite of the array to the page:

Regards,

TimCottee
byte[] image = (byte[])dataset.Tables[0].Rows[0][0];
if (image.Length > 0)
        {
            Response.ContentType = "image/jpeg";
            Response.BinaryWrite(image);
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo