C# WebBrowser Html文档到图像
我试图使网页的图像,但有些网页显示为白色页面我 在注册表编辑器中浏览\HKEY\U当前用户\U软件\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE\U浏览器\U仿真\ 并在此处添加以下内容:C# WebBrowser Html文档到图像,c#,.net,winforms,bitmap,webbrowser-control,C#,.net,Winforms,Bitmap,Webbrowser Control,我试图使网页的图像,但有些网页显示为白色页面我 在注册表编辑器中浏览\HKEY\U当前用户\U软件\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE\U浏览器\U仿真\ 并在此处添加以下内容: 带十进制值11000的WindowsFormsApp1.exe 带十进制值11000的WindowsFormsApp1.vshost.exe 这是我的密码: using System; using System.Collections.Ge
- 带十进制值11000的WindowsFormsApp1.exe
- 带十进制值11000的WindowsFormsApp1.vshost.exe
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
Dictionary<Uri, Bitmap> browserShots = new Dictionary<Uri, Bitmap>();
WebBrowser browser = new WebBrowser();
public Form1()
{
InitializeComponent();
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
}
//=========================================MADE BY JIMY====================================
private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var browser = sender as WebBrowser;
if (browser.ReadyState != WebBrowserReadyState.Complete) return;
var bitmap = WebBrowserExtender.DrawContent(browser);
if (bitmap != null)
{
if (!browserShots.ContainsKey(browser.Url))
browserShots.Add(browser.Url, bitmap);
else
{
browserShots[browser.Url]?.Dispose();
browserShots[browser.Url] = bitmap;
}
// Show the Bitmap in a PictureBox control, eventually
pictureBox1.Image = browserShots[browser.Url];
}
}
public class WebBrowserExtender
{
public static Bitmap DrawContent(WebBrowser browser)
{
if (browser.Document == null) return null;
Size docSize = Size.Empty;
Graphics g = null;
var hDc = IntPtr.Zero;
try
{
docSize.Height = (int)((dynamic)browser.Document.DomDocument).documentElement.scrollHeight;
docSize.Width = (int)((dynamic)browser.Document.DomDocument).documentElement.scrollWidth;
docSize.Height = Math.Max(Math.Min(docSize.Height, 32750), 1);
docSize.Width = Math.Max(Math.Min(docSize.Width, 32750), 1);
var previousSize = browser.ClientSize;
browser.ClientSize = new Size(docSize.Width, docSize.Height);
var bitmap = new Bitmap(docSize.Width, docSize.Height, PixelFormat.Format32bppArgb);
g = Graphics.FromImage(bitmap);
var rect = new RECT(0, 0, bitmap.Width, bitmap.Height);
hDc = g.GetHdc();
var view = browser.ActiveXInstance as IViewObject;
view.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, hDc, ref rect, IntPtr.Zero, IntPtr.Zero, 0);
browser.ClientSize = previousSize;
return bitmap;
}
catch
{
// This catch block is like this on purpose: nothing to do here
return null;
}
finally
{
if (hDc != null) g?.ReleaseHdc(hDc);
g?.Dispose();
}
}
[ComImport]
[Guid("0000010D-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IViewObject
{
void Draw(uint dwAspect, int lindex, IntPtr pvAspect, [In] IntPtr ptd,
IntPtr hdcTargetDev, IntPtr hdcDraw, ref RECT lprcBounds,
[In] IntPtr lprcWBounds, IntPtr pfnContinue, uint dwContinue);
}
[StructLayout(LayoutKind.Sequential, Pack = 4)]
struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public RECT(int left, int top, int width, int height)
{
Left = left; Top = top; Right = width; Bottom = height;
}
}
}
//=========================================MADE BY JIMY====================================}
private void button1_Click(object sender, EventArgs e)
{
browser.Navigate(textBox1.Text, null, null, "User-Agent: User agent");
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Windows.Forms;
使用系统图;
使用系统、绘图、成像;
使用System.Runtime.InteropServices;
命名空间WindowsFormsApp1
{
公共部分类Form1:Form
{
Dictionary browserShots=新建字典();
WebBrowser browser=新的WebBrowser();
公共表格1()
{
初始化组件();
browser.DocumentCompleted+=新的WebBrowserDocumentCompletedEventHandler(browser\u DocumentCompleted);
}
//========================================================================由JIMY制作====================================
私有无效浏览器\u文档已完成(对象发送者,WebBrowserDocumentCompletedEventArgs e)
{
var browser=发送方作为网络浏览器;
如果(browser.ReadyState!=WebBrowserReadyState.Complete)返回;
var bitmap=WebBrowserExtender.DrawContent(浏览器);
if(位图!=null)
{
如果(!browserShots.ContainsKey(browser.Url))
添加(browser.Url,位图);
其他的
{
browserShots[browser.Url]?.Dispose();
browserShots[browser.Url]=位图;
}
//最终在PictureBox控件中显示位图
pictureBox1.Image=browserShots[browser.Url];
}
}
公共类WebBrowserExtender
{
公共静态位图绘图内容(WebBrowser浏览器)
{
if(browser.Document==null)返回null;
Size docSize=Size.Empty;
图形g=空;
var hDc=IntPtr.Zero;
尝试
{
docSize.Height=(int)((动态)browser.Document.DomDocument.documentElement.scrollHeight;
宽度=(int)((动态)browser.Document.DomDocument).documentElement.scrollWidth;
docSize.Height=Math.Max(Math.Min(docSize.Height,32750),1);
docSize.Width=Math.Max(Math.Min(docSize.Width,32750),1);
var previousSize=browser.ClientSize;
browser.ClientSize=新尺寸(docSize.Width、docSize.Height);
var bitmap=新位图(docSize.Width、docSize.Height、PixelFormat.Format32bppArgb);
g=图形。从图像(位图);
var rect=new rect(0,0,bitmap.Width,bitmap.Height);
hDc=g.GetHdc();
var view=browser.ActiveXInstance作为IViewObject;
视图绘制(1,-1,IntPtr.Zero,IntPtr.Zero,IntPtr.Zero,hDc,ref rect,IntPtr.Zero,IntPtr.Zero,0);
browser.ClientSize=previousSize;
返回位图;
}
抓住
{
//这个拦截器是故意这样的:这里没什么可做的
返回null;
}
最后
{
如果(hDc!=null)g?.ReleaseHdc(hDc);
g?.Dispose();
}
}
[ComImport]
[Guid(“0000010D-0000-0000-C000-0000000000 46”)]
[接口类型(ComInterfaceType.InterfaceSiunknown)]
接口IViewObject
{
无效绘制(uint dwAspect、int lindex、IntPtr pvAspect、[In]IntPtr ptd、,
IntPtr hdcTargetDev、IntPtr hdcDraw、ref RECT lprcBounds、,
[In]IntPtr lprcWBounds、IntPtr pfn continue、uint dwContinue);
}
[StructLayout(LayoutKind.Sequential,Pack=4)]
结构矩形
{
公共int左;
公共int Top;
公共权利;
公共int底部;
公共矩形(整数左、整数顶、整数宽、整数高)
{
左侧=左侧;顶部=顶部;右侧=宽度;底部=高度;
}
}
}
//========================================================================由JIMY制作============================================}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
浏览(textBox1.Text,null,null,“用户代理:用户代理”);
}
}
}
尝试这样设置用户代理
browser.Navigate(url, null, null, "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0");
要打印WebBrowser控件的Html内容,需要考虑以下几点:
DocumentCompleted
将重新生成。这意味着当WebBrowser导航到一个URL时,该事件可以并将引发多次
此处的注释解释了更多内容:[WebBrowser].Document.DomDocument.documentElement.scrollHeight;
[WebBrowser].Document.DomDocument.documentElement.scrollWidth;
见:控制
,但它并没有像我们预期的那样实际实现。其他控件也是如此:使用此方法时,RichTextBox会打印空白内容Dictionary<Uri, Bitmap> browserShots = new Dictionary<Uri, Bitmap>();
private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var browser = sender as WebBrowser;
if (browser.ReadyState != WebBrowserReadyState.Complete) return;
var bitmap = WebBrowserExtender.DrawContent(browser);
if (bitmap != null) {
if (!browserShots.ContainsKey(browser.Url)) {
browserShots.Add(browser.Url, bitmap);
}
else {
browserShots[browser.Url]?.Dispose();
browserShots[browser.Url] = bitmap;
}
// Show the Bitmap in a PictureBox control, eventually
[PictureBox].Image = browserShots[browser.Url];
}
}
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class WebBrowserExtender
{
public static Bitmap DrawContent(WebBrowser browser)
{
if (browser.Document == null) return null;
Size docSize = Size.Empty;
Graphics g = null;
var hDc = IntPtr.Zero;
try {
docSize.Height = (int)((dynamic)browser.Document.DomDocument).documentElement.scrollHeight;
docSize.Width = (int)((dynamic)browser.Document.DomDocument).documentElement.scrollWidth;
var screenWidth = Screen.FromHandle(browser.Handle).Bounds.Width;
docSize.Width = Math.Max(Math.Min(docSize.Width, screenWidth), 1);
docSize.Height = Math.Max(Math.Min(docSize.Height, 32750), 1);
var previousSize = browser.ClientSize;
browser.ClientSize = new Size(docSize.Width, docSize.Height);
var bitmap = new Bitmap(docSize.Width, docSize.Height, PixelFormat.Format32bppArgb);
g = Graphics.FromImage(bitmap);
var rect = new RECT(0, 0, bitmap.Width, bitmap.Height);
hDc = g.GetHdc();
var view = browser.ActiveXInstance as IViewObject;
view.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, hDc, ref rect, IntPtr.Zero, IntPtr.Zero, 0);
browser.ClientSize = previousSize;
return bitmap;
}
catch {
// This catch block is like this on purpose: nothing to do here
return null;
}
finally {
if (hDc != null) g?.ReleaseHdc(hDc);
g?.Dispose();
}
}
[ComImport]
[Guid("0000010D-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IViewObject
{
void Draw(uint dwAspect, int lindex, IntPtr pvAspect, [In] IntPtr ptd,
IntPtr hdcTargetDev, IntPtr hdcDraw, ref RECT lprcBounds,
[In] IntPtr lprcWBounds, IntPtr pfnContinue, uint dwContinue);
}
[StructLayout(LayoutKind.Sequential, Pack = 4)]
struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public RECT(int left, int top, int width, int height)
{
Left = left; Top = top; Right = width; Bottom = height;
}
}
}