用C#来捕获屏幕

 
用C#来捕获屏幕(1)
 
[ 作者: 王天 添加时间: 2002-3-29 14:37:15 ]
 
来源: 赛迪网 www.ccidnet.com
 
其实用C#可以完成其他程序设计语言的几乎全部功能,当然C#自身的许多独到的功能,是其他程序语言所无法实现的,这就是C#越来越受到广大程序员的喜欢的原因。本文就来探讨一下用C#去实现一个重要的功能,用他来编写一个捕获当前屏幕地程序。通过这个程序,我们将了解到C#是如何调用API函数,和.Net框架中的类库内容是多么地丰富,功能是多么地强大。
 
一. 程序设计开发及运行环境:
 
(1).微软视窗2000服务器版
 
(2)..Net FrameWork SDK Beta 2
 
二. 程序设计的关键步骤:
 
要想完成这个功能,首先要了解一下在C#中如何调用API(应用程序接口)函数。虽然在.Net框架中已经提供了许多类库,这些类库的功能也十分强大,但对于一些Windows底层编程来说,还是要通过调用这些API函数才可以实现。所有API都在 "Kernel" "User " "GDI" 三个库中得以运行:其中 "Kernel" ,他的库名为 "KERNEL32.DLL" , 他主要用于产生与操作系统之间的关联,譬如:程序加载,上下文选择,文件输入输出,内存管理等等。 "User " 这个类库在Win32中名叫 "USER32.DLL" 。 它允许管理全部的用户接口。譬如:窗口 、菜单 、对话框、图标等等。 "GDI" (图象设备接口),它在Win32中的库名为: "GDI32.dll" ,它是图形输出库。使用GDI Windows "画" 出窗口、菜单以及对话框等;它能创建图形输出;它也能保存图形文件。由于本文所涉及到是图象问题,所有调用的类库是 "GDI32.dll" 。在本文程序中我们使用的API函数是 "BitBlt" ,这个函数对于广大程序员来说,一定不感觉到陌生,因为在图象处理方面他的用途是相对广的,在用其他程序语言编程中,时常也要和他打交道。在.Net FrameWork SDK中有一个名字空间 "System.Runtime.InteropServices" ,此名字空间提供了一系列的类来访问COM对象,和调用本地的API函数。下面是在C#中声明此函数:
 
[ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
private static extern bool BitBlt (
IntPtr hdcDest , // 目标 DC的句柄
int nXDest ,
int nYDest ,
int nWidth ,
int nHeight ,
IntPtr hdcSrc , // 源DC的句柄
int nXSrc ,
int nYSrc ,
System. Int32 dwRop // 光栅的处理数值
) ;
 
通过上面这个声明,就可以在下面的代码中使用此函数了。
 
下面是用C#做屏幕捕获程序的具体实现步骤:
 
(1).首先要获得当前屏幕的graphic对象,通过以下代码可以实现:
 
Graphics g1 = this .CreateGraphics ( ) ;
 
(2).创建一个Bitmap对象,并且这个Bitmap对象的大小是当前屏幕:
 
首先要获得当前屏幕的大小,通过名字空间 "System.Windows.Forms" 中的 "Screen" 类的GetWorkingArea()方法,可以实现。下面是得到当前屏幕的长(Height)和宽(Width):
 
Rectangle rect = new Rectangle ( ) ;
rect = Screen.GetWorkingArea ( this ) ;
"屏幕宽" = rect.Width ;
"屏幕长" = rect.Height ;
 
至此就可以得到我们想要的Bitmap了,通过下列语句可以实现:
 
Image MyImage = new Bitmap ( rect.Width , rect.Height , g1 ) ;
//创建以屏幕大小为标准的位图
 
(3).获得当前屏幕和此Bitmap对象的DC,这可以通过下列语句实现:
 
//得到屏幕的DC
IntPtr dc1 = g1.GetHdc ( ) ;
//得到Bitmap的DC
IntPtr dc2 = g2.GetHdc ( ) ;
 
(4).调用API函数,把当前屏幕拷贝到创建的Bitmap中:
 
BitBlt ( dc2 , 0 , 0 , rect.Width , rect.Height , dc1 , 0 , 0 , 13369376 ) ;
 
(5).释放当前屏幕和此Bitmap对象的DC,通过下面代码可以实现:
 
//释放掉屏幕的DC
g1.ReleaseHdc ( dc1 ) ;
//释放掉Bitmap的DC
g2.ReleaseHdc ( dc2 ) ;
 
(6).保存Bitmap对象,形成jpg图片:
 
MyImage.Save ( @ "c:/Capture.jpg" , ImageFormat.Jpeg );
 
当然你也可以根据自己的需要,把屏幕以其他图片的格式来保存,如果你想把图片保存为位图文件,可以把 "ImageFormat.Jpeg" 改换成 "ImageFormat.Bmp" ;想把图片保存为Gif文件,就把 "ImageFormat.Jpeg" 改换成 "ImageFormat.Gif" 。你可以保存的文件类型大概有十多种,这里就不一一介绍了,当然你也要相应改变保存文件的后缀。
 
三. 用C#来捕获屏幕的源程序代码(Capture.cs):
 
了解上面的这些步骤的实现方法,就可以得到用C#捕获屏幕的源程序,如下:
using  System;

using  System.Drawing;

using  System.Collections;

using  System.ComponentModel;

using  System.Windows.Forms;

using  System.Data;

using  System.Drawing.Imaging;

 

namespace  WindowsApplication3

{

    
public partial class Form1 : Form

    
{

        
private Button button1;

 

        
public Form1()

        
{

            InitializeComponent();

        }


 

        
private void Form1_Load(object sender, EventArgs e)

        
{

            button1 
= new Button();

            SuspendLayout();

            button1.Location 
= new System.Drawing.Point(6440);

            button1.Name 
= "button1";

            button1.Size 
= new System.Drawing.Size(8032);

            button1.TabIndex 
= 0;

            button1.Text 
= "捕获";

            button1.Click 
+= new System.EventHandler(button1_Click);

 

            AutoScaleBaseSize 
= new System.Drawing.Size(614);

            ClientSize 
= new System.Drawing.Size(216125);

            Controls.Add(button1);

            MaximizeBox 
= false;

            MinimizeBox 
= false;

            Name 
= "Form1";

            Text 
= "C#捕获当前屏幕!";

            ResumeLayout(
false);

 

        }


        
//声明一个API函数

        [System.Runtime.InteropServices.DllImportAttribute(
"gdi32.dll")]

        
private static extern bool BitBlt(

        IntPtr hdcDest, 
// 目标 DC的句柄

        
int nXDest,

        
int nYDest,

        
int nWidth,

        
int nHeight,

        IntPtr hdcSrc, 
// 源DC的句柄

        
int nXSrc,

        
int nYSrc,

        System.Int32 dwRop 
// 光栅的处理数值

        );

 

        
private void button1_Click(object sender, System.EventArgs e)

        
{

            
//获得当前屏幕的大小

            Rectangle rect 
= new Rectangle();

            rect 
= Screen.GetWorkingArea(this);

            
//创建一个以当前屏幕为模板的图象

            Graphics g1 
= this.CreateGraphics();

            
//创建以屏幕大小为标准的位图

            Image MyImage 
= new Bitmap(rect.Width, rect.Height, g1);

            Graphics g2 
= Graphics.FromImage(MyImage);

            
//得到屏幕的DC

            IntPtr dc1 
= g1.GetHdc();

            
//得到Bitmap的DC

            IntPtr dc2 
= g2.GetHdc();

            
//调用此API函数,实现屏幕捕获

            BitBlt(dc2, 
00, rect.Width, rect.Height, dc1, 0013369376);

            
//释放掉屏幕的DC

            g1.ReleaseHdc(dc1);

            
//释放掉Bitmap的DC

            g2.ReleaseHdc(dc2);

            
//以JPG文件格式来保存

            MyImage.Save(
@"c:Capture.jpg", ImageFormat.Jpeg);

            MessageBox.Show(
"当前屏幕已经保存为C盘的capture.jpg文件!");

        }


    }


}


 
捕获屏幕输入焦点,你可以使用以下代码: ```csharp using System; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; public class ScreenCaptureForm : Form { // 变量声明 private Rectangle _captureRect; private bool _capturing; private Point _startPoint; private Point _endPoint; // 构造函数 public ScreenCaptureForm() { this.DoubleBuffered = true; this.FormBorderStyle = FormBorderStyle.None; this.WindowState = FormWindowState.Maximized; this.Cursor = Cursors.Cross; this.MouseDown += new MouseEventHandler(ScreenCaptureForm_MouseDown); this.MouseMove += new MouseEventHandler(ScreenCaptureForm_MouseMove); this.Paint += new PaintEventHandler(ScreenCaptureForm_Paint); this.KeyDown += new KeyEventHandler(ScreenCaptureForm_KeyDown); } // 捕获屏幕方法 public Image CaptureScreen(Rectangle rect) { Bitmap bmp = new Bitmap(rect.Width, rect.Height); using (Graphics g = Graphics.FromImage(bmp)) { g.CopyFromScreen(rect.Location, Point.Empty, rect.Size); } return bmp; } // 绘制截图框方法 private void DrawCaptureRect(Graphics g) { using (Pen pen = new Pen(Color.Red, 2)) { g.DrawRectangle(pen, _captureRect); } } // 鼠标按下事件 private void ScreenCaptureForm_MouseDown(object sender, MouseEventArgs e) { _capturing = true; _startPoint = e.Location; } // 鼠标移动事件 private void ScreenCaptureForm_MouseMove(object sender, MouseEventArgs e) { if (_capturing) { _endPoint = e.Location; _captureRect.Location = new Point( Math.Min(_startPoint.X, _endPoint.X), Math.Min(_startPoint.Y, _endPoint.Y)); _captureRect.Size = new Size( Math.Abs(_startPoint.X - _endPoint.X), Math.Abs(_startPoint.Y - _endPoint.Y)); this.Invalidate(); } } // 绘制事件 private void ScreenCaptureForm_Paint(object sender, PaintEventArgs e) { if (_capturing) { DrawCaptureRect(e.Graphics); } } // 按键事件 private void ScreenCaptureForm_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Escape) { this.Close(); } else if (e.KeyCode == Keys.Enter) { Image img = CaptureScreen(_captureRect); img.Save("screenshot.png", System.Drawing.Imaging.ImageFormat.Png); this.Close(); } } // 程序入口 static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new ScreenCaptureForm()); } } ``` 这个代码实现了一个简单的屏幕截图工具,当你运行它时,会在屏幕上显示一个红色的矩形框,你可以用鼠标拖动这个框来选择截图区域。按下 Enter 键时,程序会将选中的区域截图保存到当前目录下的 screenshot.png 文件中。如果按下 Esc 键则退出程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值