使用 WebClient 获取远程图像会产生颗粒状 GIF 并且无法处理 PNG+BMP
题
问候!
我正在创建一个 Web 表单原型 (ImageLaoder.aspx),它将返回一个图像,以便可以像这个简单的示例一样使用其他 Web 表单/网页:
<img src="http://www.mydomain.com/ImageLoader.aspx?i=http://images.mydomain.com/img/a.jpg" />
到目前为止,它加载 JPG 没有任何问题,但与原始文件相比,GIF 看起来“有颗粒感”,BMP 和 PNG 会导致以下异常:
System.Runtime.InteropServices.ExternalException:GDI+ 中发生一般错误
到目前为止我的代码如下所示:
protected void Page_Load(object sender, EventArgs e)
{
string l_filePath = Request.QueryString["i"];
System.Drawing.Image l_image = GetImage(l_filePath);
if (l_image != null)
{
System.Drawing.Imaging.ImageFormat l_imageFormat = DetermineImageFormat(l_filePath);
WriteImageAsReponse(l_image, l_imageFormat);
}
}
private System.Drawing.Image GetImage(string filePath)
{
WebClient l_WebClient = new WebClient();
byte[] l_imageBytes = l_WebClient.DownloadData(filePath);
System.Drawing.Image l_image = null;
using (MemoryStream l_MemStream = new MemoryStream(l_imageBytes, 0, l_imageBytes.Length))
{
l_MemStream.Write(l_imageBytes, 0, l_imageBytes.Length);
l_image = System.Drawing.Image.FromStream(l_MemStream, true);
l_MemStream.Close();
}
return l_image;
}
private System.Drawing.Imaging.ImageFormat DetermineImageFormat(string filePath)
{
if (filePath.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase))
return System.Drawing.Imaging.ImageFormat.Jpeg;
else if (filePath.EndsWith(".gif", StringComparison.OrdinalIgnoreCase))
return System.Drawing.Imaging.ImageFormat.Gif;
else if (filePath.EndsWith(".png", StringComparison.OrdinalIgnoreCase))
return System.Drawing.Imaging.ImageFormat.Png;
else
return System.Drawing.Imaging.ImageFormat.Bmp;
}
private void WriteImageAsReponse(System.Drawing.Image image, System.Drawing.Imaging.ImageFormat imageFormat)
{
if (image == null)
return;
System.Drawing.Bitmap l_outputBitMap = new Bitmap(image);
if (imageFormat == System.Drawing.Imaging.ImageFormat.Jpeg)
Response.ContentType = "image/jpg";
else if (imageFormat == System.Drawing.Imaging.ImageFormat.Gif)
Response.ContentType = "image/gif";
else if (imageFormat == System.Drawing.Imaging.ImageFormat.Png)
Response.ContentType = "image/png";
else
Response.ContentType = "image/bmp";
l_outputBitMap.Save(Response.OutputStream, imageFormat);
}
知道为什么 GIF 呈颗粒状而 PNG 和 BMP 会导致异常吗?
解决方案
关于 GetImage 方法的几点:
- 当您使用 Image.FromStream 时,您不应该关闭(或处置)流
- 如果您在流上调用 Dispose(使用 using 语句),则无需调用 Close
- 您正在写入流,但不是“倒带”,因此据我所知, l_image 实际上没有获取任何数据(除非 Image.FromStream 重置位置本身)。(可能是 gif/jpg 解码器倒带流,但 bmp/png 不倒带,因此出现错误。)
- 为什么不直接使用采用字节数组的 MemoryStream 构造函数?
简而言之,我相信您的 GetImage 方法可以替换为:
private Image GetImage(string filePath)
{
WebClient l_WebClient = new WebClient();
byte[] l_imageBytes = l_WebClient.DownloadData(filePath);
MemoryStream l_stream = new MemoryStream(l_imageBytes);
return Image.FromStream(l_stream);
}
现在,更重要的是 - 为什么要加载图像?为什么不只将文件本身作为响应,按照您已经在做的那样设置内容类型 - 或者可能只是基于扩展名?换句话说,你的所有代码都将变成:
protected void Page_Load(object sender, EventArgs e)
{
string filePath = Request.QueryString["i"];
string extension = l_filePath.Substring(l_filePath.LastIndexOf('.') + 1);
Response.ContentType = "image/" + extension;
byte[] data = new WebClient.DownloadData(filePath);
Response.OutputStream.Write(data, 0, data.Length);
Response.End();
}
多一点错误处理(包括“这是一个合理的扩展吗?”)会很好,但除此之外我认为还可以。自己实际加载图像的唯一好处是您可以验证它是否真的 是 图像而不是病毒或类似的东西。
编辑:只是出于兴趣,你有充分的理由吗 想 图像请求要通过您的服务器吗?为什么网页作者会这样写:
<img src="http://www.mydomain.com/ImageLoader.aspx?i=http://images.mydomain.com/img/a.jpg" />
代替
<img src="http://images.mydomain.com/img/a.jpg" />
那里 是 它可能有用的某些原因,但在许多情况下它只是一种浪费。
不隶属于 StackOverflow