当前位置:
首页 > temp > 简明python教程 >
-
用C#实现全屏幕截图
今天一位同事想写一个全屏幕截图的代码。当然要实现的第一步是能够获取整个屏幕的位图,记得Win32 API的CreateDC, BitBlt等函数可以使用。于是上网查了下,果然屏幕截图用这些函数。但winform已经可以把API都忘记了,所以得寻找一个无Win32 API的实现方式。综合了网上的实现,以及自己的一些设计,实现思路如下:1. 开始截图时,创建一个与屏幕大小一样的位图,然后用Graphics.CopyFromScreen()把屏幕位图拷贝到该位图上。这是很关键的一步,这样所有的操作就都可以在该位图上进行了,而无实际屏幕无关了。
int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;
Bitmap bmp = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(bmp)) {
g.CopyFromScreen(0, 0, 0, 0, new Size(width, height));
}
2. 接下来为了方便在这之上进行截图,有一个很重要的设计实现方式:用全屏幕窗体代替现有真实屏幕,这样就可以把截图过程的所有操作都在那个窗体上实现(该窗体设置成无边框,高宽等于屏幕大小即可),另外为了显示掩蔽效果(只能正常显示选择的部分屏幕内容,而其实部分用一个如半透明层覆盖),就添加一层半透明位置位图。具体代码如下:
public partial class FullScreenForm : Form {
private Rectangle rectSelected = Rectangle.Empty;
private bool isClipping = false;
private Bitmap screen;
private Bitmap coverLayer = null;
private Color coverColor;
private Brush rectBrush = null;
private Bitmap resultBmp = null;
public FullScreenForm(Bitmap screen) {
InitializeComponent();
int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;
coverLayer = new Bitmap(width, height);
coverColor = Color.FromArgb(50, 200, 0, 0);
rectBrush = new SolidBrush(coverColor);
using (Graphics g = Graphics.FromImage(coverLayer)) {
g.Clear(coverColor);
}
this.Bounds = new Rectangle(0, 0, width, height);
this.screen = screen;
this.DoubleBuffered = true;
}
protected override void OnMouseDown(MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
isClipping = true;
rectSelected.Location = e.Location;
}
else if (e.Button == MouseButtons.Right) {
this.DialogResult = DialogResult.OK;
}
}
protected override void OnMouseMove(MouseEventArgs e) {
if (e.Button == MouseButtons.Left & & isClipping) {
rectSelected.Width = e.X - rectSelected.X;
rectSelected.Height = e.Y - rectSelected.Y;
this.Invalidate();
}
}
protected override void OnMouseUp(MouseEventArgs e) {
if (e.Button == MouseButtons.Left && isClipping) {
rectSelected.Width = e.X - rectSelected.X;
rectSelected.Height = e.Y - rectSelected.Y;
this.Invalidate();
resultBmp = new Bitmap(rectSelected.Width, rectSelected.Height);
using (Graphics g = Graphics.FromImage(resultBmp)) {
g.DrawImage(screen,new Rectangle(0, 0, rectSelected.Width, rectSelected.Height), rectSelected, GraphicsUnit.Pixel);
}
this.DialogResult = DialogResult.OK;
}
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.DrawImage(screen, 0, 0);
g.DrawImage(coverLayer, 0, 0);
PaintRectangle();
}
protected override void OnPaintBackground(PaintEventArgs e) {
}
protected override void OnKeyDown(KeyEventArgs e) {
if (e.KeyCode == Keys.Escape) {
this.DialogResult = DialogResult.Cancel;
}
}
private void PaintRectangle() {
using (Graphics g = Graphics.FromImage(coverLayer)) {
g.Clear(coverColor);
GraphicsPath path = new GraphicsPath();
path.AddRectangle(this.Bounds);
path.AddRectangle(rectSelected);
g.FillPath(rectBrush, path);
g.DrawRectangle(Pens.Blue, rectSelected);
}
}
public Bitmap ResultBitmap {
get { return resultBmp; }
}
}
上面的代码都很容易看明白,这里有一个技巧就是GraphicsPath,它自动会形成一个中空的区域。上面的实现很容易扩展:多区域截图,多裁判截图等都很容易实现。
int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;
Bitmap bmp = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(bmp)) {
g.CopyFromScreen(0, 0, 0, 0, new Size(width, height));
}
2. 接下来为了方便在这之上进行截图,有一个很重要的设计实现方式:用全屏幕窗体代替现有真实屏幕,这样就可以把截图过程的所有操作都在那个窗体上实现(该窗体设置成无边框,高宽等于屏幕大小即可),另外为了显示掩蔽效果(只能正常显示选择的部分屏幕内容,而其实部分用一个如半透明层覆盖),就添加一层半透明位置位图。具体代码如下:
public partial class FullScreenForm : Form {
private Rectangle rectSelected = Rectangle.Empty;
private bool isClipping = false;
private Bitmap screen;
private Bitmap coverLayer = null;
private Color coverColor;
private Brush rectBrush = null;
private Bitmap resultBmp = null;
public FullScreenForm(Bitmap screen) {
InitializeComponent();
int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;
coverLayer = new Bitmap(width, height);
coverColor = Color.FromArgb(50, 200, 0, 0);
rectBrush = new SolidBrush(coverColor);
using (Graphics g = Graphics.FromImage(coverLayer)) {
g.Clear(coverColor);
}
this.Bounds = new Rectangle(0, 0, width, height);
this.screen = screen;
this.DoubleBuffered = true;
}
protected override void OnMouseDown(MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
isClipping = true;
rectSelected.Location = e.Location;
}
else if (e.Button == MouseButtons.Right) {
this.DialogResult = DialogResult.OK;
}
}
protected override void OnMouseMove(MouseEventArgs e) {
if (e.Button == MouseButtons.Left & & isClipping) {
rectSelected.Width = e.X - rectSelected.X;
rectSelected.Height = e.Y - rectSelected.Y;
this.Invalidate();
}
}
protected override void OnMouseUp(MouseEventArgs e) {
if (e.Button == MouseButtons.Left && isClipping) {
rectSelected.Width = e.X - rectSelected.X;
rectSelected.Height = e.Y - rectSelected.Y;
this.Invalidate();
resultBmp = new Bitmap(rectSelected.Width, rectSelected.Height);
using (Graphics g = Graphics.FromImage(resultBmp)) {
g.DrawImage(screen,new Rectangle(0, 0, rectSelected.Width, rectSelected.Height), rectSelected, GraphicsUnit.Pixel);
}
this.DialogResult = DialogResult.OK;
}
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.DrawImage(screen, 0, 0);
g.DrawImage(coverLayer, 0, 0);
PaintRectangle();
}
protected override void OnPaintBackground(PaintEventArgs e) {
}
protected override void OnKeyDown(KeyEventArgs e) {
if (e.KeyCode == Keys.Escape) {
this.DialogResult = DialogResult.Cancel;
}
}
private void PaintRectangle() {
using (Graphics g = Graphics.FromImage(coverLayer)) {
g.Clear(coverColor);
GraphicsPath path = new GraphicsPath();
path.AddRectangle(this.Bounds);
path.AddRectangle(rectSelected);
g.FillPath(rectBrush, path);
g.DrawRectangle(Pens.Blue, rectSelected);
}
}
public Bitmap ResultBitmap {
get { return resultBmp; }
}
}
上面的代码都很容易看明白,这里有一个技巧就是GraphicsPath,它自动会形成一个中空的区域。上面的实现很容易扩展:多区域截图,多裁判截图等都很容易实现。
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数