Screenshot of WinForm

J

jani

Hi all,
does anyone know how to take a screenshot of a winform using c#? I
have an Add-In program for MS Word in c#, and want to render the
WinForm to a gif image, then insert the screenshot into the word
document.
Does anyone have an idea?

THX

Jani
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi jani,

Unfortunately you can get away without using PInvoke. So here is the code
for capturing windows screenshots

struct ICONINFO
{
public bool fIcon;
public int xHotspot;
public int yHotspot;
public IntPtr hbmMask;
public IntPtr hbmColor;
} ;

private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;

public static implicit operator Rectangle(RECT rect)
{
return new Rectangle(rect.left, rect.top, rect.right - rect.left,
rect.bottom - rect.top);
}
}


private const int DCX_WINDOW = 0x00000001;
private const int DCX_CACHE = 0x00000002;
private const int DCX_LOCKWINDOWUPDATE = 0x00000400;
private const int SRCCOPY = 0x00CC0020;
private const int CAPTUREBLT = 0x40000000;



[DllImport("user32.dll")]
private static extern bool GetWindowRect (IntPtr hWnd, out RECT lpRect);

[DllImport("user32.dll")]
private static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hrgnClip, int
flags);

[DllImport("user32.dll")]
private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDc);

[DllImport("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest,
int nXDest,
int nYDest,
int nWidth,
int nHeight,
IntPtr hdcSrc,
int nXSrc,
int nYSrc,
int dwRop
);

[DllImport("user32")]
private static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO
piconinfo);

//if getCursor is true the cursor icon will be captured as well
public static Image TakeSnapshot(IntPtr handle, bool getCursor)
{


RECT tempRect;
GetWindowRect(handle, out tempRect);
Rectangle windowRect = tempRect;
IntPtr formDC = GetDCEx(handle, IntPtr.Zero, DCX_CACHE | DCX_WINDOW |
DCX_LOCKWINDOWUPDATE);
Graphics grfx = Graphics.FromHdc(formDC);

Bitmap bmp = new Bitmap(windowRect.Width, windowRect.Height, grfx);
using(grfx = Graphics.FromImage(bmp))
{
IntPtr bmpDC = grfx.GetHdc();

BitBlt(bmpDC, 0, 0, bmp.Width, bmp.Height, formDC, 0, 0, CAPTUREBLT |
SRCCOPY);
grfx.ReleaseHdc(bmpDC);
if(getCursor)
{
Cursor cur = Cursor.Current;
if(cur != null)
{
ICONINFO iconInfo;
GetIconInfo(cur.Handle, out iconInfo);
Point curPos = new Point(Cursor.Position.X - windowRect.X,
Cursor.Position.Y - windowRect.Y);
curPos.Offset(-iconInfo.xHotspot, -iconInfo.xHotspot);
cur.Draw(grfx, new Rectangle(curPos, cur.Size));
}
}
ReleaseDC(handle, formDC);
}
return bmp;
}


Note that only visible windows can be captured. Capturing windows that is
minimized, obscured by another or partially visible in some way is not easy
task and I'd say nearly imposible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top