从webBrowser控件保存

从webBrowser控件保存

本文介绍了如何从webBrowser控件保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好我想在没有用户交互的情况下将图像从webBrowser控件保存到硬盘(仅限程序)。而且我不想使用WebClient或System :: Net :: WebRequest类,如果我使用这些类,我必须下载图像两次(一次在 webBrowser控件中,另一次在类中)I'm tring to save images from webBrowser control to hard disk without a user interaction(by programm only). And I do not want to Use WebClient or System::Net::WebRequest classes becouse, if i use these classes i have to download the image twice (once in webBrowser control and another in classes)我搜索并找到以下代码using namespace System::Drawing;using namespace System::Runtime::InteropServices;[ComImport, InterfaceType(ComInterfaceType::InterfaceIsIUnknown), Guid("3050F669-98B5-11CF-BB82-00AA00BDCE0B")]interface class IHTMLElementRenderFixed{ void DrawToDC([In]IntPtr hdc); void SetDocumentPrinter([In, MarshalAs(UnmanagedType::BStr)]String^ bstrPrinterName, [In]IntPtr hdc);};...mshtml::HTMLDocument^ doc = safe_cast<mshtml::HTMLDocument^>(webbrowser->Document);mshtml::IHTMLElementCollection^ collImages = doc->getElementsByTagName(L"img");for (int i = 0; i < collImages->length; ++i){ mshtml::IHTMLImgElement^ img = safe_cast<mshtml::IHTMLImgElement^>(collImages->item(nullptr, i)); IHTMLElementRenderFixed^ render = safe_cast<IHTMLElementRenderFixed^>(img); Bitmap^ bmp = gcnew Bitmap(img->width, img->height); Graphics^ g = Graphics::FromImage(bmp); IntPtr hdc = g->GetHdc(); render->DrawToDC(hdc); g->ReleaseHdc(hdc); delete g; Image^ existing = pictureBox1->Image; pictureBox1->Image = bmp; delete existing; MessageBox::Show(L"pause so you can see the picture");}推荐答案 请查找示例代码 private   void   Form1_Load( object   sender,  EventArgs  e)         private  void  Form1_Load(object  sender, EventArgs e)          {   this .webBrowser1.Navigate(" http://www.microsoft.com" );             this .webBrowser1.Navigate("http://www.microsoft.com" );          }    private   void   button1_Click( object   sender,  EventArgs  e)         private  void  button1_Click(object  sender, EventArgs e)          {   new   System.Net.WebClient();              System.Net.WebClient wc = new  System.Net.WebClient();   this .webBrowser1.Document.GetElementsByTagName(" img" );             HtmlElementCollection imgs = this .webBrowser1.Document.GetElementsByTagName("img" );    for  ( int   i  =  0;  i <  imgs.Count;  i ++)             for  (int  i = 0; i < imgs.Count; i++)              {   " src" ),  " c:\\ images"   +  i.ToString()  +  " .jpg" );                 wc.DownloadFile(imgs[i].GetAttribute("src" ), "c:\\images"  + i.ToString() + ".jpg" );              }          }  如果它对您有用,请标记为答案。 有关免费源代码,请访问 http://sourcecode-world.blogspot.com/ 这篇关于如何从webBrowser控件保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-06 15:07