how to display image if knows image data as binary?
If I can retrieve image data from sql-s开发者_如何学Cerver database, which stored as image type. How can I display the image in my web application using c#? Thanks in advance.
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
MemoryStream ms = new MemoryStream(byteArrayFromDB);
Image returnImage = Image.FromStream(ms);
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());
精彩评论