简体   繁体   中英

Getting Bitmap from a window using win32 in c#: Where am I going wrong?

I need to retrieve screenshots of all the windows associated with a program. Right now I am just trying to get this to work for the main window.

Right after image = Image.FromHbitmap(mem); the error I get is "A generic error occurred in GDI+." I was trying to follow this tutorial

http://www.dreamincode.net/forums/topic/34831-taking-a-screen-shot-in-c%23/

I think I am messing up with my pointers, specifically at these three lines:

        IntPtr oldBmp = (IntPtr)Win32Stuff.SelectObject(mem, windowImage);
        Win32Stuff.BitBlt(mem, 0, 0, windowwidth, windowheight, windowDC, 0, 0, 13369376);
        Win32Stuff.SelectObject(mem, oldBmp);

If I understand correctly, oldBmp and mem point to windowImage, then mem points to the output from BitBlt, then I don't get this line where now mem points back to oldBmp. I tried commenting it out to see if this what was messing it up, but I get the same generic error.

Here is the code in its entirety:

public static class Win32Stuff
{
    [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
    public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
    [DllImport("user32.dll", EntryPoint = "GetWindowDC")]
    public static extern IntPtr GetWindowDC(IntPtr hWnd);

    [DllImport("gdi32", EntryPoint = "CreateCompatibleDC")]
    public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
    [DllImport("gdi32", EntryPoint = "CreateCompatibleBitmap")]
    public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight);
    [DllImport("gdi32", EntryPoint = "SelectObject")]
    public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
    [DllImport("gdi32", EntryPoint = "BitBlt")]
    public static extern bool BitBlt(IntPtr hDestDC, int X, int Y, int nWidth, int nHeight, IntPtr hSrcDC, int SrcX, int SrcY, int Rop);
    [DllImport("gdi32", EntryPoint = "DeleteDC")]
    public static extern IntPtr DeleteDC(IntPtr hDC);
}

    public static void getScreenShot()
    {
        int windowwidth;
        int windowheight;
        Bitmap image;

        IntPtr windowHandle = Process.GetProcessesByName("firefox")[0].MainWindowHandle;
        IntPtr windowDC = Win32Stuff.GetWindowDC(windowHandle);
        IntPtr mem = Win32Stuff.CreateCompatibleDC(windowDC);

        //need to change this
        windowheight = 1920;
        windowwidth = 1920;

        IntPtr windowImage = Win32Stuff.CreateCompatibleBitmap(windowDC, windowwidth, windowheight);
        IntPtr oldBmp = (IntPtr)Win32Stuff.SelectObject(mem, windowImage);
        Win32Stuff.BitBlt(mem, 0, 0, windowwidth, windowheight, windowDC, 0, 0, 13369376);
        //Win32Stuff.SelectObject(mem, oldBmp);
        image = Image.FromHbitmap(mem);

        Win32Stuff.DeleteDC(mem);
        Win32Stuff.ReleaseDC(windowHandle, mem);
    }

How can I see what the specific error is, and why won't Image.FromHbitmap() output a GDI bitmap from my pointer?

Change

image = Image.FromHbitmap(mem);

to

image = Image.FromHbitmap(windowImage);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM