Capture screen image by Screen shot in C#

| August 10, 2010 | 0 Comments

As the name suggests you can take screenshot by application Screen shot !!!

Snapshot of Screenshot

Snapshot of Screenshot

Pre-Requisites

  • Visual Studio 2005 or more
  • .NET Framework 2.0

Concepts Covered

  1. Dynamic creation of Image on basis of Screen (represents Display Device) width and height.
  2. Handling most general error The process  cannot access  the file because it is being used by another  process.
  3. Running application in tray by NotifiyIcon, showing BaloonTipText& handling mouse events.

    Screenshot in Tray

  4. Handling form resizing events.
  5. Adding scrollbars to PictureBox on basis of picture size.

Code Snippet

How application works? When you execute application first times it will run in tray mode. And on right clicking application in tray mode you get two options Capture and Exit. As you click on Capture option you get your screenshot in application as shown in above image with balloon text as Congratulations Screenshot saved successfully! (saved in MyDocuments folder). Clicking left click on application in tray opens application main window.

Check out the code snippet below.

MyScreenshot.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;

namespace ScreenShot
{
    public partial class MyScreenshot : Form, IDisposable
    {
        // Bitmap declaration
        Bitmap bmp;

        public MyScreenshot()
        {
           InitializeComponent();
           this.myNotifyIcon.MouseDown += new MouseEventHandler(myNotifyIcon_MouseDown);
        }

        private void btnCapture_Click(object sender, EventArgs e)
        {
            // Setting AutoScroll to true to see full screeenshot
            this.panelPicture.AutoScroll = true;
            if (bmp != null)
            {
                bmp.Dispose();
                GC.SuppressFinalize(bmp);
            }

            // Getting Screen(Display Device) width and height
            int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
            int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;

            using (Bitmap bmpSShot = new Bitmap(screenWidth, screenHeight))
            {
                using (Graphics graphic = Graphics.FromImage(bmpSShot))
                {
                    graphic.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
                }

                FileStream fs;
                try
                {
                    fs = new FileStream("C:\\Save.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
                }
                finally
                {
                    fs.Close();
                }

                // Tracking path of MyDocuments
                string imagePath=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Screen.jpeg";
                bmpSShot.Save(imagePath,ImageFormat.Jpeg);

                if (File.Exists(imagePath))
                {
                    bmp = new Bitmap(imagePath);
                    this.Show();
                    this.Activate();
                    WindowState = FormWindowState.Normal;
                    this.screenPicture.Image = bmp;
                    this.myNotifyIcon.ShowBalloonTip(50000, "Congratulations", "Screenshot saved successfully!", ToolTipIcon.Info);
                }
            }
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                this.Hide();
            }
        }

        private void myNotifyIcon_MouseClick(object sender, MouseEventArgs e)
        {
           myNotifyIcon_MouseDown(this, e);
        }

        private void captureToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.screenPicture.Image = null;
            this.btnCapture_Click(this, null);
        }

        void myNotifyIcon_MouseDown(object sender, MouseEventArgs e)
        {
            // If Mouse Left button click resize Window to Normal
            if (e.Button == MouseButtons.Left)
            {
                this.Activate();
                WindowState = FormWindowState.Normal;
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.btnExit_Click(this, null);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Making notify icon invisible in tray
            this.myNotifyIcon.Visible = false;
        }
    }
}

Download Source Code

Category: .NET, C#

Leave a Reply

%d bloggers like this: