SKBitmap bmp = new SKBitmap(80, 30);
using (SKCanvas canvas = new SKCanvas(bmp))
{
//背景色
canvas.DrawColor(SKColors.White);
using (SKPaint sKPaint = new SKPaint())
{
sKPaint.TextSize = 25;//字体大小
sKPaint.IsAntialias = true;//开启抗锯齿
sKPaint.Typeface = SKTypeface.FromFamilyName("Cantarell", 16, 16, SKFontStyleSlant.Italic);//字体
SKRect size = new SKRect();
sKPaint.MeasureText(zu[0].ToString(), ref size);//计算文字宽度以及高度
float temp = (bmp.Width / 4 - size.Size.Width) / 2;
float temp1 = bmp.Height - (bmp.Height - size.Size.Height) / 2;
Random random = new Random();
for (int i = 0; i < 4; i++)
{
sKPaint.Color = new SKColor((byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255));
canvas.DrawText(zu[i].ToString(), temp + 20 * i, temp1, sKPaint);//画文字
}
//干扰线
for (int i = 0; i < 5; i++)
{
sKPaint.Color = new SKColor((byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255));
canvas.DrawLine(random.Next(0, 40), random.Next(1, 29), random.Next(41, 80), random.Next(1, 29), sKPaint);
}
}
//页面展示图片
using (SKImage img = SKImage.FromBitmap(bmp))
{
using (SKData p = img.Encode())
{
return OK(new SkCode() { file = File(p.ToArray(), "image/Png"), cid = cid });
}
}
这个是参考代码,问题出在
sKPaint.Typeface = SKTypeface.FromFamilyName("Cantarell", 16, 16, SKFontStyleSlant.Italic);//字体
如果是windows 下面,字体换微软雅黑,能显示,如果是linux下面,需要换成linux下面的字体,否则画不出来。
也不抛异常,让我足足搞了一个晚上。。。