本系列第一篇文章中创建的基本框架限定了印章形状为矩形,但常用的印章有方形、圆形等多种形状,本文调整程序以支持定义并显示矩形、圆角矩形、圆形、椭圆等4种形式的印章背景形状。
定义印章背景形状枚举类型,矩形、圆形、椭圆相关的尺寸能够根据印章宽度、高度计算,但圆角矩形需额外增加圆角半径尺寸,因此调整印章数据结构定义如下:
public enum BgType
{
Rect=0,
RoundRect=1,
Circle=2,
Oval=3
}
public class SealInfo
{
/// <summary>
/// 印章名称
/// </summary>
public string Name { get; set; }=string.Empty;
/// <summary>
/// 印章宽度
/// </summary>
public float Width { get; set; } = 0;
/// <summary>
/// 印章高度
/// </summary>
public float Height { get; set; } = 0;
/// <summary>
/// 尺寸单位类型,默认为毫米
/// </summary>
public UnitType UnitType { get; set; } = UnitType.Mm;
/// <summary>
/// 印章背景色,默认白色
/// </summary>
public SKColor BgColor { get; set; }=SKColors.White;
/// <summary>
/// 背景形状类型
/// </summary>
public BgType BgType { get; set; } = BgType.Rect;
/// <summary>
/// 圆角半径
/// </summary>
public float CornerRadius { get; set; } = 0;
/// <summary>
/// 是否有边框
/// </summary>
public bool HasBorder { get; set; } = false;
/// <summary>
/// 边框宽度
/// </summary>
public float BorderWidth { get; set; } = 1;
/// <summary>
/// 边框颜色
/// </summary>
public SKColor BorderColor { get;set; } = SKColors.Red;
}
虽然SKPaint的Style 属性可以设置同时绘制边框和底色(值定义为StrokeAndFill),但还没有在帮助文档或其它文档中看到边框和底色不同颜色时的设置和绘制方式,因此只能采用比较笨的方式,先绘制底色再绘制边框,同一个绘制函数调用两次,两次之间修改绘制方式和绘制颜色。采用SKPath对象保存不同类型的印章形状,最终调用canvas.DrawPath函数统一绘制底色和边框,主要代码如下:
Func<float, int, float> unitConverter = m_currSeal.UnitType == UnitType.Pixel ? CommonFunction.Pixel2Pixel : CommonFunction.MM2Pixel; ;
SKCanvas canvas = e.Surface.Canvas;
canvas.Clear();
SKPaint skPaint = new SKPaint();
skPaint.Style = SKPaintStyle.Fill;
skPaint.Color = m_currSeal.BgColor;
float borderWidth = unitConverter(m_currSeal.BorderWidth, skBoard.DeviceDpi);
SKRect rect = new SKRect(borderWidth / 2, borderWidth / 2, unitConverter(m_currSeal.Width, skBoard.DeviceDpi) - borderWidth / 2, unitConverter(m_currSeal.Height, skBoard.DeviceDpi) - borderWidth / 2);
SKPath path = new SKPath();
switch (m_currSeal.BgType)
{
case BgType.Rect:
path.AddRect(rect);
break;
case BgType.RoundRect:
path.AddRoundRect(new SKRoundRect(rect, unitConverter(m_currSeal.CornerRadius, skBoard.DeviceDpi)));
break;
case BgType.Circle:
path.AddCircle(rect.MidX, rect.MidY, Math.Min(rect.Height / 2, rect.Width / 2));
break;
case BgType.Oval:
path.AddOval(rect);
break;
}
canvas.DrawPath(path, skPaint);
if(m_currSeal.HasBorder)
{
skPaint.StrokeWidth = borderWidth;
skPaint.Style = SKPaintStyle.Stroke;
skPaint.Color = m_currSeal.BorderColor;
canvas.DrawPath(path, skPaint);
}
最后是程序运行效果,如下图所示:
参考文献:
[1]https://learn.microsoft.com/zh-cn/dotnet/api/skiasharp?view=skiasharp-2.88