Screenshot in 2 Clicks using .NET – Now with More Options
Posted by: Shoban Kumar ,
on 4/29/2009,
in
Category WinForms & WinRT
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:
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.
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.
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.
Give me a +1 if you think it was a good article. Thanks!