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
| byte[] Generate<T>(T book)
{
int width = 480;
int height = 540;
int marginY = -10;
int marginX = -10;
string mainText =Faker.Name.First(); //book.Title;
string subText = Faker.Name.Last();
string backGroundColor =listOfColor[Faker.RandomNumber.Next(0,listOfColor.Count()-1)].ToString();
string textColor = "#ffffff";
string boderColor = "#ffffff";
SKBitmap bmp = new(width, height);
SKCanvas canvas = new(bmp);
canvas.Clear(SKColor.Parse(backGroundColor));
using (var paint = new SKPaint())
{
paint.TextSize = width/ 10.0f;
paint.IsAntialias = true;
paint.Color = SKColor.Parse(textColor);
paint.IsStroke = false;
paint.StrokeWidth = 3;
paint.TextAlign = SKTextAlign.Center;
canvas.DrawText(mainText, width / 2f, height / 2f, paint);
paint.TextSize = width/ 25.0f;
paint.TextAlign = SKTextAlign.Right;
canvas.DrawText(subText, width+marginX, height+marginY, paint);
paint.TextSize = width/ 20.0f;
paint.IsStroke = true;
paint.TextAlign = SKTextAlign.Center;
paint.Color = SKColor.Parse(textColor);
}
//SKFileWStream fs = new($"Images/{book.Title}.jpg");
//bmp.Encode(fs, SKEncodedImageFormat.Jpeg, quality: 50);
bmp.Encode(SKEncodedImageFormat.Jpeg,100);
using (MemoryStream ms = new MemoryStream())
{
bmp.Encode(ms, SKEncodedImageFormat.Jpeg, 100);
return ms.ToArray();
}
return bmp.Bytes;
}
|