C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,360 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,715 people online right now. Registration is fast and FREE... Join Now!




Screen Capturing

 
Reply to this topicStart new topic

> Screen Capturing

noorahmad
Group Icon



post 15 Apr, 2009 - 09:43 PM
Post #1


IPB 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

CODE

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

CODE

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.

CODE

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.
CODE

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
CODE

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
CODE

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);
            }
        }






This post has been edited by noorahmad: 15 Apr, 2009 - 10:37 PM


Attached File(s)
Attached File  Screen_Capture.zip ( 111.61k ) Number of downloads: 163
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

CheckersW
Group Icon



post 22 Oct, 2009 - 01:43 AM
Post #2
The .zip file is corrupted for me. Is that the same for anyone else?

Could the author please re-post it?
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/7/09 07:50PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month