c#捕获当前屏幕

编程思路(API 编程):
先调用 GetForegroundWindow 获取当前活动程序窗口句柄,然后调用 GetWindowDC 获取窗口的设备句柄(或 GetDC 函数),调用 BitBlt 位图传输函数将位图拷贝到兼容的设备场景中(拷贝时可以指定位置和大小),最后保存位图文件。

以下源代码内容转自 CSDN 论坛。

要想完成这个功能,首先要了解一下在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 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
捕获屏幕输入焦点,你可以使用以下代码: ```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、付费专栏及课程。

余额充值