Connecting Tech Pros Worldwide Help | Site Map
Reply
 
LinkBack Thread Tools Search this Thread
  #1  
Old June 21st, 2007, 09:36 AM
Atran's Avatar
Needs Regular Fix
 
Join Date: May 2007
Location: Sweden
Age: 16
Posts: 323
Default Capture Screen (The Express Way in 5 Lines)

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.
Reply



  #2  
Old July 5th, 2007, 10:00 AM
Member
 
Join Date: May 2007
Location: Chennai
Posts: 99
Default

Hey Atran

Thanks!

Its nice stuff with good and simple explanation
Reply
  #3  
Old July 5th, 2007, 10:22 PM
Atran's Avatar
Needs Regular Fix
 
Join Date: May 2007
Location: Sweden
Age: 16
Posts: 323
Default

Quote:
Originally Posted by Vidhura
Hey Atran

Thanks!

Its nice stuff with good and simple explanation
You're welcome, enjoy.......
Reply
  #4  
Old July 27th, 2007, 08:25 PM
Newbie
 
Join Date: Jun 2007
Location: I moved to North Carolina after spending 32 years in California
Posts: 12
Default

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)
Reply
  #5  
Old July 31st, 2007, 12:25 PM
PKV PKV is offline
Newbie
 
Join Date: Jul 2007
Posts: 1
Default

this was really good one!!
Thanx
Reply
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.