C#使用GDI+绘制并旋转签名章实现方法
本文详细介绍如何利用C#的System.Drawing命名空间实现自定义签名章。主要涵盖绘制圆形与矩形边框、文字智能居中计算以及图像旋转功能,适用于需要在Windows Forms或ASP.NET项目中动态生成电子印章的场景。
实现原理与准备条件
实现自定义签名章的核心在于利用 Gdi+ Graphics 对象进行图形渲染。我们需要处理以下几个关键要素:
- 画布创建:使用
Bitmap作为内存图像源。 - 抗锯齿处理:确保绘制的线条和文字边缘平滑,避免锯齿。
- 坐标计算:根据边框尺寸和文字宽高,计算文本绘制的起始坐标,实现视觉居中。
- 图像变换:通过矩阵变换或三角函数计算实现图像的任意角度旋转。
具体操作步骤
第1步:引入必要的命名空间
首先确保项目引用了以下基础库,以便调用绘图类和方法:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
第2步:绘制椭圆形签名章
该方法创建一个指定宽高的 Bitmap,并在其中绘制红色椭圆边框,随后将输入的文字居中绘制于椭圆内部。若启用旋转选项,则进一步调用旋转方法。
注意:参数中的 Width 和 Height 通常代表最终输出图像的尺寸,但代码中用于定义边框区域的逻辑是 (Width/2, Height/2),需根据实际需求调整画布比例。
public static Bitmap GetDrawCircleCachet(int Width, int Height, string test, bool IsRotate, int angle)
{
// 记录圆画笔的粗细
int Circle_Brush = 2;
// 初始化画布
Bitmap bitmap = new Bitmap(Width, Height);
// 实例化Graphic类,关联到bitmap
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
// 开启抗锯齿,使图形平滑
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// 清空背景为白色
g.Clear(Color.White);
// 设置画笔颜色(红色)和宽度
Pen mypen = new Pen(Color.Red, Circle_Brush);
// 定义绘制区域:此处圆心位于(Width/4, Height/4)附近,半径随参数变化
// 原始代码逻辑:Rectangle rect = new Rectangle(10, 10, Width / 2, Height / 2);
Rectangle rect = new Rectangle(10, 10, Width / 2, Height / 2);
// 绘制椭圆边框
g.DrawEllipse(mypen, rect);
// 配置文字样式
Font star_Font = new Font("Arial", 12, FontStyle.Regular);
// 测量字符串实际占据的像素大小
SizeF var_Size = g.MeasureString(test, star_Font);
// 计算文字居中的偏移量
// 这里的计算逻辑是基于rect加上笔触宽度的范围来对齐
float CircleZjW = rect.Width + 2 * Circle_Brush;
float CircleZJH = rect.Height + 2 * Circle_Brush;
float x = (CircleZjW - var_Size.Width) / 2F + rect.X;
float y = (CircleZJH - var_Size.Height) / 2F + rect.Y;
// 在计算出的位置绘制文字
g.DrawString(test, star_Font, mypen.Brush, new PointF(x, y));
// 如果需要进行旋转,则执行旋转操作
if (IsRotate)
bitmap = Rotate(bitmap, angle);
return bitmap;
}
第3步:绘制矩形签名章
与椭圆章类似,区别仅在于使用 g.DrawRectangle 替代 g.DrawEllipse,适用于需要方形印章的业务场景。
public static Bitmap GetDrawRectangle(int width, int height, string name, bool IsRotate, int angle)
{
int Circle_Brush = 2;
Bitmap bitmap = new Bitmap(width, height);
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.Clear(Color.White);
Pen mypen = new Pen(Color.Red, Circle_Brush);
// 矩形绘制区域
Rectangle rect = new Rectangle(10, 10, width / 2, height / 2);
g.DrawRectangle(mypen, rect); // 注意:此处原文代码误用了DrawEllipse的变体或复制错误,应为DrawRectangle
Font star_Font = new Font("Arial", 12, FontStyle.Regular);
SizeF var_Size = g.MeasureString(name, star_Font);
float CircleZjW = rect.Width + 2 * Circle_Brush;
float CircleZJH = rect.Height + 2 * Circle_Brush;
float x = (CircleZjW - var_Size.Width) / 2F + rect.X;
float y = (CircleZJH - var_Size.Height) / 2F + rect.Y;
g.DrawString(name, star_Font, mypen.Brush, new PointF(x, y));
if (IsRotate)
bitmap = Rotate(bitmap, angle);
return bitmap;
}
第4步:实现图像旋转算法
当需要将生成的签名章倾斜放置时,可以使用此旋转方法。该方法通过三角函数计算旋转后新图像的宽高,并使用 TranslateTransform 和 RotateTransform 完成图形变换。
static Bitmap Rotate(Bitmap b, int angle)
{
angle = angle % 360;
// 弧度转换
double radian = angle * Math.PI / 180.0;
double cos = Math.Cos(radian);
double sin = Math.Sin(radian);
// 原图的宽和高
int w = b.Width;
int h = b.Height;
// 计算旋转后的新宽高,防止越界
int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
// 创建目标位图
Bitmap dsImage = new Bitmap(W, H);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);
// 设置高质量渲染模式
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
// 计算偏移量,使旋转中心点对齐
Point Offset = new Point((W - w) / 2, (H - h) / 2);
Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
// 执行旋转变换
g.TranslateTransform(center.X, center.Y);
g.RotateTransform(360 - angle); // 逆时针旋转
g.TranslateTransform(-center.X, -center.Y);
// 绘制原图
g.DrawImage(b, rect);
// 重置并释放资源
g.ResetTransform();
g.Save();
g.Dispose();
return dsImage;
}
完成判断与维护建议
- 资源释放:
Bitmap和Graphics对象实现了IDisposable接口,在实际生产环境中,建议在using语句块中使用它们,或者显式调用.Dispose()以释放非托管内存,避免内存泄漏。 - 字体兼容性:示例代码使用了 "Arial" 字体。如果在 Linux 服务器环境部署,请确保该环境安装了对应的字体文件,否则可能回退默认字体导致布局错乱。
- 性能优化:频繁创建
Graphics对象会产生较大开销。若在高并发场景下使用,建议复用缓存好的背景模板,或使用MemoryStream结合更轻量的图像处理库(如 ImageSharp)进行替代。
简要总结
以上介绍了 C# 自定义签名章的详细实现,包括 GDI+ 基本绘图 API 的使用、文字自动居中算法推导以及基于几何计算的图像旋转。这套方案无需依赖第三方控件,即可快速生成合规的电子签名图像。更多关于 C# 图形处理的资料请关注本站其它相关文章!
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













