C#截圖操作(幾種截圖方法)


公共函數
獲取屏幕截圖
private Bitmap GetScreenCapture()
{
Rectangle tScreenRect = new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Bitmap tSrcBmp = new Bitmap(tScreenRect.Width, tScreenRect.Height); // 用於屏幕原始圖片保存
Graphics gp = Graphics.FromImage(tSrcBmp);
gp.CopyFromScreen(0, 0, 0, 0, tScreenRect.Size);
gp.DrawImage(tSrcBmp, 0,0,tScreenRect,GraphicsUnit.Pixel);
return tSrcBmp;
}
1
2
3
4
5
6
7
8
9
圖片灰度化
public static Bitmap ImgRgbToGray(Bitmap bitmap)
{
Bitmap b = new Bitmap(bitmap);
BitmapData bmData = b.LockBits( new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
int stride = bmData.Stride; // 掃描的寬度
unsafe
{
byte* p = (byte*)bmData.Scan0.ToPointer(); // 獲取圖像首地址
int nOffset = stride - b.Width * 3; // 實際寬度與系統寬度的距離
byte red, green, blue;
for (int y = 0; y < b.Height; ++y)
{
for (int x = 0; x < b.Width; ++x)
{
blue = p[0];
green = p[1];
red = p[2];

p[0] = p[1] = p[2] = (byte)(.299 * red + .587 * green + .114 * blue); // 轉換公式

p += 3; // 跳過3個字節處理下個像素點
}
p += nOffset; // 加上間隔
}
}
b.UnlockBits(bmData); // 解鎖
return b;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
截圖方法的具體實現
假定是在坐標為(55,88)的地方截取長寬為(520,233)的矩形,截圖時整個背景灰化,截圖區域顯示原圖

使用像素替換的方法
該方法代碼最簡單,核心函數是 GetPixel 、SetPixel ,但是速度不敢恭維!!!特別慢!!!!!!!!

private Bitmap PixelReplace()
{
Bitmap tSrcBtm = GetScreenCapture();
Bitmap tGrayBtm = ImgRgbToGray();
for (int i = 55; i < 55 + 520; i++)
{
for (int k = 88; k < 88 + 233; k++)
{
tGrayBtm.SetPixel(i,k, tSrcBtm.GetPixel(i,k));
}
}
return tGrayBtm;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
在原圖上畫圖的方法
該方法比較快,但是每次都需要重新繪制,改變了底圖

private Bitmap DrawOnPicture()
{
int tAbsWidth = 1;
Bitmap tSrcBtm = GetScreenCapture();
Bitmap tGrayBtm = ImgRgbToGray();
Rectangle tRectArea = new Rectangle(55, 88, 520, 233);
Rectangle tDrawArea = new Rectangle(55 + tAbsWidth, 88 + tAbsWidth, 520 - tAbsWidth, 233 - tAbsWidth);
Graphics gp = Graphics.FromImage(tGrayBtm);
gp.DrawRectangle(Pens.Red, tRectArea);
gp.DrawImage(tSrcBtm, tDrawArea, tRectArea, GraphicsUnit.Pixel);

return tGrayBtm;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
使用蒙板的方法
實際上也是在在圖片上畫圖,只不過背景上直接畫了一個灰色的rect鋪滿屏幕,而不是取的灰度圖

public void CaptureScreen()
{
// 獲取屏幕
Bitmap btp = GetScreenCapture();
Image BackScreen = new Bitmap(btp);

// 在 BackScreen 上繪制蒙板
Graphics g = Graphics.FromImage(BackScreen);
g.FillRectangle(new SolidBrush(Color.FromArgb(100, 0, 0, 0)), 0, 0, BackScreen.Width, BackScreen.Height);
this.BackgroundImage = BackScreen;

// 最大化
this.WindowState = FormWindowState.Maximized;

// 不在任務欄顯示
this.ShowInTaskbar = false;
this.TopMost = true;

// 無邊框
this.FormBorderStyle = FormBorderStyle.None;

// 在坐標為(55,88)的地方截取長寬為(520,233)的矩形
Rectangle rect = new Rectangle(55, 88, 520, 233);
g.DrawImage(btp, rect, rect, GraphicsUnit.Pixel);

this.Show();
}
————————————————
版權聲明:本文為CSDN博主「盧然小子」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_36381867/article/details/79434143


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM