Click here to Skip to main content
15,892,537 members
Articles / Multimedia / GDI+

Screen Capturing

Rate me:
Please Sign up or sign in to vote.
4.77/5 (46 votes)
24 Mar 20062 min read 261.6K   3.2K   224  
Capture screen contents without using any Win32 API calls, just using .NET (2.0) classes.
/*----------------------------------------------------------------------------------------

	A-Soft Ingenieurbüro

	Copyright © 1994 - 2007. All Rights reserved.

	Related Copyrights :

			Microsoft .NET Windows Forms V2.0 library.
			Copyright (C) 2004...2006 Microsoft Corporation,
			All rights reserved.


	FILE		:	NativeMethods.cs

	PROJECT		:	A-Soft Library
	SUB			:	Standard Library
 
	SYSTEM		:	Windows-XP, (Windows 2000), C# (.NET 2.0, Visual Studio.NET 2005)

	AUTHOR		:	Joachim Holzhauer
	
	DESCRIPTION	:	Map some definitions from WIN32 native interface
 
	VERSION		:	1.0 - 2006.01.31

----------------------------------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;

namespace ASFTBase
{
	/// <summary>
	/// Helper class which maps some WIN32 values
	/// </summary>
	[System.Security.SuppressUnmanagedCodeSecurity()]
	[System.Runtime.InteropServices.ComVisible( false )]
	internal sealed class NativeMethods
	{
		private NativeMethods() { }

		#region RECT structure

		[StructLayout( LayoutKind.Sequential )]
		internal struct RECT
		{
			public int left;
			public int top;
			public int right;
			public int bottom;

			public RECT( int left, int top, int right, int bottom )
			{
				this.left = left;
				this.top = top;
				this.right = right;
				this.bottom = bottom;
			}

			public Rectangle Rect 
			{ 
				get { return new Rectangle( this.left, this.top, this.right - this.left, this.bottom - this.top ); } 
			}

			public static RECT FromXYWH( int x, int y, int width, int height )
			{
				return new RECT( x, y, x + width, y + height );
			}

			public static RECT FromRectangle( Rectangle rect )
			{
				return new RECT( rect.Left, rect.Top, rect.Right, rect.Bottom );
			}
		}

		#endregion RECT structure

		/// <summary>
		/// Get a windows rectangle in a RECT structure
		/// </summary>
		/// <param name="hwnd">The window handle to look up</param>
		/// <param name="rect">The rectangle</param>
		/// <returns></returns>
		[DllImport( "user32.dll", ExactSpelling = true, CharSet = CharSet.Auto )]
		public static extern bool GetWindowRect( IntPtr hwnd, out RECT rect );

		/// <summary>
		/// The BringWindowToTop function brings the specified window to the top of the Z order. 
		/// If the window is a top-level window, it is activated. 
		/// If the window is a child window, the top-level parent window associated 
		/// with the child window is activated. 
		/// </summary>
		/// <param name="hWnd">Handle to the window to bring to the top of the Z order. </param>
		/// <returns>If the function succeeds, the return value is nonzero.
		/// If the function fails, the return value is zero. 
		/// To get extended error information, call GetLastError. 
		/// </returns>
		[DllImport( "user32.dll", ExactSpelling = true, CharSet = CharSet.Auto )]
		public static extern bool BringWindowToTop( IntPtr hWnd );


		/// <summary>
		/// Get a windows rectangle in a .NET structure
		/// </summary>
		/// <param name="hwnd">The window handle to look up</param>
		/// <returns>The rectangle</returns>
		public static Rectangle GetWindowRect( IntPtr hwnd )
		{
			RECT rect = new RECT();
			GetWindowRect( hwnd, out rect );
			return rect.Rect;
		}
	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Retired
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions