0

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+.

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

}
Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
TC Alper Tokcan
  • 369
  • 1
  • 13
  • 1
    Have a look at [this](http://stackoverflow.com/questions/336387/image-save-throws-a-gdi-exception-because-the-memory-stream-is-closed). Might give you an idea! – huMpty duMpty Oct 23 '13 at 10:49
  • No thats not I'm looking for it says dont dispose stream when you use the bitmap ,like what I do I thing there is another problem :( – TC Alper Tokcan Oct 23 '13 at 10:55

1 Answers1

1

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.

Alternatively, you can use Html Agility Pack to find all img elements and download the resource behind their src URL with HttpWebRequest for example.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • It happens for same image files I can downlad it with my browser but image has no diffrence from others.Image that couses error is --> [link](www.modaelinizde.com/paco-rabanne-one-million-100-ml-edt-parfumu) – TC Alper Tokcan Oct 23 '13 at 12:54