How To: PrintWindow into a Bitmap

AlexCode

Senior Contributor
Joined
Jul 7, 2003
Messages
931
Location
Portugal
Hi...
The subject says it all...

I need to print a window into a bitmap object (just in memory), and only the client area if possible...

I tryed to use the PrintWindow API but the example I have is kind of diferent from what I need and I can't figure out what to do...
See the example at: AllAPI

Any help would be greatlly apreciated...

Alex :p
 
Alex:
Not sure if you already found an answer to this one.......but I had fun figuring it out so I'll post how I did it anyways :D . I started with a code sample from CSharpHelp and then tweaked it a bit so that it created a bitmap of a specific Form.
Code:
// This is the code from my test Form....just a Button and a PictureBox
private void button1_Click(object sender, System.EventArgs e)
{
    this.pictureBox1.Image = CaptureScreen.GetDesktopImage(this.Left, this.Top, this.Height, this.Width);
}

public class CaptureScreen
{
    public static Bitmap GetDesktopImage(int formX, int formY, int formHeight, int formWidth)
    {
      
        //Variable to keep the handle of the btimap.
        IntPtr m_HBitmap;

        //Variable to keep the refrence to the desktop bitmap.
        System.Drawing.Bitmap bmp=null;

        //Here we get the handle to the desktop device context.
        IntPtr hDC = PlatformInvokeUSER32.GetDC(PlatformInvokeUSER32.GetDesktopWindow()); 

        //Here we make a compatible device context in memory for screen device context.
        IntPtr hMemDC = PlatformInvokeGDI32.CreateCompatibleDC(hDC);
      
        //We create a compatible bitmap of screen size and using screen device context.
        m_HBitmap = PlatformInvokeGDI32.CreateCompatibleBitmap(hDC, formWidth, formHeight);
      
      
        //As m_HBitmap is IntPtr we can not check it against null. For this purspose IntPtr.Zero is used.
        if (m_HBitmap!=IntPtr.Zero)
        {
	//Here we select the compatible bitmap in memeory device context and keeps the refrence to Old bitmap.
	IntPtr hOld = (IntPtr) PlatformInvokeGDI32.SelectObject(hMemDC,  m_HBitmap);

	//We copy the Bitmap to the memory device context.
	PlatformInvokeGDI32.BitBlt(hMemDC, 0, 0, formWidth, formHeight, hDC, formX, formY,PlatformInvokeGDI32.SRCCOPY);

	//We select the old bitmap back to the memory device context.
	PlatformInvokeGDI32.SelectObject(hMemDC, hOld);

	//We delete the memory device context.
	PlatformInvokeGDI32.DeleteDC(hMemDC);

	//We release the screen device context.
	PlatformInvokeUSER32.ReleaseDC(PlatformInvokeUSER32.GetDesktopWindow(), hDC);

	//Image is created by Image bitmap handle and assigned to Bitmap variable.
	bmp=System.Drawing.Image.FromHbitmap(m_HBitmap); 

	//Delete the compatible bitmap object. 
	PlatformInvokeGDI32.DeleteObject(m_HBitmap);

	return bmp;
        }
//If m_HBitmap is null retunrn null.
        return null;
    }

}


/// 
/// This class shall keep the GDI32 APIs used in our program.
/// 
public class PlatformInvokeGDI32
{

        public  const int SRCCOPY = 13369376;
		
        [DllImport("gdi32.dll",EntryPoint="DeleteDC")]
        public static extern IntPtr DeleteDC(IntPtr hDc);

        [DllImport("gdi32.dll",EntryPoint="DeleteObject")]
        public static extern IntPtr DeleteObject(IntPtr hDc);

        [DllImport("gdi32.dll",EntryPoint="BitBlt")]
        public static extern bool BitBlt(IntPtr hdcDest,int xDest,int yDest,int wDest,int hDest,IntPtr hdcSource,int xSrc,int ySrc,int RasterOp);

        [DllImport ("gdi32.dll",EntryPoint="CreateCompatibleBitmap")]
        public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc,int nWidth, int nHeight);

        [DllImport ("gdi32.dll",EntryPoint="CreateCompatibleDC")]
        public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

        [DllImport ("gdi32.dll",EntryPoint="SelectObject")]
        public static extern IntPtr SelectObject(IntPtr hdc,IntPtr bmp);
		
}

/// 
/// This class shall keep the User32 APIs used in our program.
/// 
public class PlatformInvokeUSER32
{
        public  const int SM_CXSCREEN=0;
        public  const int SM_CYSCREEN=1;
    
        [DllImport("user32.dll", EntryPoint="GetDesktopWindow")]
        public static extern IntPtr GetDesktopWindow();

        [DllImport("user32.dll",EntryPoint="GetDC")]
        public static extern IntPtr GetDC(IntPtr ptr);

        [DllImport("user32.dll",EntryPoint="GetSystemMetrics")]
        public static extern int GetSystemMetrics(int abc);

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

}
 
Back
Top