gdi +错误从网页保存图像

时间:2013-11-29 16:15:26

标签: c# .net image-processing httpwebrequest gdi+

我有这样的方法来保存来自网址的图片:

public Image DownloadImage(string _URL)
        {
            Image _tmpImage = null;

            try
            {
                // Open a connection
                System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(_URL);

                _HttpWebRequest.AllowWriteStreamBuffering = true;

                // You can also specify additional header values like the user agent or the referer: (Optional)
                _HttpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0";
                _HttpWebRequest.Referer = url;

                // set timeout for 20 seconds (Optional)
                _HttpWebRequest.Timeout = timeout;

                // Request response:
                System.Net.WebResponse _WebResponse = _HttpWebRequest.GetResponse();

                // Open data stream:
                System.IO.Stream _WebStream = _WebResponse.GetResponseStream();

                // convert webstream to image
                _tmpImage = Image.FromStream(_WebStream);

                // Cleanup
                _WebResponse.Close();
            }
            catch (Exception e)
            {
                // Error
                new Log(id++, DateTime.Now, e.ToString() + e.Message);
                return null;
            }

            return _tmpImage;
        }

并调用此方法:

Image _Image = null;
                _Image = DownloadImage("https://***" + img_url);


System.Runtime.InteropServices.ExternalException (0x80004005): В GDI+ error occured
   in System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
   in System.Drawing.Image.Save(String filename, ImageFormat format)
   in System.Drawing.Image.Save(String filename)
   in konslat.TermSh.getAndSaveImage() в 

我做错了什么,以及如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

引发异常是因为您调用了_WebResponse.Close(),后者又关闭了基础Stream对象,但根据Image.FromStream方法的MSDN文档

  

您必须在图像的生命周期内保持流打开。

因此,要解决您的问题,您需要在关闭_tmpImage之前创建_WebResponse的副本。 This对类似问题的回答有一个示例代码 的更新
下面是示例代码 - 控制台应用程序,引用System.dll,System.Core.dll和System.Drawing.dll,目标框架版本4:

using System;  
using System.Drawing;  
using System.Drawing.Imaging;  
using System.IO;  
using System.Net;  
namespace SaveImage
{
class Program
{
    static void Main(string[] args)
    {
        using (Image image = DownloadImage(new Uri("http://www.hdwallpapersview.com/wp-content/uploads/2013/10/30/omg-funny-animals-dog-1.jpg")))
        {
            image.Save("temp.jpg", ImageFormat.Jpeg);
        }
    }
    public static Image DownloadImage(Uri url)
    {
        Image result = null;
        try
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.AllowWriteStreamBuffering = true;
            webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0";
            WebResponse webResponse = webRequest.GetResponse();
            using (Stream webStream = webResponse.GetResponseStream())
            {
                using (Image tempImage = Image.FromStream(webStream))
                {
                    result = new Bitmap(tempImage);
                }
            }
            webResponse.Close();
        }
        catch (Exception e)
        {
            return null;
        }

        return result;
    }
}

}