winform截图form图的问题

qq_36434521 2017-07-18 08:56:25
下面是我添加了CefSharp组件中的ChromiumWebBrowser,在我实际运行的窗体上是显示这个控件的,但是我截取的form图中不显示这个浏览器控件,我修改了下它的一些背景色、边框线都能显示出来,不知道为什么。求大神帮忙看一下,这个问题困扰了我好久了QWQ

其实我的需求是单独截取浏览器的截图,一开始截取下来的浏览器截图是空白的,找不到问题才有了下面的测试

截取的form图:

实际运行的窗体:


代码:

private static ChromiumWebBrowser browser = new ChromiumWebBrowser("http://www.baidu.com");

private void Form2_Load(object sender, EventArgs e)
{
browser.FrameLoadEnd += new EventHandler<FrameLoadEndEventArgs>(browser_FrameLoadEnd);
browser.Visible = true;
browser.Dock = DockStyle.Fill;
groupBox1.Controls.Add(browser);
}
private void browser_FrameLoadEnd(object obj, EventArgs e)
{
int width = this.Width;
int height = this.Height;
Bitmap bitmap = new Bitmap(width, height); // 创建画布大小
Rectangle rectangle = new Rectangle(0, 0, width, height); // 绘图区域
browser.DrawToBitmap(bitmap, rectangle);
bitmap.Save("D:\\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
...全文
585 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
TsaiRyan 2020-05-11
  • 打赏
  • 举报
回复
Thank you : )
desperaso 2019-12-26
  • 打赏
  • 举报
回复
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);
}
}
}
}
}



private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{

Bitmap screenshot = new Bitmap(webBrowser1.Width, webBrowser1.Height);
NativeMethods.GetImage(webBrowser1.ActiveXInstance, screenshot, Color.White);

// 保存图片
screenshot.Save(@"F:\h11.png");
}

datafansbj 2019-12-26
  • 打赏
  • 举报
回复
引用 5 楼 qq_36434521 的回复:
[quote=引用 4 楼 myheadachecase 的回复:] private void button1_Click(object sender, EventArgs e) { Graphics g = this.CreateGraphics(); Size s = this.Size; Bitmap image = new Bitmap(s.Width, s.Height, g); Graphics g2 = Graphics.FromImage(image); g2.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s); g2.DrawImage(image, 0, 0); image.Save("a.jpg"); }
我需要控件在不可见的情况下对网页进行截图,这个实现不了的[/quote] 没整明白,控件不可见?那个控件不可见?是浏览器控件不可见吗?不可见又怎么截图啊?
gw6328 2019-12-26
  • 打赏
  • 举报
回复
没加载完你就切图,肯定是空白,你停过几秒再切看。
a472544436 2019-12-24
  • 打赏
  • 举报
回复
实话说,cefsharp是不让你截图的,截毛线。
qaz6908768 2017-07-20
  • 打赏
  • 举报
回复
browser.Visible=true提到前面去试试,另外截图方法用点击事件调试比较好点吧
qq_36434521 2017-07-19
  • 打赏
  • 举报
回复
这么大的论坛没有懂的吗
班门弄武 2017-07-19
  • 打赏
  • 举报
回复
引用 5 楼 qq_36434521 的回复:
我需要控件在不可见的情况下对网页进行截图,这个实现不了的
好的,搞出来后通知一声。
qq_36434521 2017-07-19
  • 打赏
  • 举报
回复
引用 4 楼 myheadachecase 的回复:
private void button1_Click(object sender, EventArgs e) { Graphics g = this.CreateGraphics(); Size s = this.Size; Bitmap image = new Bitmap(s.Width, s.Height, g); Graphics g2 = Graphics.FromImage(image); g2.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s); g2.DrawImage(image, 0, 0); image.Save("a.jpg"); }
我需要控件在不可见的情况下对网页进行截图,这个实现不了的
班门弄武 2017-07-19
  • 打赏
  • 举报
回复
private void button1_Click(object sender, EventArgs e) { Graphics g = this.CreateGraphics(); Size s = this.Size; Bitmap image = new Bitmap(s.Width, s.Height, g); Graphics g2 = Graphics.FromImage(image); g2.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s); g2.DrawImage(image, 0, 0); image.Save("a.jpg"); }
qq_36434521 2017-07-19
  • 打赏
  • 举报
回复
GG.........
qq_36434521 2017-07-18
  • 打赏
  • 举报
回复
大神在哪QWQ

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧