C#基础演练:屏幕抓取小工具

TeboScreen: Basic C# Screen Capture Application
源英文文档 <http://www.codeproject.com/KB/cs/TeboScreen.aspx>
翻译:白水先生(敏捷学院)

源代码下载:http://dev.mjxy.cn/a-CSharp-based-exercises-screen-capture.aspx

Introduction

 

应用程序抓取屏幕有两种不同的方式:

抓取屏幕:抓取整个屏幕,然后将图像保存到指定的图片文件中。

抓取区域:用户按住鼠标左键,绘制一个矩形,指定他们希望捕捉屏幕的一部分。在释放鼠标左键,然后指定一个文件名将扑捉到的矩形图像保存到指定的文件中。

背景

这个应用程序是我一个C#学习的练习。我想看看使用C#如何简单的创建一个屏幕保存 程序,最后我花了一个下午的时间写应用程序。

我决定使用一个简单的方式来解决如何让用户在屏幕上画一个矩形。最大化应用程序窗口,并使它透明度为30%。先绘制一个矩形,然后使用窗体背景色绘制一个矩形用来擦出以前的矩形。由于窗体是最大化的,任何坐标位置正好对应屏幕上的坐标。ScreenShot.CaptureImage方法绘制矩形并返回矩形的坐标。

代码分析

两个模块实现屏幕抓取

Capture Screen

我们需要做的就是将屏幕的区域传递给ScreenShot.CaptureImage方法。这里唯一需要注意的地方就是我们暂停了250毫秒,让屏幕重绘。如果不这样做,直接执行下一行代码,我们应用程序的本身也会被扑捉到。

//Allow 250 milliseconds for the screen to repaint itself
 
//(we don't want to include this form in the capture)
 
System.Threading.Thread.Sleep(250);
 
Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty));
 
ScreenShot.CaptureImage(Point.Empty, Point.Empty, bounds, ScreenPath);

 

捕抓一个区域

用户按住鼠标左键,绘制一个矩形,指定他们希望捕捉屏幕的一部分。在释放鼠标左键,然后指定一个文件名将扑捉到的矩形图像保存到指定的文件中。mouse_Move事件用来擦除之前绘制的矩形。

        

private void mouse_Move(object sender, MouseEventArgs e)
 
        {
 
        //Resize (actually delete then re-draw) the rectangle if the left
 
        mouse button is held down
 
        if (LeftButtonDown)
 
            {
 
            //Erase the previous rectangle
 
            g.DrawRectangle(EraserPen, CurrentTopLeft.X, CurrentTopLeft.Y,CurrentBottomRight.X - CurrentTopLeft.X, CurrentBottomRight.Y -
 
CurrentTopLeft.Y);
 
            //Calculate X Coordinates
 
            if (Cursor.Position.X < ClickPoint.X)
 
                {
 
                CurrentTopLeft.X = Cursor.Position.X;
 
                CurrentBottomRight.X = ClickPoint.X;
 
                }
 
            else
 
                {
 
                CurrentTopLeft.X = ClickPoint.X;
 
                CurrentBottomRight.X = Cursor.Position.X;
 
                }
 
            //Calculate Y Coordinates
 
            if (Cursor.Position.Y < ClickPoint.Y)
 
                {
 
                CurrentTopLeft.Y = Cursor.Position.Y;
 
                CurrentBottomRight.Y = ClickPoint.Y;
 
                }
 
            else
 
                {
 
                CurrentTopLeft.Y = ClickPoint.Y;
 
                CurrentBottomRight.Y = Cursor.Position.Y;
 
                }
 
            //Draw a new rectangle
 
            g.DrawRectangle(MyPen, CurrentTopLeft.X, CurrentTopLeft.Y,CurrentBottomRight.X - CurrentTopLeft.X, CurrentBottomRight.Y - CurrentTopLeft.Y);
 
            }
 
        }

这里是调用ScreenShot.CaptureImage如何捕抓一个区域。

Point StartPoint = new Point(ClickPoint.X, ClickPoint.Y);
Rectangle bounds = new Rectangle(ClickPoint.X, ClickPoint.Y, CurrentPoint.X - ClickPoint.X, CurrentPoint.Y - ClickPoint.Y);
ScreenShot.CaptureImage(StartPoint, Point.Empty, bounds, ScreenPath);

捕抓屏幕的代码定义在类ScreenShot中,包含一个静态的方法CaptureImage。

     class ScreenShot
 
     {
 
     public static void CaptureImage(Point SourcePoint, Point DestinationPoint,
 
         Rectangle SelectionRectangle, string FilePath)
 
         {
 
         using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width,
 
             SelectionRectangle.Height))
 
             {
 
             using (Graphics g = Graphics.FromImage(bitmap))
 
                 {
 
                 g.CopyFromScreen(SourcePoint, DestinationPoint,
 
                     SelectionRectangle.Size);
 
                 }
 
             bitmap.Save(FilePath, ImageFormat.Bmp);
 
             }
 
         }
 
     }

转载注明出处:敏捷学院

你可能感兴趣的:(C#,职场,休闲,抓图)