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)
-
Screen_Capture.zip (111.61K)
Number of downloads: 828
This post has been edited by noorahmad: 15 April 2009 - 11:37 PM



MultiQuote


|