System.Drawing
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 |
public async Task<IActionResult> CreateImage() { using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(500, 80)) { using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap)) { //背景描画 using (SolidBrush solidBrush = new SolidBrush(Color.White)) { //graphics.FillRectangle(solidBrush, new Rectangle(0, 0, 200, 200)); graphics.FillRectangle(solidBrush, graphics.VisibleClipBounds); } // 図形描画 using (SolidBrush solidBrush = new SolidBrush(Color.Blue)) { graphics.DrawPie(Pens.Red, 0, 0, 80, 80, 300, 300); } // 文字描画 string serial = "管理番号:0001"; using (System.Drawing.Font font = new System.Drawing.Font("メイリオ", 36)) using (SolidBrush solidBrush = new SolidBrush(Color.Black)) { graphics.TextRenderingHint = TextRenderingHint.AntiAlias; graphics.DrawString(serial, font, solidBrush, new System.Drawing.Point(0, 0)); } // Controllerで実行する場合(ASP.NET) var ms = new MemoryStream(); bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png); ms.Position = 0; return new FileStreamResult(ms, "image/png"); // PictureBoxの場合 // PictureBox1.Image = bitmap; } } } |
QRコードの場合(ZXing.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 |
public async Task<IActionResult> CreateImage(string? id) { using System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(500, 70); using System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap); using (SolidBrush solidBrush = new SolidBrush(Color.White)) { graphics.FillRectangle(solidBrush, graphics.VisibleClipBounds); } var writer = new BarcodeWriter { Format = ZXing.BarcodeFormat.QR_CODE, Options = new ZXing.Common.EncodingOptions { Width = 200, Height = 200, Margin = 2, } }; var ms = new MemoryStream(); var bmp = writer.Write("abcabc"); bmp.Encode(ms, SKEncodedImageFormat.Png, 100); ms.Position = 0; return new FileStreamResult(ms, "image/png"); } |
SkiaSharp+ZXing.SkiaSharpを利用し1つの画像として生成
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 |
public async Task<IActionResult> CreateImage(string? id) { using SkiaSharp.SKBitmap bitmap = new SkiaSharp.SKBitmap(500, 70); using SkiaSharp.SKCanvas canvas = new SkiaSharp.SKCanvas(bitmap); //背景描画1 canvas.Clear(SKColors.White); //背景描画2 SkiaSharp.SKPaint paint1 = new SkiaSharp.SKPaint(); paint1.Style = SkiaSharp.SKPaintStyle.Fill; paint1.Color = SkiaSharp.SKColors.Blue; //canvas.DrawRect (canvas.LocalClipBounds, paint1); // 文字描画 SkiaSharp.SKPaint paint2 = new SkiaSharp.SKPaint(); paint2.Style = SkiaSharp.SKPaintStyle.Fill; paint2.Color = SkiaSharp.SKColors.Black ; paint2.TextSize = 36; paint2.Typeface = SkiaSharp.SKTypeface.FromFamilyName("Yu Gothic UI"); paint2.IsAntialias = true; canvas.DrawText("管理番号:" + id, 70, 48, paint2); //QRコード描画 var writer = new BarcodeWriter { Format = ZXing.BarcodeFormat.QR_CODE, Options = new ZXing.Common.EncodingOptions { Width = 70, Height = 70, Margin = 0, } }; var ms = new MemoryStream(); var qr = writer.Write(Url.Content("/TestTable33/Details/" + id)); canvas.DrawImage(SkiaSharp.SKImage.FromBitmap(qr), new SkiaSharp.SKPoint(0,0)); bitmap.Encode(ms, SKEncodedImageFormat.Png, 100); ms.Position = 0; return new FileStreamResult(ms, "image/png"); } |