简体   繁体   English

GDI +中发生一般错误

[英]A generic error occurred in GDI+

Hı I'm trying to take some data and pictures from a web page. 我正在尝试从网页上获取一些数据和图片。 I can take data and some pictures but sometimes it gives error when taking pictures error is = 我可以拍摄数据和一些照片,但有时在拍摄照片时会出错= =

System.Drawing---->Void Save(System.String, System.Drawing.Imaging.ImageCodecInfo, System.Drawing.Imaging.EncoderParameters) A generic error occurred in GDI+. System.Drawing ---->无效保存(System.String,System.Drawing.Imaging.ImageCodecInfo,System.Drawing.Imaging.EncoderParameters)GDI +中发生一般错误。

where is my fault ? 我的错在哪里?

my code is: 我的代码是:

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 ";

}

sometimes it gives error 有时会给出错误

Analyze when that happens. 分析何时发生。 If it always happens for the same image file, download it using your browser and inspect the image. 如果同一图像文件总是发生这种情况,请使用浏览器下载它并检查该图像。 If it happens randomly, it might be that the image isn't loaded yet for example. 例如,如果它随机发生,则可能是图像尚未加载。

You can create a static HTML page to test this, manually adding images to test with. 您可以创建一个静态HTML页面进行测试,并手动添加图像进行测试。

Alternatively, you can use Html Agility Pack to find all img elements and download the resource behind their src URL with HttpWebRequest for example. 或者,您可以使用Html Agility Pack查找所有img元素,并使用HttpWebRequest例如在其src URL下下载资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM