Skip to content

Instantly share code, notes, and snippets.

@jankurianski
Created December 2, 2014 07:18
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jankurianski/f3419d4580517516c24b to your computer and use it in GitHub Desktop.
Save jankurianski/f3419d4580517516c24b to your computer and use it in GitHub Desktop.
Takes a screenshot of a Windows Forms control using BitBlt, making it work with CefSharp's WinForms.ChromiumWebBrowser
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
}
}
@rajesh-smartwebtech
Copy link

Hi @jankurianski ,
is it possible to hole page screenshot in cefsharp browser ?

@jankurianski
Copy link
Author

@kevalkothari your saved file name extension should be .png, not .bmp.

@jankurianski
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment