-
-
Save jankurianski/f3419d4580517516c24b to your computer and use it in GitHub Desktop.
using System; | |
using System.Drawing; | |
using System.Drawing.Imaging; | |
using System.Runtime.InteropServices; | |
using System.Windows.Forms; | |
public class ControlSnapshot | |
{ | |
public static Bitmap Snapshot(Control c) | |
{ | |
int width = 0, height = 0; | |
IntPtr hwnd = IntPtr.Zero; | |
IntPtr dc = IntPtr.Zero; | |
c.Invoke(new MethodInvoker(() => { | |
width = c.ClientSize.Width; | |
height = c.ClientSize.Height; | |
hwnd = c.Handle; | |
dc = GetDC(hwnd); | |
})); | |
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppRgb); | |
if (dc != IntPtr.Zero) { | |
try { | |
using (Graphics g = Graphics.FromImage(bmp)) { | |
IntPtr bdc = g.GetHdc(); | |
try { | |
BitBlt(bdc, 0, 0, width, height, dc, 0, 0, TernaryRasterOperations.SRCCOPY); | |
} finally { | |
g.ReleaseHdc(bdc); | |
} | |
} | |
} finally { | |
ReleaseDC(hwnd, dc); | |
} | |
} | |
return bmp; | |
} | |
[DllImport("user32.dll", SetLastError = true)] | |
public static extern IntPtr GetDC(IntPtr hWnd); | |
[DllImport("user32.dll")] | |
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); | |
[DllImport("gdi32.dll")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
public static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, TernaryRasterOperations dwRop); | |
public enum TernaryRasterOperations : uint | |
{ | |
/// <summary>dest = source</summary> | |
SRCCOPY = 0x00CC0020, | |
/// <summary>dest = source OR dest</summary> | |
SRCPAINT = 0x00EE0086, | |
/// <summary>dest = source AND dest</summary> | |
SRCAND = 0x008800C6, | |
/// <summary>dest = source XOR dest</summary> | |
SRCINVERT = 0x00660046, | |
/// <summary>dest = source AND (NOT dest)</summary> | |
SRCERASE = 0x00440328, | |
/// <summary>dest = (NOT source)</summary> | |
NOTSRCCOPY = 0x00330008, | |
/// <summary>dest = (NOT src) AND (NOT dest)</summary> | |
NOTSRCERASE = 0x001100A6, | |
/// <summary>dest = (source AND pattern)</summary> | |
MERGECOPY = 0x00C000CA, | |
/// <summary>dest = (NOT source) OR dest</summary> | |
MERGEPAINT = 0x00BB0226, | |
/// <summary>dest = pattern</summary> | |
PATCOPY = 0x00F00021, | |
/// <summary>dest = DPSnoo</summary> | |
PATPAINT = 0x00FB0A09, | |
/// <summary>dest = pattern XOR dest</summary> | |
PATINVERT = 0x005A0049, | |
/// <summary>dest = (NOT dest)</summary> | |
DSTINVERT = 0x00550009, | |
/// <summary>dest = BLACK</summary> | |
BLACKNESS = 0x00000042, | |
/// <summary>dest = WHITE</summary> | |
WHITENESS = 0x00FF0062, | |
/// <summary> | |
/// Capture window as seen on screen. This includes layered windows | |
/// such as WPF windows with AllowsTransparency="true" | |
/// </summary> | |
CAPTUREBLT = 0x40000000 | |
} | |
} |
If you are calling a method like below...
Bitmap img = ControlSnapshot.Snapshot(SessionKeywords.driver);
img.Save(@"E\shahul.png", ImageFormat.Png);Got Exception :
Exception thrown: 'System.Runtime.InteropServices.ExternalException' in System.Drawing.dllAny update ? on this?
@shahulshas94 the problem is most likely related to the SessionKeywords.driver
control not being valid. This type of question is better suited to stackoverflow.com - but before posting the question, ensure you can create a minimal reproducible example: https://stackoverflow.com/help/minimal-reproducible-example
@kevalkothari maybe you are passing in the wrong control. Are you passing in an instance of WinForms.ChromiumWebBrowser
?
I think I am passing correct reference, See details below.
Defining ChromiumWebBrowser below:
using CefSharp.MinimalExample.WinForms.Controls;
using CefSharp.WinForms;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace CefSharp.MinimalExample.WinForms
{
public partial class BrowserForm : Form
{
private readonly ChromiumWebBrowser browser;
...
Passing reference of that browser as below:
private void button1_Click(object sender, EventArgs e)
{
Bitmap img = ControlSnapshot.Snapshot(browser);
img.Save(@"C:\test\demo_new.bmp", ImageFormat.Png);
}
does it correct reference?
Hi @jankurianski ,
is it possible to hole page screenshot in cefsharp browser ?
@kevalkothari your saved file name extension should be .png
, not .bmp
.
@rajesh-smartwebtech you have to implement whole page screenshot yourself, by scrolling the page and taking multiple screenshots. I do not have example code for this.
If you are calling a method like below...
Bitmap img = ControlSnapshot.Snapshot(SessionKeywords.driver);
img.Save(@"E\shahul.png", ImageFormat.Png);
Got Exception :
Exception thrown: 'System.Runtime.InteropServices.ExternalException' in System.Drawing.dll
Any update ? on this?