解決C#全屏幕截圖的實(shí)現(xiàn)方法
更新時(shí)間:2013年05月20日 11:28:55 作者:
本篇文章是對(duì)在C#中實(shí)現(xiàn)全屏幕截圖的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
今天一位同事想寫一個(gè)全屏幕截圖的代碼。當(dāng)然要實(shí)現(xiàn)的第一步是能夠獲取整個(gè)屏幕的位圖,記得Win32 API的CreateDC, BitBlt等函數(shù)可以使用。于是上網(wǎng)查了下,果然屏幕截圖用這些函數(shù)。但winform已經(jīng)可以把API都忘記了,所以得尋找一個(gè)無Win32 API的實(shí)現(xiàn)方式。綜合了網(wǎng)上的實(shí)現(xiàn),以及自己的一些設(shè)計(jì),實(shí)現(xiàn)思路如下:
1. 開始截圖時(shí),創(chuàng)建一個(gè)與屏幕大小一樣的位圖,然后用Graphics.CopyFromScreen()把屏幕位圖拷貝到該位圖上。這是很關(guān)鍵的一步,這樣所有的操作就都可以在該位圖上進(jìn)行了,而無實(shí)際屏幕無關(guān)了。
Code
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. 接下來為了方便在這之上進(jìn)行截圖,有一個(gè)很重要的設(shè)計(jì)實(shí)現(xiàn)方式:用全屏幕窗體代替現(xiàn)有真實(shí)屏幕,這樣就可以把截圖過程的所有操作都在那個(gè)窗體上實(shí)現(xiàn)(該窗體設(shè)置成無邊框,高寬等于屏幕大小即可),另外為了顯示掩蔽效果(只能正常顯示選擇的部分屏幕內(nèi)容,而其實(shí)部分用一個(gè)如半透明層覆蓋),就添加一層半透明位置位圖。具體代碼如下:
Code
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; }
}
}
上面的代碼都很容易看明白,這里有一個(gè)技巧就是GraphicsPath,它自動(dòng)會(huì)形成一個(gè)中空的區(qū)域。上面的實(shí)現(xiàn)很容易擴(kuò)展:多區(qū)域截圖,多裁判截圖等都很容易實(shí)現(xiàn)。
1. 開始截圖時(shí),創(chuàng)建一個(gè)與屏幕大小一樣的位圖,然后用Graphics.CopyFromScreen()把屏幕位圖拷貝到該位圖上。這是很關(guān)鍵的一步,這樣所有的操作就都可以在該位圖上進(jìn)行了,而無實(shí)際屏幕無關(guān)了。
復(fù)制代碼 代碼如下:
Code
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. 接下來為了方便在這之上進(jìn)行截圖,有一個(gè)很重要的設(shè)計(jì)實(shí)現(xiàn)方式:用全屏幕窗體代替現(xiàn)有真實(shí)屏幕,這樣就可以把截圖過程的所有操作都在那個(gè)窗體上實(shí)現(xiàn)(該窗體設(shè)置成無邊框,高寬等于屏幕大小即可),另外為了顯示掩蔽效果(只能正常顯示選擇的部分屏幕內(nèi)容,而其實(shí)部分用一個(gè)如半透明層覆蓋),就添加一層半透明位置位圖。具體代碼如下:
復(fù)制代碼 代碼如下:
Code
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; }
}
}
上面的代碼都很容易看明白,這里有一個(gè)技巧就是GraphicsPath,它自動(dòng)會(huì)形成一個(gè)中空的區(qū)域。上面的實(shí)現(xiàn)很容易擴(kuò)展:多區(qū)域截圖,多裁判截圖等都很容易實(shí)現(xiàn)。
相關(guān)文章
Unity實(shí)現(xiàn)場(chǎng)景漫游相機(jī)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)場(chǎng)景漫游相機(jī),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10C# Base 64 編碼/解碼實(shí)現(xiàn)代碼
這篇文章主要介紹了C# Base 64 編碼/解碼實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-02-02C#實(shí)現(xiàn)基于鏈表的內(nèi)存記事本實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)基于鏈表的內(nèi)存記事本,實(shí)例分析了C#基于鏈表實(shí)現(xiàn)的記事本功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07Unity實(shí)戰(zhàn)之FlyPin(見縫插針)小游戲的實(shí)現(xiàn)
這篇文章主要介紹了利用Unity制作FlyPin(見縫插針)小游戲的實(shí)現(xiàn)方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起試一試2022-01-01使用數(shù)字簽名實(shí)現(xiàn)數(shù)據(jù)庫記錄防篡改(Java實(shí)現(xiàn))
本文主要介紹了Java中使用數(shù)字簽名實(shí)現(xiàn)數(shù)據(jù)庫記錄防篡改的方法與步驟。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01淺談c#開發(fā)者應(yīng)該了解的15個(gè)特性
本文列舉了15個(gè)值得了解的C#特性,旨在讓.NET開發(fā)人員更好的使用C#語言進(jìn)行開發(fā)工作。2021-05-05C#實(shí)現(xiàn)功能強(qiáng)大的中國(guó)農(nóng)歷日歷操作類
這篇文章主要介紹了C#實(shí)現(xiàn)功能強(qiáng)大的中國(guó)農(nóng)歷日歷操作類,實(shí)例分析了C#操作時(shí)間及字符串的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03