C# Winform實(shí)現(xiàn)截圖工具的示例代碼
更新時(shí)間:2024年02月15日 08:30:49 作者:搬磚的詩(shī)人Z
這篇文章主要為大家詳細(xì)介紹了如何使用C# Winform制作一個(gè)簡(jiǎn)單的截圖工具,從而實(shí)現(xiàn)截圖功能,文中的示例代碼講解詳細(xì),有需要的可以參考下
效果圖

實(shí)現(xiàn)代碼
截圖主要代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace OCR
{
public partial class ScreenBody : Form
{
public static Image myImage;
private Graphics MainPainter; //主畫面
private Pen pen; //畫筆
private bool isDowned; //判斷鼠標(biāo)是否按下
private bool RectReady; //矩形是否繪制完成
private Image baseImage; //基本圖形(原來的畫面)
private Rectangle Rect; //就是要保存的矩形
private Point downPoint; //鼠標(biāo)按下的點(diǎn)
int tmpx;
int tmpy;
public ScreenBody()
{
InitializeComponent();
}
private void ScreenBody_Load(object sender, EventArgs e)
{
try
{
this.WindowState = FormWindowState.Maximized;
MainPainter = this.CreateGraphics();
pen = new Pen(Brushes.Blue);
isDowned = false;
baseImage = this.BackgroundImage;
Rect = new Rectangle();
RectReady = false;
}
catch (Exception ex)
{
log4netHelper.Error("---報(bào)錯(cuò)方法--ScreenBody_Load");
log4netHelper.Error(ex.Message);
log4netHelper.Error(ex.StackTrace);
log4netHelper.Error("---end---");
}
}
private void ScreenBody_MouseDown(object sender, MouseEventArgs e)
{
try
{
if (e.Button == MouseButtons.Left)
{
isDowned = true;
if (RectReady == false)
{
Rect.X = e.X;
Rect.Y = e.Y;
downPoint = new Point(e.X, e.Y);
}
if (RectReady == true)
{
tmpx = e.X;
tmpy = e.Y;
}
}
if (e.Button == MouseButtons.Right)
{
this.Close();
return;
}
}
catch (Exception ex)
{
log4netHelper.Error("---報(bào)錯(cuò)方法--ScreenBody_MouseDown");
log4netHelper.Error(ex.Message);
log4netHelper.Error(ex.StackTrace);
log4netHelper.Error("---end---");
}
}
private void ScreenBody_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDowned = false;
RectReady = true;
}
}
private void ScreenBody_MouseMove(object sender, MouseEventArgs e)
{
try
{
if (RectReady == false)
{
if (isDowned == true)
{
Image New = DrawScreen((Image)baseImage.Clone(), e.X, e.Y);
MainPainter.DrawImage(New, 0, 0);
New.Dispose();
}
}
if (RectReady == true)
{
if (Rect.Contains(e.X, e.Y))
{
this.Cursor = Cursors.Hand;
if (isDowned == true)
{
//和上一次的位置比較獲取偏移量
Rect.X = Rect.X + e.X - tmpx;
Rect.Y = Rect.Y + e.Y - tmpy;
//記錄現(xiàn)在的位置
tmpx = e.X;
tmpy = e.Y;
MoveRect((Image)baseImage.Clone(), Rect);
}
}
else
{
this.Cursor = Cursors.Arrow;
}
}
}
catch (Exception ex)
{
log4netHelper.Error("---報(bào)錯(cuò)方法--ScreenBody_MouseMove");
log4netHelper.Error(ex.Message);
log4netHelper.Error(ex.StackTrace);
log4netHelper.Error("---end---");
}
}
//畫屏幕
private Image DrawScreen(Image back, int Mouse_x, int Mouse_y)
{
try
{
Graphics Painter = Graphics.FromImage(back);
DrawRect(Painter, Mouse_x, Mouse_y);
return back;
}
catch (Exception ex)
{
log4netHelper.Error("---報(bào)錯(cuò)方法--DrawScreen");
log4netHelper.Error(ex.Message);
log4netHelper.Error(ex.StackTrace);
log4netHelper.Error("---end---");
return null;
}
}
//畫矩形
private void DrawRect(Graphics Painter, int Mouse_x, int Mouse_y)
{
int width = 0;
int heigth = 0;
try
{
if (Mouse_y < Rect.Y)
{
Rect.Y = Mouse_y;
heigth = downPoint.Y - Mouse_y;
}
else
{
heigth = Mouse_y - downPoint.Y;
}
if (Mouse_x < Rect.X)
{
Rect.X = Mouse_x;
width = downPoint.X - Mouse_x;
}
else
{
width = Mouse_x - downPoint.X;
}
}
catch (Exception ex)
{
log4netHelper.Error("---報(bào)錯(cuò)方法--DrawRect");
log4netHelper.Error(ex.Message);
log4netHelper.Error(ex.StackTrace);
log4netHelper.Error("---end---");
}
finally
{
Rect.Size = new Size(width, heigth);
Painter.DrawRectangle(pen, Rect);
}
}
//移動(dòng)矩形
private void MoveRect(Image image, Rectangle Rect)
{
try
{
Graphics Painter = Graphics.FromImage(image);
Painter.DrawRectangle(pen, Rect.X, Rect.Y, Rect.Width, Rect.Height);
MainPainter.DrawImage(image, 0, 0);
image.Dispose();
}
catch (Exception ex)
{
log4netHelper.Error("---報(bào)錯(cuò)方法--MoveRect");
log4netHelper.Error(ex.Message);
log4netHelper.Error(ex.StackTrace);
log4netHelper.Error("---end---");
}
}
private void ScreenBody_MouseDoubleClick(object sender, MouseEventArgs e)
{
try
{
Image memory = new Bitmap(Rect.Width, Rect.Height);
Graphics g = Graphics.FromImage(memory);
g.CopyFromScreen(Rect.X + 1, Rect.Y + 1, 0, 0, new Size(Rect.Size.Width - 2, Rect.Size.Height - 2));
myImage = memory;
this.Close();
this.Dispose();
}
catch (Exception ex)
{
log4netHelper.Error("---報(bào)錯(cuò)方法--ScreenBody_MouseDoubleClick");
log4netHelper.Error(ex.Message);
log4netHelper.Error(ex.StackTrace);
log4netHelper.Error("---end---");
}
}
}
}HotKey,熱鍵注冊(cè)
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace OCR
{
public class HotKey
{
//如果函數(shù)執(zhí)行成功,返回值不為0。
//如果函數(shù)執(zhí)行失敗,返回值為0。要得到擴(kuò)展錯(cuò)誤信息,調(diào)用GetLastError。
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //要定義熱鍵的窗口的句柄
int id, //定義熱鍵ID(不能與其它ID重復(fù))
KeyModifiers fsModifiers, //標(biāo)識(shí)熱鍵是否在按Alt、Ctrl、Shift、Windows等鍵時(shí)才會(huì)生效
Keys vk //定義熱鍵的內(nèi)容
);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd, //要取消熱鍵的窗口的句柄
int id //要取消熱鍵的ID
);
//定義了輔助鍵的名稱(將數(shù)字轉(zhuǎn)變?yōu)樽址员阌谟洃洠部扇コ嗣杜e而直接使用數(shù)值)
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
}
}
}以上就是C# Winform實(shí)現(xiàn)截圖工具的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C# Winform截圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#實(shí)現(xiàn)在PDF文檔中應(yīng)用多種不同字體
在PDF文檔中,可繪制不同字體樣式、不同語(yǔ)言的文字,可通過使用Standard字體、TrueType字體、CJK字體或者自定義(私有)等字體類型。本文將具體介紹實(shí)現(xiàn)的方法,需要的可以參考一下2022-01-01
C#將圖片存放到SQL SERVER數(shù)據(jù)庫(kù)中的方法
這篇文章主要介紹了C#將圖片存放到SQL SERVER數(shù)據(jù)庫(kù)中的方法,以實(shí)例形式較為詳細(xì)的分析了C#保存圖片到SQL Server數(shù)據(jù)庫(kù)的具體步驟與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
關(guān)于finalize機(jī)制和引用、引用隊(duì)列的用法詳解
下面小編就為大家?guī)硪黄P(guān)于finalize機(jī)制和引用、引用隊(duì)列的用法詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
Unity的AssetPostprocessor之Model函數(shù)使用實(shí)戰(zhàn)
這篇文章主要為大家介紹了Unity的AssetPostprocessor之Model函數(shù)使用實(shí)戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08

