Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / MFC
Article

Fast screen, window, region and print screen capture

Rate me:
Please Sign up or sign in to vote.
1.39/5 (73 votes)
4 Dec 2002CPOL 332.8K   5K   67   64
Screen Snaper is an fast screen, window, region and print screen capture

Sample Image - ScreenSnaper.gif

Introduction

Screen Snaper is an simple application for Screen capture and snapshot. The application use exported function from library SnaperHelper.dll compatible with VC, VB, Delphi and other languages that can use DLL.

Snapshot features

  • Get Desktop window
  • Get Window on the Desktop
  • Get Region of the desktop
  • Trap Print Screen key

In Window and Region capture mode, an helper show the zoomed position under the cursor and the current color. These modes also include keys shortcut and pop menu to permit switch between mode and much more...

About demo

This demo application include source code for clients in VC and VB. The source code of library SnaperHelper.dll is not provided.

History

  • 8 december 2002 - Demo project updated. Now include missing SnaperHelper.lib and SnaperHelperLib.h.

Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
Canada Canada

Comments and Discussions

 
GeneralPlease Help Pin
Shaheed Legion11-May-06 1:23
Shaheed Legion11-May-06 1:23 
AnswerRe: Please Help Pin
DiegoJancic25-May-06 10:35
DiegoJancic25-May-06 10:35 
I really don't know why this article is in the CODE project, if there is no CODE...
Use this, it's not perfect, but it's here!
<br />
/////////////////////////////////////////////////////////////////////////<br />
//////////////////// THIS CODE WASN'T CREATED BY ME, BUT I DON'T REMEMBER<br />
//////////////////// THE AUTHOR'S NAME. SORRY!<br />
/////////////////////////////////////////////////////////////////////////<br />
<br />
<br />
public Image CaptureScreen() <br />
		{<br />
			return CaptureWindow( User32.GetDesktopWindow() );<br />
		}<br />
		/// <summary><br />
		/// Creates an Image object containing a screen shot of a specific window<br />
		/// </summary><br />
		/// <param name="handle">The handle to the window. (In windows forms, this is obtained by the Handle property)</param><br />
		/// <returns></returns><br />
		public Image CaptureWindow(IntPtr handle)<br />
		{<br />
			// get te hDC of the target window<br />
			IntPtr hdcSrc = User32.GetWindowDC(handle);<br />
			// get the size<br />
			User32.RECT windowRect = new User32.RECT();<br />
			User32.GetWindowRect(handle,ref windowRect);<br />
			int width = windowRect.right - windowRect.left;<br />
			int height = windowRect.bottom - windowRect.top;<br />
			// create a device context we can copy to<br />
			IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);<br />
			// create a bitmap we can copy it to,<br />
			// using GetDeviceCaps to get the width/height<br />
			IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,width,height); <br />
			// select the bitmap object<br />
			IntPtr hOld = GDI32.SelectObject(hdcDest,hBitmap);<br />
			// bitblt over<br />
			GDI32.BitBlt(hdcDest,0,0,width,height,hdcSrc,0,0,GDI32.SRCCOPY);<br />
			// restore selection<br />
			GDI32.SelectObject(hdcDest,hOld);<br />
			// clean up <br />
			GDI32.DeleteDC(hdcDest);<br />
			User32.ReleaseDC(handle,hdcSrc);<br />
			// get a .NET image object for it<br />
			Image img = Image.FromHbitmap(hBitmap);<br />
			// free up the Bitmap object<br />
			GDI32.DeleteObject(hBitmap);<br />
			return img;<br />
		}<br />
		/// <summary><br />
		/// Captures a screen shot of a specific window, and saves it to a file<br />
		/// </summary><br />
		/// <param name="handle"></param><br />
		/// <param name="filename"></param><br />
		/// <param name="format"></param><br />
		public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format) <br />
		{<br />
			Image img = CaptureWindow(handle);<br />
			img.Save(filename,format);<br />
		}<br />
		/// <summary><br />
		/// Captures a screen shot of the entire desktop, and saves it to a file<br />
		/// </summary><br />
		/// <param name="filename"></param><br />
		/// <param name="format"></param><br />
		public void CaptureScreenToFile(string filename, ImageFormat format) <br />
		{<br />
			Image img = CaptureScreen();<br />
			img.Save(filename,format);<br />
		}<br />
<br />
		/// <summary><br />
		/// Helper class containing Gdi32 API functions<br />
		/// </summary><br />
		private class GDI32<br />
		{<br />
            <br />
			public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter<br />
			[DllImport("gdi32.dll")]<br />
			public static extern bool BitBlt(IntPtr hObject,int nXDest,int nYDest,<br />
				int nWidth,int nHeight,IntPtr hObjectSource,<br />
				int nXSrc,int nYSrc,int dwRop);<br />
			[DllImport("gdi32.dll")]<br />
			public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC,int nWidth, <br />
				int nHeight);<br />
			[DllImport("gdi32.dll")]<br />
			public static extern IntPtr CreateCompatibleDC(IntPtr hDC);<br />
			[DllImport("gdi32.dll")]<br />
			public static extern bool DeleteDC(IntPtr hDC);<br />
			[DllImport("gdi32.dll")]<br />
			public static extern bool DeleteObject(IntPtr hObject);<br />
			[DllImport("gdi32.dll")]<br />
			public static extern IntPtr SelectObject(IntPtr hDC,IntPtr hObject);<br />
		}<br />
<br />
		/// <summary><br />
		/// Helper class containing User32 API functions<br />
		/// </summary><br />
		private class User32<br />
		{<br />
			[StructLayout(LayoutKind.Sequential)]<br />
				public struct RECT<br />
			{<br />
				public int left;<br />
				public int top;<br />
				public int right;<br />
				public int bottom;<br />
			}<br />
			[DllImport("user32.dll")]<br />
			public static extern IntPtr GetDesktopWindow();<br />
			[DllImport("user32.dll")]<br />
			public static extern IntPtr GetWindowDC(IntPtr hWnd);<br />
			[DllImport("user32.dll")]<br />
			public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDC);<br />
			[DllImport("user32.dll")]<br />
			public static extern IntPtr GetWindowRect(IntPtr hWnd,ref RECT rect);<br />
		}

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.