Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

[WPF] Screen Cutter

0.00/5 (No votes)
16 Apr 2013 1  
WPF application to capture screen.

Introduction 

A very simple WPF application for capture an area of screen, only including the basic function.  You can drag the application onto the task bar and run itself so that simplifies your work.    

Thinking

How to make the desktop looks "dead"?

"Dead" here means the whole desktop has no response for any operation. I use an unframed full screen window contain the picture of the screen itself.  In brief, taking a picture for the whole screen and then put it into a "full max" window.  

How to look "gray"?  

Picture is at the bottom on the window (Panel.ZIndex="0") and an "gray" transparent canvas (Panel.ZIndex="1") is at the top on the window. The mixed result can make window looks "gray" to fulfill the need. 

Opacity is very useful here to make canvas looks "gray". The detailed attribute is Opacity="0.5" Background="Black", it's a very easy step to make translucent for an canvas. But when it comes to the selected area (original color), this is not translucent, so we have no way to use the Opacity any more. 

Apply 3X3 grid layout can solve this problem directly.  You will got the idea after see the below picture. 

 

How to select the area?

It's very clear that we should record two point's position in the MouseDown and MouseMove event. beginPoint init in the MouseDown event and endPoint update in the MouseMove.

After confirm positions of two points, middle grid's boundary should be adjust at the same time.

  • Width of first column equals with Math.Min(beginPoint.X, endPoint.X)
  • Height of first row equals with Math.Min(beginPoint.Y, endPoint.Y)
  • Width of middle grid equals with Math.Abs(beginPoint.X-endPoint.X)
  • Height of middle grid equals with Math.Abs(beginPoint.Y-endPoint.Y) 

Using the code   

The following code is used for capture for an area that mainly use the  CopyFromScreen method instead of API. Also noticed the condition that width or height equals with zero will return null.  

public static Bitmap CaptureScreen(int x, int y, int width, int height)
{
    if (width == 0 || height == 0)
    {
        return null;
    }
    Bitmap image = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    using (Graphics graphic = Graphics.FromImage(image))
    {
        graphic.CopyFromScreen(x, y, 0, 0, new System.Drawing.Size(width, height), CopyPixelOperation.SourceCopy);
    }
    return image;
}  

Points of Interest 

Concerning about multi-screen of different resolution and size. I only have the idea about  all screen are are on the  horizontal direction. May be one horizontal and other vertical will return the wrong size for the whole screen (not tested). Does anybody now how judge whether the screen is on horizontal or vertical direction?  

 foreach (System.Windows.Forms.Screen screen in screens)
{
    System.Drawing.Rectangle screenRect = screen.Bounds;
    screenWidth += screenRect.Width;
    if (screenRect.Height > screenHeight)
    {
        screenHeight = screenRect.Height;
    }
}
this.Width = screenWidth;
this.Height = screenHeight;  

History  

  • Initial version v1.0.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here