下载图像并将其保存到硬盘后2图像获得gdi异常为什么?

时间:2016-08-28 21:52:49

标签: c# .net winforms

def convert[T1, T2](x: T1)(implicit a: A[T1, T2]): T2 = {
  a.convert(x)
}

def reverse[T1, T2](x: T2)(implicit a: A[T1, T2]): T1 = {
  a.reverse(x)
}

implicit val b = new B
// or implicit val c = new C

convert(7)
reverse(convert(7))

例外是在线:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using HtmlAgilityPack;
using mshtml;
using System.Text.RegularExpressions;
using System.IO.Compression;

namespace Extract_Images
{
    public partial class Form1 : Form
    {
        private int numberoflinks = 13;
        private int currentLinkNumber = 0;
        private string mainlink;
        private WebBrowser webBrowser1;
        private bool firsturltonav = false;

        public Form1()
        {
            InitializeComponent();

            webBrowser1 = new WebBrowser();
            webBrowser1.ScriptErrorsSuppressed = true;
            webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
            label1.Text = "Number of links: ";
            mainlink = "http://www.test.com";
        }

        private void ProcessFirstLink()
        {
            string firstLink = mainlink;
            firsturltonav = true;
            webBrowser1.Navigate(firstLink);
        }

        private void ProcessNextLink()
        {
            if (currentLinkNumber < numberoflinks)
            {
                currentLinkNumber++;
                string linktonav = mainlink + "index"+currentLinkNumber.ToString() + ".html";
                label1.Text = currentLinkNumber.ToString();
                webBrowser1.Navigate(linktonav);
            }
        }

        int count = 0;
        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            mshtml.HTMLDocument objHtmlDoc = (mshtml.HTMLDocument)webBrowser1.Document.DomDocument;
            string pageSource = objHtmlDoc.documentElement.innerHTML;

            if (firsturltonav == true)
                ParseNumberOfLinks();

            string[] hrefs = this.webBrowser1.Document.Links.Cast<HtmlElement>()
             .Select(a => a.GetAttribute("href")).Where(h => h.Contains(".jpg")).ToArray();

            foreach(string a in hrefs)
            {
                var result = GetImageAsync(a);
                result.ContinueWith(task =>
                {                    
                        task.Result.Save(@"C:\images\file" + count + ".jpg");
                        pictureBox1.Image = task.Result;
                        count++;
                        label2.Invoke(new MethodInvoker(delegate { label2.Text = count.ToString(); }));
                }); 
            }
            ProcessNextLink();
        }

        public async Task<Image> GetImageAsync(string url)
        {
            var tcs = new TaskCompletionSource<Image>();
            Image webImage = null;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            await Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null)
                .ContinueWith(task =>
                {
                    var webResponse = (HttpWebResponse)task.Result;
                    Stream responseStream = webResponse.GetResponseStream();
                    if (webResponse.ContentEncoding.ToLower().Contains("gzip"))
                        responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
                    else if (webResponse.ContentEncoding.ToLower().Contains("deflate"))
                        responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);

                    if (responseStream != null) webImage = Image.FromStream(responseStream);
                    tcs.TrySetResult(webImage);
                    webResponse.Close();
                    responseStream.Close();
                });
            return tcs.Task.Result;
        }

        private void ParseNumberOfLinks()
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            ProcessNextLink();
        }
    }
}

前两张图片保存正常,但抛出异常:

GDI +中发生了一般错误

如果我正在使用try和catch我在StackTrace中看到:

at System.Drawing.Image.Save(String filename,ImageCodecInfo encoder,EncoderParameters encoderParams)        在System.Drawing.Image.Save(String filename,ImageFormat格式)        在System.Drawing.Image.Save(String filename)        在D:\ C-Sharp \ Extract_Images \ Extract_Images \ Extract_Images \ Form1.cs:第76行的Extract_Images.Form1.b__2(任务`1任务)中

第76行是:

task.Result.Save(@"C:\images\file" + count + ".jpg");

0 个答案:

没有答案