c# winform 截图 网页

原理:通过winform程序 内置的WebBrowser 打开网页,打开成功后,通过Drawing调整保存图片。

 1 //winform加载事件

 2 private void Form_Load(object sender, EventArgs e)

 3 {    

 4     //接收web url

 5     string colle = string.Empty;

 6     string url = string.Empty;

 7     //获取进程调用传入的命令

 8     string[] args = Environment.GetCommandLineArgs();

 9     //string[] args = new string[] { @"E:\MicroSpace\MicroSpace\localpage\Debug\GetCutImage.exe", "-u:http://sliefj.com/template/amo.html","-n:E:\\TEST\\test.jpg" };

10     

11     //获取参数

12     for (int i = 1; i < args.Length; i++)

13     {

14         switch (args[i].Substring(0, 3))

15         {

16             case "-u:":

17                 url = args[i].Substring(3);

18                 break;

19             case "-n:":

20                 imagename = args[i].Substring(3);

21                 break;

22             default:                        

23                 colle = args[i];

24                 break;

25         }

26     }            

27     WebBrowser webBrowser = new WebBrowser();  // 创建一个WebBrowser

28     webBrowser.ScrollBarsEnabled = false;  // 隐藏滚动条

29     webBrowser.Navigate(url);  // 打开网页

30     webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);  // 增加网页加载完成事件处理函数

31 }

 

 1 //webBrowser 页面加载完成后,保存图片        

 2 private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

 3 {

 4     WebBrowser webBrowser = (WebBrowser)sender;   

 5     // 网页加载完毕才保存

 6     if (webBrowser.ReadyState == WebBrowserReadyState.Complete)

 7     {

 8         bool save_suc = true;

 9         try

10         {

11             // 获取网页高度和宽度,也可以自己设置

12             int height = webBrowser.Document.Body.ScrollRectangle.Height;

13             int width = webBrowser.Document.Body.ScrollRectangle.Width;

14 

15             //截图宽度、高度

16             int imgwidth = width - 2 * 60;

17             int imgheight = height - 48;

18 

19             // 调节webBrowser的高度和宽度

20             webBrowser.Height = height;

21             webBrowser.Width = width;

22 

23             Bitmap bitmap = new Bitmap(width, height);  // 创建高度和宽度与网页相同的图片

24             Rectangle rectangle = new Rectangle(0, 0, width, height);  // 绘图区域

25             //Rectangle mm=new Rectangle(

26             webBrowser.DrawToBitmap(bitmap, rectangle);  // 绘制浏览器图片至bitmap

27 

28             int bmpw = bitmap.Width;

29             int bmph = bitmap.Height;

30 

31             System.Drawing.Image Imag = new System.Drawing.Bitmap(imgwidth, imgheight);//创建Image

32             System.Drawing.Graphics gp = System.Drawing.Graphics.FromImage(Imag);//获取Image的Graphics

33 

34             //通过Graphics绘图实现截图效果

35             gp.DrawImage(bitmap,new Rectangle(0,0,imgwidth,imgheight),new Rectangle(60,48,imgwidth,imgheight),GraphicsUnit.Pixel);

36             //保存

37             Imag.Save(imagename, System.Drawing.Imaging.ImageFormat.Jpeg);

38             //bitmap.Save(imagename, System.Drawing.Imaging.ImageFormat.Jpeg);

39             bitmap.Dispose();

40             gp.Dispose();

41             Imag.Dispose();

42         }

43         catch (Exception ex)               

44         {         

45            

46         }

47         finally

48         {

49                                

50             this.Dispose(true);

51             System.Diagnostics.Process.GetCurrentProcess().Kill();

52             this.Close();

53             Application.Exit();

54         }

55     }

56 }
1 //图片保存方式

2 //imagename 图片路径

3 bitmap.Save(imagename, System.Drawing.Imaging.ImageFormat.Jpeg);

4 Image.Save(imagename, System.Drawing.Imaging.ImageFormat.Jpeg);

 

你可能感兴趣的:(WinForm)