我在下面有一个方法可以将PNG图像文件(已知文件大小:小于1MB(转换为字节数组,响应类返回用于图像下载的Web请求的字节数组。
问题是 ToArray((,它会在内存中创建另一个副本。GetBuffer(( 返回底层缓冲区,这提供了更好的性能。
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
using(MemoryStream ms = new MemoryStream())
{
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Png);
return ms.ToArray();
}
}
任何人都可以使用 GetBuffer(( 提供代码吗?
.NET 4.5, ASP.NET,
从流创建字节数组
将图像转换为字节数组的最快方法
您应该将文件直接保存到 asp.net 的输出蒸汽中
public ActionResult DownloadFile()
{
Image imageIn = GetImage();
imageIn.Save(Response.OutputStream, ImageFormat.Png);
return new HttpStatusCodeResult(HttpStatusCode.OK);
}