473,395 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,395 developers and data experts.

Capture Screen (The Express Way in 5 Lines)

Atran
319 100+
Hello Everyone.
In this article: You will know to capture the screen in 2 ways:
1)- Capture full screen.
2)- Capture region.

Let's Begin:
First make a new Windows Application project.
And make sure your program uses these namespaces:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4. using System.Windows.Forms;
  5.  
Now, we will begin writing the code:
1)- Create a new bitmap object.
Expand|Select|Wrap|Line Numbers
  1. //Bitmap screenBitmap = new Bitmap(int width, int height, PixelFormat);
  2. Bitmap screenBitmap = new Bitmap(1024, 768, PixelFormat.Format32bppArgb);
  3.  
2)- Create a new rectangle object.
Expand|Select|Wrap|Line Numbers
  1. //screenRegion will grab the size of your current screen.
  2. Rectangle screenRegion = Screen.AllScreens[0].Bounds;
  3.  
3)- Create a new graphics object.
Expand|Select|Wrap|Line Numbers
  1. Graphics screenGraphics = Graphics.FromImage(screenBitmap);
  2.  
4)- Copy the image from the screen.
Expand|Select|Wrap|Line Numbers
  1. //screenGraphics will copy the image from the screen.
  2. screenGraphics.CopyFromScreen(screenRegion.Left, screenRegion.Top, 0, 0, screenRegion.Size);
  3.  
5)- Save the image:
Expand|Select|Wrap|Line Numbers
  1. screenBitmap.Save(@"C:\Screen.jpg", ImageFormat.Jpeg);
  2.  
Here is the code:
Expand|Select|Wrap|Line Numbers
  1. Bitmap screenBitmap = new Bitmap(1024, 768, PixelFormat.Format32bppArgb);
  2. Rectangle screenRegion = Screen.AllScreens[0].Bounds;
  3.  
  4. // It will copy the current screep image to the bitmap image.
  5. Graphics screenGraphics = Graphics.FromImage(screenBitmap);
  6. screenGraphics.CopyFromScreen(screenRegion.Left, screenRegion.Top, 0, 0, screenRegion.Size); 
  7.  
  8. screenBitmap.Save(@"c:\test.jpg", ImageFormat.Jpeg);
  9.  

You can also capture a part of the screen (region):
Expand|Select|Wrap|Line Numbers
  1. /* The Code was:
  2.    screenGraphics.CopyFromScreen(screenRegion.Left, screenRegion.Top, 0, 0, screenRegion.Size); 
  3. */
  4.  
  5. //See the difference.
  6.  
  7. //screenGraphics.CopyFromScreen(intSourceX, intSourceY......);
  8. screenGraphics.CopyFromScreen(200, 300, 0, 0, screenRegion.Size);
  9.  
Hope this help you.
Jun 21 '07 #1
5 8978
Vidhura
99
Hey Atran

Thanks!

Its nice stuff with good and simple explanation
Jul 5 '07 #2
Atran
319 100+
Hey Atran

Thanks!

Its nice stuff with good and simple explanation
You're welcome, enjoy.......
Jul 5 '07 #3
Great tip
this is something that I can use daily with a few modifications.
I do not use C# though so I thought that you would not mind if I posted the VB code
Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.drawing
  3. Imports System.Drawing.Imaging
  4. Imports System.Windows.Forms
  5.         Dim screenBitmap As New Bitmap(1024, 768, PixelFormat.Format32bppArgb)
  6.         Dim screenRegion As Rectangle = Screen.AllScreens(0).Bounds
  7.  
  8.         ' It will copy the current screep image to the bitmap image.
  9.         Dim screenGraphics As Graphics = Graphics.FromImage(screenBitmap)
  10.         screenGraphics.CopyFromScreen(screenRegion.Left, screenRegion.Top, 0, 0, screenRegion.Size)
  11.  
  12.         screenBitmap.Save("c:\test.jpg", ImageFormat.Jpeg)
Jul 27 '07 #4
PKV
1
this was really good one!!
Thanx
Jul 31 '07 #5
Hi ,
The above code is working gr8 in development machine.Once i deploy it in the server its giving 'Handle Invalid' error.
Any suggestion
Mar 14 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Adrian Lumsden | last post by:
Hello, I have an app where I have to screen scrape to capture an image from a JMF film player. The user is given a dialog with a list of frames that can be exported as images. If the one they...
3
by: Me Mine | last post by:
i, I have trying to code a small console app that will allow a user to select a window and then create a screen capture of the window. I haven't been able to figure out how to do the screen...
1
by: xc | last post by:
Greetings. I encountered a wield problem when grabing screen images. Sometimes in some computers I can capture the screen, but other times not so. In some computer I cannot capture the screen...
10
by: Rattanak Song | last post by:
Hi, I'm trying to build a database which can capture a still image from a digital camera or web cam and import to the database via an Object or something like that. Any help would be very...
0
by: hellokitty | last post by:
GetDC/GetWindowDC can only capture on screen,visually, stuff and not for screen in background If I execute an exe in other desktop, by CreateDesktop/SwitchDesktop, it requires to actually...
2
by: Pohihihi | last post by:
Hello NG, How can I take a snapshot of full screen (of a area of screen as needed) using any dotNet language? Thank you, Po
6
by: Clif | last post by:
Hi, I would like to find out exactly what is being sent when a web page uses the post request method. My thought was to use the webbrowser control together with the HTTPWebRequest class, but am...
3
by: Tuy Solang | last post by:
Hi Everyone, I am just starting to learn Java from O'Reilly CD. I could not find any example that is related to screen capture. Is it possible in Java like in Microsoft C? Thanks,
2
by: raylopez99 | last post by:
Beware newbies: I spent a day before I figured this out: copying a bitmap (image) file to file is not quite like copying a text file--you have to do some tricks (see below), like using a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.