Screenshot in 2 Clicks using .NET – Now with More Options

Posted by: Shoban Kumar , on 4/29/2009, in Category WinForms & WinRT
Views: 66413
Abstract: In my previous article, Screenshot in 2 Clicks using .NET, we learnt how we can capture the screenshot with just 2 clicks. But what if we want to capture a part of the screen? In this article we will see how we can capture a part of screen with just few more addition to the existing code.
Screenshot in 2 Clicks using .NET  – Now with More Options
In my previous article, Screenshot in 2 Clicks using .NET, we learnt how we can capture the screenshot with just 2 clicks. But what if we want to capture a part of the screen? In this article we will see how we can capture a part of screen with just few more addition to the existing code.
If you haven’t read my previous article Screenshot in 2 Clicks using .NET, then please do so as this article only shows the additional code required to add more functionality to the previous one.
Step 1: Open the project and change the following properties of Form1
Opacity : 20% , FormBorderStyle : SizableToolWindow , TopMost : True
Step 2: Add a new item to ‘ContextMenuStrip1’and name it as ‘Select Area’. Your new form should look similar to the one shown below:
Tool
Step 3: Double click the ‘Select Area’and add the following code
C#
private void selectAreaToolStripMenuItem_Click(object sender, EventArgs e)
{
    this.Show();
    this.WindowState = FormWindowState.Normal;
}
VB.NET
Private Sub SelectAreaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAreaToolStripMenuItem.Click
        Me.Show()
        Me.WindowState = FormWindowState.Normal
    End Sub
Step 4: Add the following code to Form1’s DoubleClickevent
C#
private void Form1_DoubleClick(object sender, EventArgs e)
{
    try
    {
        bmp = new Bitmap(this.Size.Width,
                               this.Size.Height,
                               PixelFormat.Format32bppArgb);
        graphics = Graphics.FromImage(bmp);
       
 
        graphics.CopyFromScreen(this.Location.X,
                                    this.Location.Y,
                                    0,
                                    0,
                                    Screen.PrimaryScreen.Bounds.Size,
                                    CopyPixelOperation.SourceCopy);
 
        SaveFileDialog saveDialog = new SaveFileDialog();
        saveDialog.Filter = "JPeg Image|*.jpg";
        saveDialog.Title = "Save Image as";
        saveDialog.ShowDialog();
        if (saveDialog.FileName != string.Empty)
            bmp.Save(saveDialog.FileName, ImageFormat.Jpeg);
        graphics.Dispose();
        bmp.Dispose();
        this.Hide();
        this.WindowState = FormWindowState.Minimized;
        
 
    }
    catch
    {
 
    }
    finally
    {
        graphics.Dispose();
        bmp.Dispose();
    }
 
}
VB.NET
    Private Sub Form1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
        Try
            bmp = New Bitmap(Me.Size.Width, Me.Size.Height, PixelFormat.Format32bppArgb)
 
 
            graphics = Graphics.FromImage(bmp)
            graphics.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy)
 
            Dim saveDialog As New SaveFileDialog
            saveDialog.Filter = "JPeg Image|*.jpg"
            saveDialog.Title = "Save Image as"
            saveDialog.ShowDialog()
            If saveDialog.FileName <> "" Then
                bmp.Save(saveDialog.FileName, ImageFormat.Jpeg)
            End If
            bmp.Dispose()
            graphics.Dispose()
 
            Me.Hide()
            Me.WindowState = FormWindowState.Minimized
        Catch ex As Exception
 
        Finally
            bmp = Nothing
            graphics = Nothing
        End Try
 
    End Sub
All we have done in the code shown above is changed the opacity of the form so that we have a transparent form like selector when ‘Select Area’ menu is clicked. We have also changed its border style so that we can resize the window. When the form is double clicked, we follow the steps of creating a screenshot similar to the one shown in my previous article Screenshot in 2 Clicks using .NET. However this time we changed the size of the new image to the size of the selector form and selected the coordinates as the starting x,y coordinates of the form. Shown below is an example of how we can take a screenshot of www.dotnetcurry.com using our code.
Select the area you want to capture using the selector form.
Screenshot
Once the area is selected, double click on the selector form and a Save Dialog will be presented to you. Save the screenshot to the desired location.
Screenshot 2
Note: If a border appears in the screenshot, you can remove it by reducing the Height and Width of the Bitmap.
I hope you find this article useful. The entire source code of this article can be downloaded from here.
What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
ShobanKumar is an ex-Microsoft MVP in SharePoint who currently works as a SharePoint Consultant. You can read more about his projects at http://shobankumar.com. You can also follow him on twitter @shobankr


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Barry on Wednesday, April 29, 2009 8:37 AM
password password - yawn... what ever happened to freedom of information!!?!??!
Comment posted by Carol on Thursday, April 30, 2009 9:26 AM
All information is free over here. The password is to prevent spurious downloads and make sure that registered users or subscribers get access to it. It's just a one time process and worth it.
Comment posted by polas on Tuesday, March 20, 2012 10:42 AM
Hello I have an idea and can you add a circle with a choice of the area in order to draw the image and save on everything.
Then you would have a really good program :)?

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

FREE .NET MAGAZINES

Free DNC .NET Magazine

Tags

JQUERY COOKBOOK

jQuery CookBook