SkiaSharp 初探

有个 .Net Framework 4.x 的项目需要升级到 .Net 6,升级过后发现 System.Drawing.dll 库相关功能模块会报错:

Could not load file or assembly ‘System.Drawing.Common, Version=7.0.0.0, Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51’.系统找不到指定文件。

因为 System.Drawing 对跨平台不支持,只支持Windows系统,.Net 6 貌似不再支持。

于是使用 SkiaSharp 代替。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//BitmapSource flowImage;
//public BitmapSource FlowImage
//{
// get { return flowImage; }
// set { SetProperty(ref flowImage, value); }
//}

string flowImage;
public string FlowImage
{
get { return flowImage; }
set { SetProperty(ref flowImage, value); }
}

//void drawFlowImage(List<ZLTASKSTEP> stepLst, int curStep)
//{
//HashSet<string> stepParentNameHS = new HashSet<string>();
//for (int i = 0; i < stepLst.Count; i++)
//{
// stepParentNameHS.Add(stepLst[i].F_PARENT_STEP_NAME);
// if (stepLst[i].F_STEP_NUM == curStep)
// curStep = stepParentNameHS.Count;
//}
//string[] stepParentNameArr = stepParentNameHS.ToArray();
//Bitmap bitmap = new Bitmap((stepParentNameArr.Length+1) * L + L / 2, L * 10 / 9);
//using (Graphics g = Graphics.FromImage(bitmap))
//{
// StringFormat stringFormat = new StringFormat();
// stringFormat.Alignment = StringAlignment.Center;
// stringFormat.LineAlignment = StringAlignment.Center;
// Pen startGeomPen = new Pen(Color.FromArgb(140,182,40), 3);
// Point[] startPoints = getGeomPnts(1);
// g.FillPolygon(startGeomPen.Brush, startPoints);
// g.DrawPolygon(new Pen(Color.White, 5), startPoints);
// Pen startStringPen = new Pen(Color.White, 30);
// g.DrawString("任务开始", new Font("黑体", L / 90 * 100 / 62.5f * 5f),
// startStringPen.Brush, new PointF(L * 49 / 50.0f , L * 10 / 9 / 2), stringFormat);
// for (int i = 0; i < stepParentNameArr.Length; i++)
// {
// Pen geomPen = new Pen(i + 1 == curStep ? Color.FromArgb(50,130,170) :
// (i + 1 < curStep ? Color.FromArgb(140, 182, 40) : Color.Silver), 3);
// Point[] curPoints = getGeomPnts(i+2);
// g.FillPolygon(geomPen.Brush, curPoints);
// g.DrawPolygon(new Pen(Color.White, 6), curPoints);
// Pen stringPen = new Pen(Color.White, 30);
// g.DrawString(stepParentNameArr[i], new Font("黑体", L / 90 * 100 / 62.5f * 5f),
// stringPen.Brush, new PointF(L * 24 / 25.0f + L * (i+1), L * 10 / 9 / 2), stringFormat);
// }
//}
//FlowImage = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero,
// System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

//Bitmap bitmap = new Bitmap(stepLst.Count * L + L / 2, L * 10 / 9);
//using (Graphics g = Graphics.FromImage(bitmap))
//{
// StringFormat stringFormat = new StringFormat();
// stringFormat.Alignment = StringAlignment.Center;
// stringFormat.LineAlignment = StringAlignment.Center;
// for (int i = 0; i < stepLst.Count; i++)
// {
// Pen geomPen = new Pen(stepLst[i].F_STEP_NUM == curStep ? Color.FromArgb(50, 130, 170) :
// (stepLst[i].F_STEP_NUM < curStep ? Color.FromArgb(140, 182, 40) : Color.Silver), 3);
// Point[] curPoints = getGeomPnts(i + 1);
// g.FillPolygon(geomPen.Brush, curPoints);
// g.DrawPolygon(new Pen(Color.White, 6), curPoints);
// Pen stringPen = new Pen(Color.White, 30);
// g.DrawString(stepLst[i].F_STEP_NAME, new Font("黑体", L / 90 * 100 / 62.5f * 4.2f),
// stringPen.Brush, new PointF(L * 49 / 50.0f + L * i, L * 10 / 9 / 2), stringFormat);
// }
//}
//FlowImage = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero,
// System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
//}

void drawFlowImage(List<ZLTASKSTEP> stepLst, int curStep)
{
var imageInfo = new SKImageInfo(
width: stepLst.Count * L + L / 2,
height: L * 10 / 9,
colorType: SKColorType.Rgba8888,
alphaType: SKAlphaType.Premul);
var surface = SKSurface.Create(imageInfo);
var canvas = surface.Canvas;
SKColor sKColor_Done = new SKColor(140, 182, 40);
SKColor sKColor_Doing = new SKColor(50, 130, 170);
SKColor sKColor_NotDone = new SKColor(192, 192, 192);

for (int i = 0; i < stepLst.Count; i++)
{
SKColor sKColor_Cur;
if (stepLst[i].F_STEP_NUM == curStep)
sKColor_Cur = sKColor_Doing;
else
sKColor_Cur = stepLst[i].F_STEP_NUM < curStep ? sKColor_Done : sKColor_NotDone;
SKPaint sKPaint_Cur_Poly = new SKPaint()
{
Color = sKColor_Cur,
IsAntialias = true,
Style = SKPaintStyle.Fill
};
SKPaint sKPaint_Cur_Line = new SKPaint()
{
Color = new SKColor(255, 255, 255),
StrokeWidth = 3,
IsAntialias = true,
Style = SKPaintStyle.Stroke
};
SKPoint[] sKPoints_Cur = getGeomPnts(i + 1);
SKPath sKPath_Cur_Poly = new SKPath();
sKPath_Cur_Poly.AddPoly(sKPoints_Cur);
SKPath sKPath_Cur_Line = new SKPath();
sKPath_Cur_Line.AddPoly(sKPoints_Cur);
canvas.DrawPath(sKPath_Cur_Poly, sKPaint_Cur_Poly);
canvas.DrawPath(sKPath_Cur_Line, sKPaint_Cur_Line);
}
for (int i = 0; i < stepLst.Count; i++)
{
SKPaint sKPaint_Cur_Text = new SKPaint()
{
TextSize = 45.0f,
IsAntialias = true,
Color = new SKColor(255, 255, 255),
TextAlign = SKTextAlign.Center,
Style = SKPaintStyle.Fill,
TextEncoding = SKTextEncoding.Utf8,
Typeface = SKTypeface.FromFamilyName("宋体"),
FakeBoldText= true
};
float x = L * (3 / 2 + i)-10;
float y = 180;
canvas.DrawText(stepLst[i].F_STEP_NAME, x, y, sKPaint_Cur_Text);
}

using (SKImage image = surface.Snapshot())
using (SKData data = image.Encode())
{
try
{
//总是出问题
//// Define parameters used to create the BitmapSource.
//PixelFormat pf = PixelFormats.Bgr32;
//int width = stepLst.Count * L + L / 2;
//int height = L * 10 / 9;
//int rawStride = (height * pf.BitsPerPixel + pf.BitsPerPixel-1) / 8;
////int rawStride = data.ToArray().Length * 8 / height / pf.BitsPerPixel;
//// Create a BitmapSource.
//FlowImage = BitmapSource.Create(width, height,
// 96, 96, pf, null,
// data.ToArray(), rawStride);
//PixelFormat pixelFormat = PixelFormats.Gray8;
//int stride = (stepLst.Count * L + L / 2) * pixelFormat.BitsPerPixel / 8;

if (!Directory.Exists(Config.InitlizeConfig.FlowImgTempPath))
Directory.CreateDirectory(Config.InitlizeConfig.FlowImgTempPath);
string tempFlowImgFilePath = Config.InitlizeConfig.FlowImgTempPath + Guid.NewGuid() + ".png";
using (var stream = File.Create(tempFlowImgFilePath))
{
stream.Write(data.ToArray(), 0, data.ToArray().Length);
}
if (File.Exists(tempFlowImgFilePath))
FlowImage = tempFlowImgFilePath;
}
catch(Exception ex)
{
MessageDialogHelper.ShowErrorDialogOK(ex.Message);
}
}
}

//Point[] getGeomPnts(int x)
//{
// return new Point[] {
// new Point(0 + L * (x - 1), 0),
// new Point(L + L * (x - 1), 0),
// new Point(L * 3 / 2 + L * (x - 1), L*10/9/2),
// new Point(L + L * (x - 1), L*10/9),
// new Point(0 + L * (x - 1), L*10/9),
// new Point(L/2 * (x - 1 == 0 ? 0 : 1) + L * (x - 1), L*10/9/2),
// new Point(0 + L * (x - 1), 0)
// };
//}

SKPoint[] getGeomPnts(int x)
{
return new SKPoint[] {
new SKPoint(0 + L * (x - 1), 0),
new SKPoint(L + L * (x - 1), 0),
new SKPoint(L * 3 / 2 + L * (x - 1), L*10/9/2),
new SKPoint(L + L * (x - 1), L*10/9),
new SKPoint(0 + L * (x - 1), L*10/9),
new SKPoint(L/2 * (x - 1 == 0 ? 0 : 1) + L * (x - 1), L*10/9/2),
new SKPoint(0 + L * (x - 1), 0)
};
}

官方文档:SkiaSharp Namespace | Microsoft Learn
-2.88)