System.Drawing.Image作为asp.net图像容器的源代码

时间:2010-05-12 06:58:38

标签: asp.net image bytearray

我从字节数组

创建了图像
System.Drawing.Image newImage;
using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
       ms.Write(imageBytes, 0, imageBytes.Length);
       newImage = System.Drawing.Image.FromStream(ms, true);                
}

现在我需要将此图像作为asp:Image(System.Web.UI.WebControls.Image)的源代码。这是否可能,因为我知道转换是不可能的?

2 个答案:

答案 0 :(得分:1)

使用以下代码:

  System.IO.MemoryStream ms = new System.IO.MemoryStream();
  image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
  Response.ClearContent();
  Response.ContentType = "image/Gif";
  Response.BinaryWrite(ms.ToArray());


<asp:Image ID="Image1" runat="server" ImageUrl="~/pic.aspx"/>

答案 1 :(得分:1)

如果您从字节数组开始,只需发送它。除非您需要更改图像格式,否则无需解析和重新编码。

如果您确实需要执行图像处理,可以consider an existing library doesn't have memory leaks, and is optimized for the server。 GDI调用在服务器上是危险的 - 你必须确切地知道你正在做什么。

为获得最佳性能,请使用HttpModule来回答请求。 MUG4N建议的aspx页面将为请求增加大量开销。 .ashx优于.aspx文件,但不兼容MVC,并且不允许通过HttpModule和RewritePath调用实现磁盘缓存。

相关问题