C#仿QQ實(shí)現(xiàn)簡(jiǎn)單的截圖功能
接上一篇寫(xiě)的截取電腦屏幕,我們?cè)谠瓉?lái)的基礎(chǔ)上加一個(gè)選擇區(qū)域的功能,實(shí)現(xiàn)自定義選擇截圖。
個(gè)人比較懶,上一篇的代碼就不重新設(shè)計(jì)了,就簡(jiǎn)單改一下呈現(xiàn)方式。
不得不吐槽一下,在windows10系統(tǒng)上設(shè)置了放大比例的話,用這種方式來(lái)實(shí)現(xiàn)截圖功能的話需要去計(jì)算比例。后面有機(jī)會(huì)的話,用第三方DLL再實(shí)現(xiàn)一次。
實(shí)現(xiàn)功能
屏幕選擇區(qū)域截圖
開(kāi)發(fā)環(huán)境
開(kāi)發(fā)工具:Visual Studio 2013
.NET Framework版本:4.5
實(shí)現(xiàn)代碼
//將上一篇的內(nèi)容改成以下內(nèi)容 // pictureBox1.Image = bmp; Form2 frm = new Form2(); frm.BaseImage = bmp; frm.TopMost = true; frm.Show();
/*Form2代碼*/
#region Dll引用
[DllImport("User32.dll", EntryPoint = "GetDC")]
private extern static IntPtr GetDC(IntPtr hWnd);
[DllImport("User32.dll", EntryPoint = "ReleaseDC")]
private extern static int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
[DllImport("User32.dll")]
public static extern int GetSystemMetrics(int hWnd);
const int DESKTOPVERTRES = 117;
const int DESKTOPHORZRES = 118;
const int SM_CXSCREEN = 0;
const int SM_CYSCREEN = 1;
#endregion
//<summary>
//獲取DPI縮放比例
//</summary>
//<param name="dpiscalex"></param>
//<param name="dpiscaley"></param>
public static void GetDPIScale(ref float dpiscalex, ref float dpiscaley)
{
int x = GetSystemMetrics(SM_CXSCREEN);
int y = GetSystemMetrics(SM_CYSCREEN);
IntPtr hdc = GetDC(IntPtr.Zero);
int w = GetDeviceCaps(hdc, DESKTOPHORZRES);
int h = GetDeviceCaps(hdc, DESKTOPVERTRES);
ReleaseDC(IntPtr.Zero, hdc);
dpiscalex = (float)w / x;
dpiscaley = (float)h / y;
}
public Bitmap BaseImage { get; set; }
Graphics picGraphics;
//記錄鼠標(biāo)開(kāi)始截圖的位置
int startX = 0, startY = 0;
//記錄鼠標(biāo)結(jié)束截圖的位置
int endX = 0, endY = 0;
//記錄DPI縮放比例
float x = 0f, y = 0f;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
/* 初始化賦值*/
GetDPIScale(ref x, ref y);
picGraphics = pictureBox1.CreateGraphics();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
DrawRect(endX - startX, endY - startY,e.Graphics);
}
private void Form2_KeyPress(object sender, KeyPressEventArgs e)
{
//按下esc退出
if (e.KeyChar == 27)
{
this.Close();
}
}
//完成截圖
private void lbSucess_Click(object sender, EventArgs e)
{
int clip_w = (int)((endX - startX) * x) - 4, clip_h = (int)((endY - startY) * y) - 4;
if (clip_w < 1 || clip_h < 1)
{
return;
}
//獲取截圖
Bitmap clipBmp = new Bitmap(clip_w, clip_h);
Graphics g = Graphics.FromImage(clipBmp);
g.CopyFromScreen((int)(startX * x) + 2, (int)(startY * y) + 2, 0, 0, new Size(clip_w, clip_h), CopyPixelOperation.SourceCopy);
//將截圖設(shè)置到剪切板
Clipboard.SetImage(clipBmp);
g.Dispose();
this.Close();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
//隱藏操作面板
panel1.Visible = false;
//記錄鼠標(biāo)開(kāi)始截圖的位置
if (e.Button == MouseButtons.Left)
{
startX = e.X;
startY = e.Y;
endX = 0; endY = 0;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//繪制截圖區(qū)域
DrawRect(e.X - startX, e.Y - startY, picGraphics);
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
//截圖完成
if (e.Button == MouseButtons.Left)
{
endX = e.X;
endY = e.Y;
DrawRect(endX - startX, endY - startY, picGraphics);
if (endX > startX && endY > startY)
{
//顯示操作面板
Thread.Sleep(100);
panel1.Location = new Point(e.X - panel1.Width, e.Y + 5);
panel1.Visible = true;
}
}
}
//繪制截圖區(qū)域
private void DrawRect(int w, int h,Graphics g)
{
Bitmap img = (Bitmap)BaseImage.Clone();
//雙緩沖技術(shù)畫(huà)矩形,防止重影和抖動(dòng)
Graphics Painter = Graphics.FromImage(img);
Painter.DrawRectangle(new Pen(Color.Red), startX * x, startY * y, w * x, h * y);
g.DrawImage(img, 0, 0, img.Width / x, img.Height / y);
Painter.Dispose();
img.Dispose();
}實(shí)現(xiàn)效果

我這邊演示代碼很少做異常處理(前面也是,后面也是,畢竟不是做項(xiàng)目為主),大家使用的時(shí)候根據(jù)情況自行處理下即可,亦或者可能會(huì)有內(nèi)存未及時(shí)釋放的情況。
到此這篇關(guān)于C#仿QQ實(shí)現(xiàn)簡(jiǎn)單的截圖功能的文章就介紹到這了,更多相關(guān)C#截圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)帶進(jìn)度條的ListView
這篇文章主要介紹了C#實(shí)現(xiàn)帶進(jìn)度條的ListView 的相關(guān)資料,需要的朋友可以參考下2016-02-02
FileShare枚舉的使用小結(jié)(文件讀寫(xiě)鎖)
其實(shí)/FileShare就是控制文件流的“訪問(wèn)權(quán)限”,當(dāng)然,這僅僅是入門(mén)的文件操作,自己做了筆記,也希望能給大家?guī)?lái)幫助2014-01-01
C#中委托的基礎(chǔ)入門(mén)與實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于C#中委托的基礎(chǔ)入門(mén)與實(shí)現(xiàn)方法的相關(guān)資料,究竟什么是委托,用最通俗易懂的話來(lái)講,你就可以把委托看成是用來(lái)執(zhí)行方法(函數(shù))的一個(gè)東西,需要的朋友可以參考下2021-08-08
C#?App.config和Web.config加密的實(shí)現(xiàn)步驟
本文介紹了如何使用C#對(duì)App.config和Web.config文件進(jìn)行加密,通過(guò)使用ConfigurationSection類(lèi)和SymmetricAlgorithm類(lèi),我們可以保護(hù)配置文件中的敏感數(shù)據(jù),確保只有授權(quán)人員可以訪問(wèn)2023-08-08
C#在MySQL大量數(shù)據(jù)下的高效讀取、寫(xiě)入詳解
最近由于工作的原因,經(jīng)常需要對(duì)海量數(shù)據(jù)進(jìn)行處理,做的數(shù)據(jù)爬蟲(chóng)相關(guān),動(dòng)輒千萬(wàn)級(jí)別的數(shù)據(jù),單表幾十個(gè)G 都是都是家常便飯。 那么主要的開(kāi)發(fā)語(yǔ)言是C#,數(shù)據(jù)庫(kù)使用的是MySQL。下面通過(guò)這篇文章我們來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11
C#實(shí)現(xiàn)微信退款及對(duì)賬功能的示例詳解
在招聘報(bào)名系統(tǒng)里,考務(wù)費(fèi)支付是其中一個(gè)環(huán)節(jié),支付方式很多種,比如銀聯(lián)、微信、支付寶等等,本次我們以微信支付進(jìn)行舉例,在實(shí)際的應(yīng)用中,對(duì)于支付成功的考生,我們會(huì)遇到實(shí)現(xiàn)退款的需求,所以本文給大家介紹了使用C#實(shí)現(xiàn)微信退款及對(duì)賬,需要的朋友可以參考下2023-11-11
C#使用iTextSharp從PDF文檔獲取內(nèi)容的方法
這篇文章主要介紹了C#使用iTextSharp從PDF文檔獲取內(nèi)容的方法,涉及C#基于iTextSharp操作pdf文件的相關(guān)技巧,需要的朋友可以參考下2015-06-06

