0

Hı 我正在尝试从网页中获取一些数据和图片。我可以拍摄数据和一些照片,但有时拍照时会出错,错误是=

System.Drawing---->Void Save(System.String, System.Drawing.Imaging.ImageCodecInfo, System.Drawing.Imaging.EncoderParameters) GDI+ 中出现一般错误。

我的错在哪里?

我的代码是:

if (!added)
{

    i = 0;
    object[] array = new object[6];
    HtmlElementCollection aad = webBrowser2.Document.GetElementsByTagName("p");

    array[3] = aad[4].InnerHtml;
    array[1] = aad[3].InnerHtml;
    array[2] = aad[6].InnerHtml;
    aad = webBrowser2.Document.GetElementsByTagName("h1");
    array[0] = aad[0].InnerHtml;
    aad = webBrowser2.Document.GetElementsByTagName("span");
    array[4] = aad[3].InnerHtml.Replace("<BR>", "\n");
    array[5] = webBrowser2.Document.Url.ToString();

    timer1.Enabled = false;
    added = true;
    get = true;

    HtmlElementCollection imgs = webBrowser2.Document.Images;
    if (Directory.Exists(Convert.ToString(array[1])))
    {

    }
    else
    {

        Directory.CreateDirectory("" + array[1]);

    }

    IHTMLDocument2 doc = (IHTMLDocument2)webBrowser2.Document.DomDocument;
    IHTMLControlRange imgRange = (IHTMLControlRange)((HTMLBody)doc.body).createControlRange();
    var clip = Clipboard.GetDataObject();

    int iaa = 0;

    foreach (IHTMLImgElement img in doc.images)
    {
        try
        {
            if (img.nameProp.Contains("zoom"))
            {

                imgRange.add((IHTMLControlElement)img);

                imgRange.execCommand("Copy", false, null);

                using (Bitmap bmp = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap))
                {

                    try
                    {
                        bmp.Save(@"" + Application.StartupPath + "\\" + array[1] + "\\" + img.nameProp, ImageFormat.Jpeg);
                        richTextBox2.Text += img.nameProp + " Saved " + array[1];
                        iaa++;
                    }
                    catch
                    {
                        bmp.Save(@"" + Application.StartupPath + "\\" + array[1] + "\\" + img.nameProp, ImageFormat.Bmp);
                        richTextBox2.Text += img.nameProp + " Saved  as bmp due error for " + array[1]+"\n";
                        iaa++;

                    }
                }
            }
        }
        catch(Exception ex)
        {
            HtmlElementCollection aads = webBrowser2.Document.GetElementsByTagName("p");
            richTextBox2.Text += ex.Source + "---->" + ex.TargetSite + "\n" + ex.Message + "\n" + ex.InnerException + "\n" + aads[3].InnerHtml + "\n"+img.nameProp+"\n";

        }
    }


    a.Tables[0].Rows.Add(array);
    Clipboard.SetDataObject(clip);
    richTextBox2.Text += "Product " + array[1] + " Succesfully Added with " + (iaa-1).ToString() + " images \n ";

}
4

1 回答 1

1

有时它会出错

分析何时发生。如果相同的图像文件总是发生这种情况,请使用浏览器下载并检查图像。如果它是随机发生的,例如,可能是图像尚未加载。

您可以创建一个静态 HTML 页面来测试这一点,手动添加图像进行测试。

或者,您可以使用 Html Agility Pack 来查找所有img元素并下载其srcURL后面的资源HttpWebRequest,例如。

于 2013-10-23T11:00:54.120 回答