Steven Zack
Steven Zack

Reputation: 5104

how to display image if knows image data as binary?

If I can retrieve image data from sql-server database, which stored as image type. How can I display the image in my web application using c#? Thanks in advance.

Upvotes: 0

Views: 1828

Answers (3)

sreeprasad
sreeprasad

Reputation: 3260

You may want to look at this as well

byte[] imageBytes = (byte[]) imageReader.GetValue(0);    
MemoryStream ms = new MemoryStream(imageBytes); 
FileStream fs = File.OpenWrite(imagePath);
fs.Write(ms.GetBuffer(), 0, ms.Position());

Upvotes: 0

Samir Adel
Samir Adel

Reputation: 2499

You have to create an http handler that returns the image

public void ProcessRequest(HttpContext context)
        {
            Byte[] yourImage = //get your image byte array
            context.Response.BinaryWrite(yourImage);
            context.Request.ContentType = "image/jpeg";
            context.Response.AddHeader("Content-Type", "image/jpeg");
            context.Response.AddHeader("Content-Length", (yourImage).LongLength.ToString());
            con.Close();

            context.Response.End();
            context.Response.Close();
        }

You can can do that by creating a GenericHandler file type from the visual studio and add the previous code in then you can call you can write the url of the generic handler as the image source

Upvotes: 4

Illuminati
Illuminati

Reputation: 4619

 MemoryStream ms = new MemoryStream(byteArrayFromDB);
     Image returnImage = Image.FromStream(ms);

Upvotes: 2

Related Questions