如果知道图像数据为二进制,如何显示图像

本文关键字:图像 何显示 显示 二进制 数据 如果 | 更新日期: 2023-09-27 18:04:11

如果我可以从sql-server数据库中检索图像数据,它存储为图像类型。我如何使用c#在我的web应用程序中显示图像?

如果知道图像数据为二进制,如何显示图像

您必须创建一个返回图像的http处理程序

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();
        }

你可以通过在visual studio中创建一个GenericHandler文件类型并添加前面的代码然后你可以调用你可以写通用处理程序的url作为图像源

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

你可能也想看看这个

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