您的位置:首页 >C#使用System.Drawing实现位图取色与绘图的方法
发布于2026-08-05 阅读(0)
扫一扫,手机访问
在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();
}
}
方法一的核心逻辑就四步:建一个1x1的Bitmap,拿Graphics对象从屏幕拷像素,然后调用GetPixel读出颜色。方法二走的是GetDC+GetPixel的API路线,虽然不需要生成临时位图,但得手动释放设备上下文,稍不注意容易内存泄漏。所以日常用的话,还是推荐方法一。
画图这事,离不开Graphics对象。只要从位图创建出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画圆,其实是画一个外接矩形为正方形的椭圆。圆的半径就是正方形边长的一半。另外,抗锯齿开关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 btnSave_Click(object sender, EventArgs e)
{
if (displayedBitmap != null)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "PNG Image|*.png|JPEG Image|*.jpg";
if (sfd.ShowDialog() == DialogResult.OK)
{
displayedBitmap.Save(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();
}
}
}
这个示例里有一个细节值得留意:每次点击画圆之前,都会先把displayedBitmap重置为原图的副本,这样就不会出现多个圆圈叠加在一起的情况。如果你想要保留历史标记,那就不需要重置,直接在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里清理。这个习惯养成了,后面写大项目才不会出现莫名其妙的内存暴涨。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8