ASP.Net-将System.Drawing.Image设置为System.Control.Image



我需要将我从byte[]创建的图像保存到ASPX页面中的图像控件中。到目前为止:

byte[] myByte = (byte[])row["image"];
MemoryStream myStream = new MemoryStream(myByte, false);
System.Drawing.Image sampleImage = System.Drawing.Image.FromStream(myStream);

然后我需要将此图像设置为System.Web.UI.WebControls.image

在没有任何外部.ashx或.asmx文件的情况下,如何在同一个aspx页面中执行此操作?基本上,我需要的是将来自db的byte[]设置为图像控制

以下是如何设置嵌入的base64编码图像(如果图像是png):

<asp:Image runat="server" ID="foo" />
foo.Attributes["src"] = "data:image/png;base64," + 
    Convert.ToBase64String(myByte);

同样,应该避免这种情况;你不能缓存像这样提供的图像;因此,如果您真的想提供来自db的图像,您应该考虑使用外部处理程序/页面来提供它们(并在该处理程序上设置缓存)。

最新更新