前言:

最近学习C#,想找个练手的项目实践一下,回想到一款比较好用的截图工具GreenShot是C#开源的,于是想自己试着实现一下简单的截图功能,于是就有了这几篇文章作为总结。

截图的几种实现思路

  • 使用灰度图片替换原始图片,再将原始图片画到截图区域
  • 使用灰度图片替换原始图片,再使用原图相应坐标的像素替换
  • 在原图上使用蒙板实现截图区域与非截图区域的对比

图片的基本操作

  • 获取屏幕宽、高
  • 获取屏幕截图
  • 获取指定图片的灰度图
  • 在指定图片上的指定区域画图
  • bitmap 、 image 、 bytes 的转换

获取屏区域

屏幕信息可以通过使用 [ System.Windows.Forms.Screen ] 类来获取

// 获取工作区域
// 检索与指定点最接近的工作区。工作区是显示器的桌面区域,不包括任务栏、停靠窗口和停靠工具栏。
Rectangle tWorkArea = System.Windows.Forms.Screen.GetWorkingArea(this);// 获取整个屏幕区域
int tWide = Screen.PrimaryScreen.Bounds.Width;int tHeight = Screen.PrimaryScreen.Bounds.Height;Rectangle _screenRect = new Rectangle(0, 0, tWide, tHeight);

获取屏幕截图

// 创建位图对象,初始化长、宽
System.Drawing.Bitmap _srcScreen = new Bitmap(_screenRect.Width, _screenRect.Height);// 将位图与绘图区域绑定
Graphics graphics = Graphics.FromImage(_srcScreen);// 设置高清模式
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;// 从屏幕拷贝图片到 _screenRect
graphics.CopyFromScreen(0, 0, 0, 0, _screenRect.Size);// 绘制 _screenRect 到指定区域
graphics.DrawImage(_srcScreen, 0, 0, _screenRect, GraphicsUnit.Pixel);// 保存图片
_srcScreen.Save(@"C:tmp.png");// 保存jpg格式
_srcScreen.Save(@"C:tmp.jpg", ImageFormat.Jpeg);

将指定图片灰度化

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;
}

在图片的指定区域绘图

// 从本地加载图片
//Image tImg = Image.FromFile(@"C:a.png");//Image newBmp = Image.FromFile(@"C:b.png");Bitmap tImg = New Bitmap(@"C:a.png");Bitmap tNeedDraw = New Bitmap(@"C:b.png");Rectangle srcRect = new Rectangle(tImg.X,tImg.Y,tImg.Width,tImg.Height);//
Graphics graphics = Graphics.FromImage(tImg);Rectangle dstRect = new Rectangle(50, 20, tNeedDraw.Width, tNeedDraw.Height);// 画矩形
graphics.DrawRectangle(Pens.Goldenrod, dstRect);// 将 newBmp 的 newRect 区域 画到 tImg 上的 dstRect 区域,
graphics.DrawImage(newBmp, dstRect, newRect, GraphicsUnit.Pixel);

bitmap 和 image 转换

// 来源:https://www.cnblogs.com/arxive/p/6277377.html 
//Bitmap转换成Image
private static System.Windows.Controls.Image Bitmap2Image(System.Drawing.Bitmap Bi)
{
MemoryStream ms = new MemoryStream();
Bi.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
BitmapImage bImage = new BitmapImage();
bImage.BeginInit();
bImage.StreamSource = new MemoryStream(ms.ToArray());
bImage.EndInit();
ms.Dispose();
Bi.Dispose();
System.Windows.Controls.Image i = new System.Windows.Controls.Image();
i.Source = bImage;
return i ;}

//Image转换Bitmap1. Bitmap img = new Bitmap(imgSelect.Image);2. Bitmap bmp = (Bitmap)pictureBox1.Image;

Bitmap 和 byte[] 转换

byte[] byteImage = new Byte[ms.Length]; 
byteImage = ms.ToArray();

stream = new MemoryStream(byteImage);
Bitmap bmp = new Bitmap((Image)new Bitmap(stream));