C#中利用System.Drawing库,通过截屏获取屏幕指定点颜色(推荐)或调用WindowsAPI实现取色,并可在位图上使用DrawEllipse绘制抗锯齿圆形。结合鼠标交互,实现点击图片取色并在点击位置画圆标记,提供完整WindowsForms示例。
C#使用System.Drawing实现位图取色与绘图方法
时间:2026-08-12
浏览:387
文章目录
在 C# 开发中,使用System.Drawing处理位图并不复杂。无论是获取屏幕颜色,还是在图片上绘制标记,只需掌握几个核心方法,就可以快速实现。下面围绕位图取色、绘图以及交互式示例,分别说明具体实现方式。

提取鼠标指定位置的颜色值
如果需要获取屏幕任意坐标的颜色,常见做法是先截取该位置的屏幕内容,再从位图中读取像素值。这里提供两种实现方式:一种使用Graphics.CopyFromScreen,兼容性更好,也更适合日常开发;另一种通过 Windows API 直接读取像素,执行效率较高,但需要额外引入外部函数。通常情况下,更推荐第一种方式。
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class ColorPicker
{
// 方法一:通过截屏获取指定屏幕坐标的颜色(推荐,更通用)
public static Color GetColorAtScreenPoint(Point screenPoint)
{
// 创建一个1x1像素的位图
using (Bitmap bitmap = new Bitmap(1, 1))
{
// 使用Graphics对象从屏幕指定位置拷贝1个像素 using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(screenPoint, Point.Empty, new Size(1, 1));
}
// 返回该像素的颜色
return bitmap.GetPixel(0, 0);
}
}
// 方法二:使用Windows API (GetPixel) 直接获取(需要引用user32.dll和gdi32.dll)
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
[DllImport("gdi32.dll")]
private static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);
public static Color GetColorAtScreenPoint_API(Point screenPoint)
{
IntPtr hdc = GetDC(IntPtr.Zero);
uint pixel = GetPixel(hdc, screenPoint.X, screenPoint.Y);
ReleaseDC(IntPtr.Zero, hdc);
// 将GetPixel返回的COLORREF转换为Color对象 byte red = (byte)(pixel & 0x000000FF);
byte green = (byte)((pixel & 0x0000FF00) >> 8);
byte blue = (byte)((pixel & 0x00FF0000) >> 16);
return Color.FromArgb(red, green, blue);
}
// 示例:在定时器或循环中实时获取鼠标位置颜色 public static void StartRealTimeColorPick(Label colorLabel)
{
Timer timer = new Timer();
timer.Interval = 50; // 每50毫秒更新一次
timer.Tick += (sender, e) =>
{
Point mousePos = Control.MousePosition;
Color color = GetColorAtScreenPoint(mousePos);
colorLabel.Text = $"坐标:({mousePos.X},{mousePos.Y}), RGB:({color.R},{color.G},{color.B})";
colorLabel.BackColor = color;
};
timer.Start();
}
}
第一种方法的实现过程很直接:创建一个 1×1 的Bitmap,通过Graphics把屏幕指定位置的像素复制过来,再使用GetPixel读取颜色值。第二种方法通过GetDC和GetPixel直接调用系统接口,虽然省去了临时位图的创建,但需要自行管理设备上下文释放。因此在一般场景中,使用Graphics.CopyFromScreen会更稳妥。
在位图上绘制圆形
如果要在位图上绘图,核心在于先从Bitmap创建Graphics对象。有了画布之后,就可以绘制圆形、矩形或其他图形。下面示例分别演示了两种常见做法:一种是在现有位图副本上画圆,另一种是创建新位图后再绘制。
using System.Drawing;
using System.Drawing.Drawing2D;
public class BitmapDrawer
{
// 方法:在现有位图上绘制一个圆
public static Bitmap DrawCircleOnBitmap(Bitmap originalBitmap, Point center, int radius, Color circleColor, int penWidth = 2)
{
// 创建原图的副本,避免修改原图 Bitmap resultBitmap = new Bitmap(originalBitmap);
using (Graphics g = Graphics.FromImage(resultBitmap))
{
// 设置绘图质量
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
// 创建画笔
using (Pen pen = new Pen(circleColor, penWidth))
{
// 计算圆的左上角坐标和直径 Rectangle rect = new Rectangle(center.X - radius, center.Y - radius, radius * 2, radius * 2);
// 绘制圆 g.DrawEllipse(pen, rect);
// 如果需要填充圆,使用 Brush
// using (SolidBrush brush = new SolidBrush(Color.FromArgb(50, circleColor)))
// {
// g.FillEllipse(brush, rect);
// }
}
}
return resultBitmap;
}
// 方法:创建一个新的位图并在上面画圆
public static Bitmap CreateBitmapWithCircle(int width, int height, Point center, int radius, Color backgroundColor, Color circleColor)
{
Bitmap bitmap = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(bitmap))
{
// 填充背景色 using (SolidBrush backgroundBrush = new SolidBrush(backgroundColor))
{
g.FillRectangle(backgroundBrush, 0, 0, width, height);
}
// 画圆
using (Pen pen = new Pen(circleColor, 3))
{
Rectangle rect = new Rectangle(center.X - radius, center.Y - radius, radius * 2, radius * 2);
g.DrawEllipse(pen, rect);
}
}
return bitmap;
}
}
这里使用的是DrawEllipse方法。当外接矩形是正方形时,绘制出来的就是圆。圆心位置由center控制,半径由radius决定。同时,启用SmoothingMode.AntiAlias可以让圆形边缘更平滑,减少锯齿感。
结合鼠标交互实现点击取色并画圆
将取色与绘图结合后,就可以做成一个简单的交互工具:在图片上点击某个位置,立即读取该像素的颜色,并在点击处绘制一个圆形标记。下面的 Windows Forms 示例展示了完整实现过程,适合直接测试和修改。
using System;
using System.Drawing;
using System.Windows.Forms;
namespace BitmapSimpleDemo
{
public partial class MainForm : Form
{
private Bitmap originalBitmap; // 原始位图 private Bitmap displayedBitmap; // 当前显示的位图(可能已画圆)
private Point lastClickPoint;
private int circleRadius = 30;
public MainForm()
{
InitializeComponent();
// 加载一张示例图片到PictureBox
originalBitmap = new Bitmap(@"你的图片路径.jpg");
pictureBox1.Image = originalBitmap;
displayedBitmap = (Bitmap)originalBitmap.Clone();
}
// PictureBox的鼠标点击事件 private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (originalBitmap == null) return;
lastClickPoint = e.Location;
// 1. 获取点击位置的颜色
Color clickedColor = originalBitmap.GetPixel(e.X, e.Y);
lblColorInfo.Text = $"点击点颜色 - R:{clickedColor.R} G:{clickedColor.G} B:{clickedColor.B}";
pnlColorPreview.BackColor = clickedColor;
// 2. 在 displayedBitmap 上以点击点为中心画圆
// 先重置为原图
displayedBitmap.Dispose();
displayedBitmap = (Bitmap)originalBitmap.Clone();
using (Graphics g = Graphics.FromImage(displayedBitmap))
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// 使用与取色形成对比的颜色画圆,例如取反色 Color drawColor = Color.FromArgb(255 - clickedColor.R, 255 - clickedColor.G, 255 - clickedColor.B);
using (Pen pen = new Pen(drawColor, 3))
{
Rectangle rect = new Rectangle(e.X - circleRadius, e.Y - circleRadius, circleRadius * 2, circleRadius * 2);
g.DrawEllipse(pen, rect);
}
}
// 3. 更新PictureBox显示 pictureBox1.Image = displayedBitmap;
}
// 按钮:保存画了圆的图片 private void btnSa ve_Click(object sender, EventArgs e)
{
if (displayedBitmap != null)
{
Sa veFileDialog sfd = new Sa veFileDialog();
sfd.Filter = "PNG Image|*.png|JPEG Image|*.jpg";
if (sfd.ShowDialog() == DialogResult.OK)
{
displayedBitmap.Sa ve(sfd.FileName);
}
}
}
// 按钮:重置图片,清除所有圆圈 private void btnReset_Click(object sender, EventArgs e)
{
displayedBitmap.Dispose();
displayedBitmap = (Bitmap)originalBitmap.Clone();
pictureBox1.Image = displayedBitmap;
lblColorInfo.Text = "颜色信息";
pnlColorPreview.BackColor = SystemColors.Control;
}
// 窗体关闭时释放资源
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
originalBitmap.Dispose();
displayedBitmap.Dispose();
}
}
}
该示例的处理流程比较清晰:用户点击图片后,程序先用GetPixel获取当前坐标的颜色值,再把结果显示到文本和颜色预览区域,随后在图片副本上以点击点为中心绘制一个圆形标记,最后刷新PictureBox显示内容。
其中一个关键点是,每次绘制之前都会将displayedBitmap恢复为原图副本,这样可以避免圆形标记重复叠加。如果需要保留所有历史点击标记,则可以直接在当前位图上继续绘制。示例中还使用了取反色作为圆圈颜色,以便在不同背景下保持较好的可见性。
关键操作总结
| 任务 | 核心类/方法 | 说明 |
|---|---|---|
| 获取屏幕坐标颜色 | Graphics.CopyFromScreen | 通过截取 1 像素屏幕区域实现,无需额外 API 声明,使用更安全。 |
| 获取位图像素颜色 | Bitmap.GetPixel(x, y) | 直接从Bitmap对象中读取指定坐标的颜色值。 |
| 在位图上绘图 | Graphics.FromImage(bitmap) | 先从位图创建Graphics画布,再调用DrawEllipse绘制圆形。 |
| 鼠标交互取色 | PictureBox.MouseClick事件 + e.Location | 在控件中获取鼠标相对坐标,再映射到位图像素位置。 |
| 资源管理 | using语句或手动Dispose() | Bitmap和Graphics都属于非托管资源,使用完成后需要及时释放。 |
在实际使用中,Bitmap和Graphics的资源释放不能忽略。无论是通过using语句自动清理,还是在适当位置调用Dispose(),都应保证这些对象在使用完成后及时释放,以避免不必要的资源占用问题。
实用工具推荐
绿化、压缩、噪点和虚拟水印,自由调节做旧程度。
选择画质和输出格式,预览压缩前后的大小。