Form Capture

  • Thread starter Thread starter Viktor
  • Start date Start date
Hi there...

I think this might help you... First of all we need to use PInvoke...
using System.Runtime.InteropServices;

Second we need to have some API functions' prototypes (GDI+) to call from
within our program

class APICalls {
[DllImport("Gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

[DllImport("Gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int
width, int height);

[DllImport("Gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr
hgdiobj);

[DllImport("Gdi32.dll")]
public static extern int BitBlt(IntPtr hdcdest, int nxdest, int
nydest, int nwidth, int nheight, IntPtr
hdcsrc, int nxsrc, int
nysrc, int raster);
}

and finally some code to "capture" our form (this could be placed in
button's click...)

Graphics graph = Graphics.FromHwnd(Handle); // Get a graphics object
from form's Hwnd
Region rgn = new Region(m_rectangle); // Get a Region. m_rectangle is a
Rectangle struct (client area)
graph.SetClip(rgn, CombineMode.Exclude); // Clip that region
IntPtr hdc = graph.GetHdc(); // Get a HDC (Handle to a Device Context)
from our graph object
IntPtr dchandle = APICalls.CreateCompatibleDC(hdc); // Create a
compatible DC
// Create a compatible bitmap from our HDC
IntPtr bmphandle = APICalls.CreateCompatibleBitmap(hdc,
m_rectangle.Width, m_rectangle.Height);
IntPtr select = APICalls.SelectObject(dchandle, bmphandle); // Select
that object (bitmap)
// Perform a BitBlt (Bit-Block Transfer) and get our snapshot from our
form
int success = APICalls.BitBlt(dchandle, m_rectangle.X, m_rectangle.Y,
m_rectangle.Width,
m_rectangle.Height, hdc,
0, 0, 13369376); // SRCCOPY
Image imagen = Bitmap.FromHbitmap(bmphandle); // Save our bitmap into an
Image object

// Dispose resources here...

try {
rgn.Dispose();
graph.Dispose();
} catch {}

and that's it... Hope this may help you...

Regards,
 
Back
Top