4

Anyone know why this keeps returning a blank image? I found this function here.

I pass in a handle to a notepad process/window.

    public static Image DrawToBitmap(IntPtr handle)
    {
        Bitmap image = new Bitmap(500, 500, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        using (Graphics graphics = Graphics.FromImage(image))
        {
            IntPtr hDC = graphics.GetHdc();
            SendMessage(new HandleRef(graphics, handle), WM_PRINT, hDC, PRF_CHILDREN);
            graphics.ReleaseHdc(hDC);
        }
        return image;
    } 

I make use of the above like so:

Image myimage = DrawToBitmap(handle);

myimage.Save("C:\\here.png", ImageFormat.Png);

Thanks all for any help

Update

I think I have managed to get the error code from SendMessage using the below:

if (SendMessage(handle, WM_PRINT, hDC, PRF_CLIENT))
{
    Console.WriteLine("Success!");
}
else
{
    Console.WriteLine("Error: " + Marshal.GetLastWin32Error());
}

I get an error of 8 and I found it means not enough memory? I have over 500MB free! Maybe I am understanding this wrong?

Kay
  • 845
  • 7
  • 21
  • 33
  • Could it be a permissions issue? Are you running your .exe as Admin or running visual studio as Admin if you are debugging from VS? – Bala R Apr 17 '11 at 20:57
  • @Bala R - I am using Visual Studio 2010 and I am running it as an Admin. I also launched the notepad window. – Kay Apr 17 '11 at 20:58
  • 1
    I can't get your code to work, always pure black output. However, [this post tells how to capture a window's graphics](http://stackoverflow.com/questions/1163761/c-capture-screenshot-of-active-window). – Chris O Apr 18 '11 at 01:35
  • I tried it, and the linked code does not work when passing `this.Handle`. – pickypg Apr 18 '11 at 03:22
  • @Chris - I was using that already, but I need a way without relying on the windows UI i.e. I can't capture from screen it has to be via memory. – Kay Apr 18 '11 at 08:38
  • @Kay, screen capture, which can be done via GDI, GDI+, WPF, OpenGL, DirectX, and whatever else, all relies on a rendered screen from a user desktop session. (There might be an alternative, through the way Citrix thin clients work, but I'm completely guessing on that). – Chris O Apr 18 '11 at 13:14
  • @Chris - I don't think the above function relies on the rendered screen since the previous question I found this function was trying to avoid making use of the windows UI. Do you think this method fits into the categories you listed? – Kay Apr 18 '11 at 13:29
  • *"I can't capture from screen it has to be via memory."* This sentence makes no sense. Every method (including [this one](http://stackoverflow.com/questions/1163761/c-capture-screenshot-of-active-window/1163770#1163770)) will capture from memory, because what's rendered on the monitor is stored in memory. You certainly don't ask the monitor what it's rendering - you get it from the video memory. – BlueRaja - Danny Pflughoeft Apr 26 '11 at 17:52

2 Answers2

2

Instead of SendMessage you could just use PrintWindow

Here is a sample

public static Image DrawToBitmap(IntPtr handle)
{
    RECT rect = new RECT();
    GetWindowRect(handle, ref rect);

    Bitmap image = new Bitmap(rect.Right - rect.Left, rect.Bottom - rect.Top);

    using (Graphics graphics = Graphics.FromImage(image))
    {
        IntPtr hDC = graphics.GetHdc();
        PrintWindow(new HandleRef(graphics, handle), hDC, 0);
        graphics.ReleaseHdc(hDC);
    }

    return image;
}

#region Interop

[DllImport("USER32.DLL")]
private static extern bool PrintWindow(HandleRef hwnd, IntPtr hdcBlt, int nFlags);

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}

#endregion
prashanth
  • 2,059
  • 12
  • 13
  • I've been using the above and its the closest that has worked but I get some black images with visible window bar and file menu, its weird but that merits a new question. – Kay Apr 29 '11 at 11:00
0

Try passing PRF_CLIENT or PRF_NONCLIENT to SendMessage in addition to PRF_CHILDREN.

Also, how are you deriving the handle that you are passing to the function?

PBJ
  • 1
  • I just tried your suggestion but it didn't work unfortunatly. I derive the handle by using `Process.GetProcessesByName` with get handle by window title name `p.MainWindowTitle`. I have also tried firing of notepad myself and getting a direct handle to it and that hasn't worked. I can't figure out what to try even! – Kay Apr 18 '11 at 14:26