C# 全屏幕截图的代码实例
本文导语: C#实现的一个全屏幕截图的代码。 第一步获取整个屏幕的位图,记得Win32 API的CreateDC, BitBlt等函数可以使用。找到一个无Win32 API的实现方式,结合网上别的案例,自己完善了一下。 具体实现思路与代码,如下文所示。 1,开始截...
C#实现的一个全屏幕截图的代码。
第一步获取整个屏幕的位图,记得Win32 API的CreateDC, BitBlt等函数可以使用。找到一个无Win32 API的实现方式,结合网上别的案例,自己完善了一下。
具体实现思路与代码,如下文所示。
1,开始截图时,创建一个与屏幕大小一样的位图,然后用Graphics.CopyFromScreen()把屏幕位图拷贝到该位图上。
此步很关键:这样实现后,所有的操作就都可以在该位图上进行了,而无实际屏幕无关了。
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,它自动会形成一个中空的区域。
有兴趣的朋友,还可以扩展下以上代码的功能,比如实现多区域截图,多裁判截图等,都不是很难,大家动手实践下吧。