【发布时间】:2013-09-19 05:50:05
【问题描述】:
我正在数据库中保存高分辨率图像的高分辨率和压缩版本。
当用户请求高分辨率图像时,我需要显示压缩后的图像。这是我的代码。
问题是:当我将该图像字节数组设置为流和位图时,文件大小已压缩 2.27MB 至 339kB。
我在这里做错了什么?
private void DisplayImageFromBytes(byte[] byteArray, int resizeWidth, bool isHiResImage) {
if (isHiResImage)
{
Stream stream = new MemoryStream(byteArray);
System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
Bitmap bitmap = null;
if (resizeWidth > 0 && img.Width > resizeWidth)
{
int newHeight = (int)((float)img.Height * ((float)resizeWidth / (float)img.Width));
bitmap = new Bitmap(img, resizeWidth, newHeight);
}
else
{
bitmap = new Bitmap(img);
}
Response.ContentType = "image/Jpeg";
bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Dispose();
img.Dispose();
bitmap.Dispose();
}
else
{
DisplayImageFromBytes(byteArray, resizeWidth);
}
}
【问题讨论】:
-
那么您的高分辨率图像格式是 Jpeg 吗?
-
是的..目前可以上传任何图像类型!
-
您对所有进入您的图像使用相同的图像格式。只需尝试使用 System.Drawing.Imaging.ImageFormat.Jpeg 的 img.RawFormat。在 bitmap.Save();看看它是否适合你。
-
并且您不需要创建不需要任何转换的新位图。您可以直接将其保存为流形式 System.Drawing.Image img
-
@Rezoan 这行得通..!感谢您的建议。
标签: c# image memorystream