C#学习教程:将WebBrowser.Document转换为位图?分享


将WebBrowser.Document转换为位图?

是否可以将WebBrowser.Document绘制到位图? 基本上是获取WebBrowser控件的屏幕截图(注意,这是一个不在表单上但只在代码中的WebBrowser)。

WebBrowser w = new WebBrowser(); w.Document = "Hello world."; w.Document.DrawToBitmap ??? 

谢谢!

http://www.bryancook.net/2006/03/screen-capture-for-invisible-windows.html

和这里:

http://www.codeproject.com/KB/graphics/screen_capturing.aspx

我相信您应该获取WebBrowser控件的句柄并将其内容保存为这些链接中建议的图像。

我使用以下代码捕获WebBrowser控件中加载的网页的屏幕截图:

 class NativeMethods { [ComImport] [Guid("0000010D-0000-0000-C000-000000000046")] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface IViewObject { void Draw([MarshalAs(UnmanagedType.U4)] uint dwAspect, int lindex, IntPtr pvAspect, [In] IntPtr ptd, IntPtr hdcTargetDev, IntPtr hdcDraw, [MarshalAs(UnmanagedType.Struct)] ref RECT lprcBounds, [In] IntPtr lprcWBounds, IntPtr pfnContinue, [MarshalAs(UnmanagedType.U4)] uint dwContinue); } [StructLayout(LayoutKind.Sequential, Pack = 4)] struct RECT { public int Left; public int Top; public int Right; public int Bottom; } public static void GetImage(object obj, Image destination, Color backgroundColor) { using(Graphics graphics = Graphics.FromImage(destination)) { IntPtr deviceContextHandle = IntPtr.Zero; RECT rectangle = new RECT(); rectangle.Right = destination.Width; rectangle.Bottom = destination.Height; graphics.Clear(backgroundColor); try { deviceContextHandle = graphics.GetHdc(); IViewObject viewObject = obj as IViewObject; viewObject.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, deviceContextHandle, ref rectangle, IntPtr.Zero, IntPtr.Zero, 0); } finally { if(deviceContextHandle != IntPtr.Zero) { graphics.ReleaseHdc(deviceContextHandle); } } } } } 

例:

上述就是C#学习教程:将WebBrowser.Document转换为位图?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—猴子技术宅(www.ssfiction.com)

 Bitmap screenshot = new Bitmap(1024, 768); NativeMethods.GetImage(webBrowser.ActiveXInstance, screenshot, Color.White); 

  public void HTMLScreenShot() { WebBrowser wb = new WebBrowser(); wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted); wb.Size = new Size(800, 600); // Add html as string wb.Navigate("about:blank"); wb.Document.Write("Hellow World!"); // Add html from website // wb.Navigate("http://myurl.com"); } void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { WebBrowser wb = sender as WebBrowser; using (Bitmap bitmap = new Bitmap(wb.Width, wb.Height)) { Rectangle bounds = new Rectangle(new Point(0, 0), wb.Size); wb.DrawToBitmap(bitmap, bounds); bitmap.Save("C:WebsiteScreenshot.png"); } } 

 // // If you want to take a snap from existing webBrowser Control // private void button1_Click(object sender, EventArgs e) { using (FileDialog fd = new SaveFileDialog()) { fd.Filter = "Image (*.png)|*.png"; if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { new WebPageSnap(webBrowser1.Url.ToString(), fd.FileName); //might take 3 or 4 seconds to save cauz it has to load again. } } } // // Or if you want to take a snap without showing up // private void button2_Click(object sender, EventArgs e) { using (FileDialog fd = new SaveFileDialog()) { fd.Filter = "Image (*.png)|*.png"; if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string url = "http://www.google.com"; // or url = textBox1.Text; new WebPageSnap(url, fd.FileName); } } } class WebPageSnap { WebBrowser wb; string outFile; public WebPageSnap(string url, string outputFile) { wb = new WebBrowser(); wb.ProgressChanged += wb_ProgressChanged; outFile = outputFile; wb.ScriptErrorsSuppressed = true; wb.ScrollBarsEnabled = false; wb.Navigate(url); } void wb_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e) { if (e.CurrentProgress == e.MaximumProgress) { wb.ProgressChanged -= wb_ProgressChanged; try { int scrollWidth = 0; int scrollHeight = 0; scrollHeight = wb.Document.Body.ScrollRectangle.Height; scrollWidth = wb.Document.Body.ScrollRectangle.Width; wb.Size = new Size(scrollWidth, scrollHeight); Bitmap bitmap = new Bitmap(wb.Width, wb.Height); for (int Xcount = 0; Xcount < bitmap.Width; Xcount++) for (int Ycount = 0; Ycount < bitmap.Height; Ycount++) bitmap.SetPixel(Xcount, Ycount, Color.Black); wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height)); bitmap.Save(outFile, ImageFormat.Png); } catch { } } } } 

本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ssfiction.com/ckf/1269750.html

(0)
上一篇 1天前
下一篇 1天前

精彩推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注