我正在寻找一种简单的方法来设置 Http 响应中动态创建的图像的内容长度。
Response.ContentType = "image/jpeg";
Response.Clear();
Response.BufferOutput = true;
...
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
...
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
Response.Flush();
尝试设置内容长度
Response.AddHeader("Content-Length", Response.OutputStream.Length())
给出意想不到的结果。
此外,我不希望将文件复制到另一个流(例如 MemoryStream)并将其再次复制到输出流。
有什么想法吗?
谢谢。
最佳答案
long bmpByteSize;
using (var ms = new MemoryStream(estimatedLength /* you can instantiate without this parameter at all */))
{
bmp.Save(ms, ImageFormat.Png);
bmpByteSize= ms.Length;
}
关于c# - 在 ASP.NET 中设置动态创建图像的内容长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9598197/