.NET ez 2012-06-20
.Net 可以輕鬆利用 WebBrowser 製作網頁截圖功能!
程式碼如下:
private void Main_Load(object sender, EventArgs e) { WebBrowser wb = new WebBrowser(); this.Controls.Add(wb); wb.Url = new Uri("http://www.google.com.tw/"); //要截取的網址 while (wb.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents(); //偵測網頁是否載入完畢 //如果寬度小於1024,就以1024為寬度,高度自動偵測 wb.Width = 1024; Application.DoEvents(); wb.Width = wb.Document.Body.ScrollRectangle.Width; if (wb.Width < 1024) wb.Width = 1024; Application.DoEvents(); wb.Height = wb.Document.Body.ScrollRectangle.Height; Application.DoEvents(); //截圖儲存為圖片 using (Bitmap bmp = new Bitmap(wb.Width, wb.Height)) { wb.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height)); EncoderParameters myEncoderParameters = new EncoderParameters(1); myEncoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 100); bmp.Save("儲存檔案路徑", GetEncoder(System.Drawing.Imaging.ImageFormat.Jpeg), myEncoderParameters); } } /// <summary> /// IMG編碼產生 /// </summary> /// <param name="format"></param> /// <returns></returns> private ImageCodecInfo GetEncoder(ImageFormat format) { ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); foreach (ImageCodecInfo codec in codecs) { if (codec.FormatID == format.Guid) return codec; } return null; }
標籤: .NET
本文章網址:
https://www.ez2o.com/Blog/Post/csharp-WebBrowser-Screenshots
https://www.ez2o.com/Blog/Post/247
https://www.ez2o.com/Blog/Post/csharp-WebBrowser-Screenshots
https://www.ez2o.com/Blog/Post/247