C#使用System.Drawing实现位图取色与绘图的方法
作者:吴可可123
想用C#实现鼠标取色和绘图吗,本文带你掌握System.Drawing核心操作:通过CopyFromScreen获取屏幕颜色、用Graphics在Bitmap上画圆,并演示鼠标交互取色绘图的完整示例,帮你轻松搞定位图处理,需要的朋友可以参考下
在C#中,使用System.Drawing命名空间可以简单处理位图,实现取色和绘图。以下是核心操作的代码示例。
1. 提取鼠标指定点的颜色值
要获取屏幕上任意一点的颜色,核心是使用Graphics.CopyFromScreen方法截取屏幕,然后从截取的位图中读取像素颜色 。
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();
}
}2. 在位图上画圆
在位图上绘图,需要使用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;
}
}
3. 结合鼠标交互:点击取色并画圆
以下是一个完整的Windows Forms示例,实现点击图片框(PictureBox)获取该点颜色,并以该点为中心画圆。
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();
}
}
}
关键操作总结:
| 任务 | 核心类/方法 | 说明 |
|---|---|---|
| 获取屏幕坐标颜色 | Graphics.CopyFromScreen | 通过截取1像素屏幕区域实现,无需API声明,更安全 。 |
| 获取位图像素颜色 | Bitmap.GetPixel(x, y) | 直接从Bitmap对象中读取指定坐标的颜色。 |
| 在位图上绘图 | Graphics.FromImage(bitmap) | 从位图创建Graphics画布,然后使用DrawEllipse画圆 。 |
| 鼠标交互取色 | PictureBox.MouseClick事件 + e.Location | 在控件内获取鼠标相对坐标,再对应到位图像素。 |
| 资源管理 | using语句或手动Dispose() | Bitmap和Graphics是非托管资源,使用后必须释放。 |
到此这篇关于C#使用System.Drawing实现位图取色与绘图的方法的文章就介绍到这了,更多相关C#位图取色与绘图内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
