0

I am using the following code to get the WindowRect for a process on my machine (been testing with the Windows 8.1 calculator).

    RECT rc;
    GetWindowRect(hWnd, out rc);

    var bmp = new Bitmap(rc.Right - rc.Left, rc.Bottom - rc.Top, PixelFormat.Format32bppArgb);
    var gfxBmp = Graphics.FromImage(bmp);
    IntPtr hdcBitmap = gfxBmp.GetHdc();
    PrintWindow(hWnd, hdcBitmap, 0);
    gfxBmp.ReleaseHdc(hdcBitmap);
    gfxBmp.Dispose();

But on Windows 8.1, the bitmap produced is cropped (in the case of the calculator, by about 100 pixels width and 150 height).

RECT is defined as:

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int Left;        // x position of upper-left corner
    public int Top;         // y position of upper-left corner
    public int Right;       // x position of lower-right corner
    public int Bottom;      // y position of lower-right corner
}

I've been pulling my hair out with this, can anyone spot what I'm doing wrong?

Cheers

Rich

Dutts
  • 5,781
  • 3
  • 39
  • 61
  • 2
    are you running on a high dpi setup, where scaling is something other than 100%? – John Gardner Mar 17 '15 at 19:04
  • Give example values for `rc` members. Is there difference in Windows 7? – i486 Mar 17 '15 at 19:05
  • 3
    My psychic debugger says that you didn't declare you program to [be dpi-aware](http://stackoverflow.com/a/13228495/17034). – Hans Passant Mar 17 '15 at 19:06
  • Ignore my last comment. What is **hWnd** and how are you getting it? –  Mar 17 '15 at 19:20
  • Thanks for your comments guys, good call on the dpi, I'll check when I'm back at my computer. – Dutts Mar 18 '15 at 11:00
  • @jp2code hWnd is coming from another MainWindowHandle on the process returned by a GetProcessById call. I assume that works and it is selecting the correct window for the process Id I use, it just crops it. I think the DPI setting might be a good clue so I'll check it out – Dutts Mar 18 '15 at 11:05

0 Answers0