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