在c#开发中,生成自定义图片是非常常见的需求,不管是生成带个性化文字的头像、添加专属水印的商品图,还是动态生成验证码、制作数据可视化截图,都可以通过c#内置的GDI+相关类库实现。核心是利用System.Drawing命名空间下的相关类完成画布创建、图形绘制、文字渲染等操作。

环境准备与核心类说明
首先需要引入System.Drawing相关的依赖,在.NET Framework项目中该命名空间默认已包含,.NET Core及更高版本的项目需要手动安装System.Drawing.Common NuGet包。生成自定义图片涉及的核心类如下:
- Bitmap:表示位图对象,是最终生成的图片载体,可以指定图片的宽度、高度和像素格式
- Graphics:绘图表面类,所有绘制操作(画图形、写文字、贴图片)都需要通过该类的方法完成
- Brush:画笔基类,用于定义填充颜色或纹理,常用的有SolidBrush(纯色画笔)、HatchBrush(纹理画笔)等
- Font:字体类,定义文字的字体名称、大小和样式
- Pen:画笔类,用于绘制线条、边框等轮廓内容
基础自定义图片生成流程
生成自定义图片的通用流程可以分为四步:创建Bitmap对象作为画布、获取Graphics绘图对象、调用Graphics的方法绘制内容、保存或输出图片。下面是生成一个纯色背景加自定义文字的基础示例:
using System;
using System.Drawing;
namespace CustomImageDemo
{
class Program
{
static void Main(string[] args)
{
// 1. 创建Bitmap画布,指定宽度800,高度400,像素格式为32位ARGB
using (Bitmap bitmap = new Bitmap(800, 400, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
{
// 2. 获取Graphics绘图对象
using (Graphics graphics = Graphics.FromImage(bitmap))
{
// 设置绘图质量,避免文字和图形边缘模糊
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
// 3. 绘制自定义内容:先填充浅灰色背景
using (Brush bgBrush = new SolidBrush(Color.LightGray))
{
graphics.FillRectangle(bgBrush, 0, 0, bitmap.Width, bitmap.Height);
}
// 绘制自定义文字
using (Font titleFont = new Font("微软雅黑", 24, FontStyle.Bold))
using (Brush textBrush = new SolidBrush(Color.DarkBlue))
{
// 文字绘制位置,x=100,y=150
graphics.DrawString("这是自定义生成的图片", titleFont, textBrush, 100, 150);
}
// 绘制一个红色矩形边框
using (Pen borderPen = new Pen(Color.Red, 3))
{
graphics.DrawRectangle(borderPen, 50, 50, bitmap.Width - 100, bitmap.Height - 100);
}
}
// 4. 保存图片到本地
bitmap.Save("custom_image.png", System.Drawing.Imaging.ImageFormat.Png);
Console.WriteLine("自定义图片生成完成");
}
}
}
}
常见自定义图片场景实现
生成带水印的自定义图片
给现有图片添加自定义文字水印是常见需求,实现思路是先把原图加载为Bitmap,再获取其Graphics对象,在指定位置绘制半透明的文字水印:
using System;
using System.Drawing;
namespace WatermarkDemo
{
class Program
{
static void Main(string[] args)
{
// 原图路径,这里使用本地路径示例,实际可替换为业务中的图片路径
string sourceImagePath = "source.jpg";
string outputImagePath = "watermark_image.jpg";
using (Bitmap sourceBitmap = new Bitmap(sourceImagePath))
{
using (Graphics graphics = Graphics.FromImage(sourceBitmap))
{
// 设置水印文字字体
using (Font watermarkFont = new Font("微软雅黑", 20, FontStyle.Italic))
{
// 创建半透明的黑色画笔,alpha值128表示50%透明度
using (Brush watermarkBrush = new SolidBrush(Color.FromArgb(128, 0, 0, 0)))
{
// 水印绘制位置,放在图片右下角,留20像素边距
float x = sourceBitmap.Width - 200;
float y = sourceBitmap.Height - 50;
graphics.DrawString("我的专属水印", watermarkFont, watermarkBrush, x, y);
}
}
}
// 保存带水印的图片
sourceBitmap.Save(outputImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
Console.WriteLine("带水印的自定义图片生成完成");
}
}
}
}
生成动态验证码图片
验证码图片需要随机生成文字、随机背景和干扰线,同样是依托Graphics类的绘制方法实现:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace VerifyCodeDemo
{
class Program
{
static void Main(string[] args)
{
// 验证码文字,实际业务中可随机生成
string verifyCode = "A3B7";
int width = 120;
int height = 40;
using (Bitmap bitmap = new Bitmap(width, height))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.SmoothingMode = SmoothingMode.HighQuality;
// 填充随机背景色
Random random = new Random();
Color bgColor = Color.FromArgb(random.Next(200, 256), random.Next(200, 256), random.Next(200, 256));
graphics.Clear(bgColor);
// 绘制干扰线
for (int i = 0; i < 5; i++)
{
using (Pen linePen = new Pen(Color.FromArgb(random.Next(0, 200), random.Next(0, 200), random.Next(0, 200)), 1))
{
int x1 = random.Next(width);
int y1 = random.Next(height);
int x2 = random.Next(width);
int y2 = random.Next(height);
graphics.DrawLine(linePen, x1, y1, x2, y2);
}
}
// 绘制验证码文字
using (Font codeFont = new Font("Arial", 20, FontStyle.Bold))
{
for (int i = 0; i < verifyCode.Length; i++)
{
using (Brush textBrush = new SolidBrush(Color.FromArgb(random.Next(0, 150), random.Next(0, 150), random.Next(0, 150))))
{
// 每个字符位置随机偏移,增加识别难度
float x = 10 + i * 25 + random.Next(-5, 5);
float y = random.Next(5, 15);
graphics.DrawString(verifyCode[i].ToString(), codeFont, textBrush, x, y);
}
}
}
// 绘制干扰点
for (int i = 0; i < 100; i++)
{
int x = random.Next(width);
int y = random.Next(height);
bitmap.SetPixel(x, y, Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
}
}
bitmap.Save("verify_code.png", System.Drawing.Imaging.ImageFormat.Png);
Console.WriteLine("验证码自定义图片生成完成");
}
}
}
}
注意事项
- 使用完Bitmap、Graphics等实现了IDisposable接口的对象后,一定要用using语句或者手动调用Dispose方法释放资源,避免内存泄漏
- 如果生成的图片需要输出到HTTP响应中,不要调用Save方法保存到本地,而是调用
bitmap.Save(Response.OutputStream, ImageFormat.Png)直接输出 - 在Linux环境下使用System.Drawing.Common时,需要安装libgdiplus依赖,否则可能会出现绘图异常
- 绘制文字时要注意字体是否在运行环境中存在,避免使用不存在的字体导致文字显示异常,必要时可以嵌入字体文件
C#自定义图片生成System_DrawingGraphicsGDI+修改时间:2026-07-10 17:48:14