gdi +错误从网页保存图片 [英] gdi+ error saving image from webpage

查看:242
本文介绍了gdi +错误从网页保存图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种从网址保存图片的方法:

I have such method for saving image from url:

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() в 

我做错了什么以及如何解决呢?

what i do wrong, and how to solve this?

推荐答案

引发异常是因为您调用_WebResponse.Close()依次关闭了基础Stream对象,但是根据MSDN文档中的

The exception is raised because you call _WebResponse.Close() which in turn closes the underlying Stream object, but according to MSDN documentation for Image.FromStream method

在图像的整个生命周期中,必须保持流打开.

You must keep the stream open for the lifetime of the Image.

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

So to solve your problem you need to create a copy of your _tmpImage before closing the _WebResponse. This answer to similar question has a sample code.
Update
Here is sample code - Console application with references to System.dll, System.Core.dll and System.Drawing.dll, target framework version 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;
    }
}

}

这篇关于gdi +错误从网页保存图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆