How To Make A Screen Capture Program In C#

All tutorials created in C# to be posted in here.
1 post Page 1 of 1
Contributors
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

hey guys

now i will give you a small guide to make a screen capture program in c-sharp first of all i want to say that this is not advanced program simply written ,

you need two buttons and button 1 will capture whole screen and button 2 will capture active window so now change the text and make the form small :lol:

this is my form

Image :o

now its time to code we are using two functions so now lets create the first function i will call it ActiveWindow so start with
Code: Select all
public void ActiveWindow()
{
now we need to get the bounds of the screen so
Code: Select all
rectangle.bounds = this.bounds;
now we need to set the bitmap object to the size of the active window so
Code: Select all
 using (Bitmap bmp = new Bitmap(bounds.Width, bounds.Height))
            {
now the next step is taking a snap of the screen so
Code: Select all
  using (Graphics gfx = Graphics.FromImage(bmp))
                {
                    gfx.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                }
now next is to save the screen snap as jpg in your desktop so
Code: Select all
String _path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "ActiveWindow.jpg");
                bmp.Save(_path);
            }

}
now the ActiveWindow function is finished


now lets go ahead for the next function i named it WholeScreen so like what we did before just use
Code: Select all
public void WholeScreen()
{
now to capture the wholescreen its better to hide our application so use
Code: Select all
this.hide();
and now lets the take the bounds of the screen
Code: Select all
Rectangle bounds = Screen.PrimaryScreen.Bounds;
now lets set the bitmap object to the size of the screen
Code: Select all
  using (Bitmap bmp = new Bitmap(bounds.Width, bounds.Height))
            {
p
after that lets snap your screen :lol:
Code: Select all
  using (Graphics gfx = Graphics.FromImage(bmp))
                {
                    gfx.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
                }
after that we need to save it to our desktop so
Code: Select all
 String _path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "WholeScreen.jpg");
                bmp.Save(_path);
            }               
  
now we will let the form to be visible
Code: Select all
this.show();
}
now functions are finished now lets make the functions work simply double click your button 1 and as i said before button 1 is for capturing whole screen so just write
Code: Select all
WholeScreen();
and now for button to capture active window use
Code: Select all
ActiveWindow();
now your done making a screenshot program so now test it and your images will be in desktop

Image

here is the full codes
Code: Select all
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;

namespace Screenshot
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        public void ActiveWindow()
        {
                     
            Rectangle bounds = this.Bounds;

                   using (Bitmap bmp = new Bitmap(bounds.Width, bounds.Height))
            {
                               using (Graphics gfx = Graphics.FromImage(bmp))
                {
                    gfx.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                }

              
                String _path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "ActiveWindow.jpg");
                bmp.Save(_path);
            }

                }
            
        public void WholeScreen()
        {
          
            this.Hide();
                      
            Rectangle bounds = Screen.PrimaryScreen.Bounds;

                      using (Bitmap bmp = new Bitmap(bounds.Width, bounds.Height))
            {
              
                using (Graphics gfx = Graphics.FromImage(bmp))
                {
                    gfx.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
                }
                        String _path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "WholeScreen.jpg");
                bmp.Save(_path);
            }
                                           this.Show();
        }

   
        private void button1_Click(object sender, EventArgs e)
        {
            WholeScreen();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ActiveWindow();
        }

       
    }
}

hope this is really useful and sorry for my bad english
Find my programs on Softpedia
1 post Page 1 of 1
Return to “C-Sharp Tutorials”