Screenshot of WinForm

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jani

    Screenshot of WinForm

    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
  • Stoitcho Goutsev \(100\) [C# MVP]

    #2
    Re: Screenshot of WinForm

    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_LOCKWINDOWU PDATE = 0x00000400;
    private const int SRCCOPY = 0x00CC0020;
    private const int CAPTUREBLT = 0x40000000;



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

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

    [DllImport("user 32.dll")]
    private static extern int ReleaseDC(IntPt r hWnd, IntPtr hDc);

    [DllImport("gdi3 2.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("user 32")]
    private static extern bool GetIconInfo(Int Ptr hIcon, out ICONINFO
    piconinfo);

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


    RECT tempRect;
    GetWindowRect(h andle, out tempRect);
    Rectangle windowRect = tempRect;
    IntPtr formDC = GetDCEx(handle, IntPtr.Zero, DCX_CACHE | DCX_WINDOW |
    DCX_LOCKWINDOWU PDATE);
    Graphics grfx = Graphics.FromHd c(formDC);

    Bitmap bmp = new Bitmap(windowRe ct.Width, windowRect.Heig ht, grfx);
    using(grfx = Graphics.FromIm age(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.Po sition.X - windowRect.X,
    Cursor.Position .Y - windowRect.Y);
    curPos.Offset(-iconInfo.xHotsp ot, -iconInfo.xHotsp ot);
    cur.Draw(grfx, new Rectangle(curPo s, cur.Size));
    }
    }
    ReleaseDC(handl e, 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.

    --
    HTH
    Stoitcho Goutsev (100) [C# MVP]


    "jani" <jani2004@gmx.n et> wrote in message
    news:ac71e2b5.0 410280502.ca7e7 ae@posting.goog le.com...[color=blue]
    > 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[/color]


    Comment

    Working...