.Net Stats:

How to support the site


Site Wide Message: (current site time 9/9/2010 8:49:46 AM EDT)
  • We want your input! One of our sponsors wants to know your opinion about development related issues. Click here to tell us what you think.
  • Are you an emerging/young developer (aged 18-30)? If so, would you like the chance to affect future developer tools and products?
    If so, then click here to give your feedback.
 

Capture Screen (C#)

Print
Email
VB icon
Submitted on: 10/14/2003 9:00:35 PM
By: Jon Davis  
Level: Intermediate
User Rating: By 2 Users
Compatibility:C#

Users have accessed this code  9867 times.
 
author picture
(About the author)
 
     This is an almost direct C# translation of the VB.NET "Capture Screen" submission by Masoud (M.D) at http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=309&lngWId=10
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Capture Screen (C#)
// Description:This is an almost direct C# translation of the VB.NET "Capture Screen" submission by Masoud (M.D) at http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=309&lngWId=10
// By: Jon Davis
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1663&lngWId=10//for details.//**************************************

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace PlanetSourceCode
{
	/// <summary>
	/// Summary description for ScreenShotView.
	/// </summary>
	public class ScreenShotView : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		public ScreenShotView()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
//Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As String) As Integer
		[DllImport("gdi32.dll")]
		private static extern int CreateDC(string lpDriverName, string lpDeviceName, string lpOutput, string lpInitData);
//
//Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Integer) As Integer
		[DllImport("gdi32.dll")]
		private static extern int CreateCompatibleDC(int hDC);
//
//Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer
		[DllImport("gdi32.dll")]
		private static extern int CreateCompatibleBitmap(int hDC, int nWidth, int nHeight);
//
//Private Declare Function GetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" (ByVal hdc As Integer, ByVal nIndex As Integer) As Integer
		[DllImport("gdi32.dll")]
		private static extern int GetDeviceCaps(int hdc, int nIndex);
//
//Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Integer, ByVal hObject As Integer) As Integer
		[DllImport("gdi32.dll")]
		private static extern int SelectObject(int hDC, int hObject);
//
//Private Declare Function BitBlt Lib "GDI32" (ByVal srchDC As Integer, ByVal srcX As Integer, ByVal srcY As Integer, ByVal srcW As Integer, ByVal srcH As Integer, ByVal desthDC As Integer, ByVal destX As Integer, ByVal destY As Integer, ByVal op As Integer) As Integer
		[DllImport("gdi32.dll")]
		private static extern int BitBlt(int srchDC, int srcX, int srcY, int srcW, int srcH, int desthDC, int destX, int destY, int op);
//
//Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Integer) As Integer
		[DllImport("gdi32.dll")]
		private static extern int DeleteDC(int hDC);
//
//Private Declare Function DeleteObject Lib "GDI32" (ByVal hObj As Integer) As Integer
		[DllImport("gdi32.dll")]
		private static extern int DeleteObject(int hObj);
//
//Const SRCCOPY As Integer = &HCC0020
		const int SRCCOPY = 13369376;
//
//Private oBackground As Bitmap
		//private Bitmap oBackground;
//Private FW, FH As Integer
		private int FW, FH;
		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// ScreenShotView
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(416, 342);
			this.Name = "ScreenShotView";
			this.Text = "ScreenShotView";
			this.Load += new System.EventHandler(this.ScreenShotView_Load);
		}
		#endregion
//		Protected Sub CaptureScreen()
		protected Bitmap CaptureScreen() {
//
//Dim hSDC, hMDC As Integer
			int hSDC, hMDC;
//Dim hBMP, hBMPOld As Integer
			int hBMP, hBMPOld;
//Dim r As Integer
			int r;
//
//hSDC = CreateDC("DISPLAY", "", "", "")
			hSDC = CreateDC("DISPLAY", "", "", "");
//hMDC = CreateCompatibleDC(hSDC)
			hMDC = CreateCompatibleDC(hSDC);
//
//FW = GetDeviceCaps(hSDC, 8)
			FW = GetDeviceCaps(hSDC, 8);
//FH = GetDeviceCaps(hSDC, 10)
			FH = GetDeviceCaps(hSDC, 10);
//hBMP = CreateCompatibleBitmap(hSDC, FW, FH)
			hBMP = CreateCompatibleBitmap(hSDC, FW, FH);
//
//hBMPOld = SelectObject(hMDC, hBMP)
			hBMPOld = SelectObject(hMDC, hBMP);
//r = BitBlt(hMDC, 0, 0, FW, FH, hSDC, 0, 0, 13369376)
			r = BitBlt(hMDC, 0, 0, FW, FH, hSDC, 0, 0, SRCCOPY);
//hBMP = SelectObject(hMDC, hBMPOld)
			hBMP = SelectObject(hMDC, hBMPOld);
//
//r = DeleteDC(hSDC)
			r = DeleteDC(hSDC);
//r = DeleteDC(hMDC)
			r = DeleteDC(hMDC);
//
//oBackground = Image.FromHbitmap(New IntPtr(hBMP))
			Bitmap ret = Image.FromHbitmap(new IntPtr(hBMP));
//DeleteObject(hBMP)
			DeleteObject(hBMP);
			return ret;
//
//End Sub
			}
		private void ScreenShotView_Load(object sender, System.EventArgs e) {
			this.BackgroundImage = CaptureScreen();
		}
	}
}


Other 9 submission(s) by this author

 

 
 Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:
 
Your Vote!

What do you think of this code(in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
10/19/2003 7:37:29 AMMasoud (M.D)

Thank you for your attention.
(If this comment was disrespectful, please report it.)

 
Add Your Feedback!

Note:Not only will your feedback be posted, but an email will be sent to the code's author from the email account you registered on the site, so you can correspond directly.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
To post feedback, first please login.