Page 1 of 1

Screen Capturing

#1 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Posted 15 April 2009 - 10:43 PM

Posted Image
In this tutorial I will be showing you how you would go about Getting Image from Screen in C#. Reading and knowing this Project is very Simple, before we get into any code, you need to understand that this tutorial is not to advanced tutorial, using System. Drawing and System.IO namespace. If you are not familiar with this item, I suggest you study them before attempting this tutorial.

Getting Image from Screen is not so advanced but it is in intermediate level. This tutorial is not to long it is too short. And so this tutorial is not taking you time.
So Letís Began:
First of All Import The namespace

using System.IO;
using System.Drawing; 



The System.Drawing namespace provides access to GDI+ basic graphics functionality. More advanced functionality is provided in the System.Drawing.Drawing2D, System.Drawing.Imaging, and System.Drawing.Text namespaces.
The Graphics class provides methods for drawing to the display device. Classes such as Rectangle and Point encapsulate GDI+ primitives. The Pen class is used to draw lines and curves, while classes derived from the abstract class Brush are used to fill the interiors of shapes.

Create the variables

int _ImageCounter,i;
String _Path = @"C:\Images\";



The System.Drawing.Bitmap namespace:
Encapsulates a GDI+ bitmap, which consists of the pixel data for a graphics image and its attributes. A Bitmap is an object used to work with images defined by pixel data.

private void GetScreenImage(Rectangle _Rectangle)
{
_ImageCounter++;
Size _Size = new Size(_Rectangle.Width,_Rectangle.Height);
using (Bitmap _Bitmap = new Bitmap(_Size.Width,_Size.Height ))
{
using (Graphics _Graphics = Graphics.FromImage(_Bitmap))
{
_Graphics.CopyFromScreen(Point.Empty, Point.Empty, _Size);
String iDate;
iDate = DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Millisecond.ToString();
iDate+= "-" + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString();
_Bitmap.Save(@"C:\Images\Image" + _ImageCounter + iDate +".PNG");
}			  



Here we are passing one parameter System.Drawing.Rectangle:
Represents a rectangle


OK lets now import the images to our form, here we need a ListView.
private void FillListView()
		{
			try
			{
				listView1.Items.Clear();
				listView1.LargeImageList = imageList1;
				foreach (String str in Directory.GetFiles(_Path, "*.PNG"))
				{
					String ImageNameOnly = Path.GetFileName(str);
					Image _Image = Image.FromFile(str);
					imageList1.Images.Add(str, _Image);
					String FileName = Path.GetFileNameWithoutExtension(str);
					listView1.Items.Add(ImageNameOnly ,str  );
				}
			}


And then Start a Timer to get screen images time to time
private void timer1_Tick(object sender, EventArgs e)
		{
			i++;
			if (i == 30)
			{
				GetNow();
				i = 0;
				FillListView();
				prog.Value = 0;
			}
			prog.Maximum = 30;
			prog.Value++;
			toolStripStatusLabel1.Text=i + "/" +30;
		}


After that if user double clicked on ListView->Image then the image will open
private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			try
			{
				ListView.SelectedListViewItemCollection obj = listView1.SelectedItems;
				if (obj.Count > 0)
				{
					String aPath = obj[0].ImageKey;
					System.Diagnostics.Process.Start(aPath);
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

Attached File(s)


This post has been edited by noorahmad: 15 April 2009 - 11:37 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Screen Capturing

#2 CheckersW   User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 198
  • Joined: 04-April 09

Posted 22 October 2009 - 02:43 AM

The .zip file is corrupted for me. Is that the same for anyone else?

Could the author please re-post it?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1