SkiaSharp

 

 [HttpGet]
 [NonUnify]
 public IActionResult AvatarTest()
 {
     // info 为你的画布大小 例如 with=750 hight=1024
     var info = new SKImageInfo(750, 1024);
     // create the surface using the information
     //使用信息创建表面
     var surface = SKSurface.Create(info);

     //初始化画布
     var canvas = surface.Canvas;

     //载入底图
     using var bmp = SKBitmap.Decode("C:\\Users\\Administrator\\source\\repos\\FurionProject\\FurionProject.Web.Entry\\img\\mb.jpg");
     // create a paint object so that drawing can happen at a higher resolution
     //创建一个绘制对象,以便绘制可以以更高的分辨率进行
     using (var paint = new SKPaint())
     {
         paint.IsAntialias = true;
         paint.FilterQuality = SKFilterQuality.High;
         //把图绘制在画布上
         canvas.DrawBitmap(bmp, info.Rect, paint);
     }
     //canvas.Save();

     using (var avatarImage = SKImage.FromBitmap(SKBitmap.Decode("C:\\Users\\Administrator\\source\\repos\\FurionProject\\FurionProject.Web.Entry\\img\\gz.png")))
     {
         // 35,50是你图片的坐标 注意:不要超出画布的大小,否则无法显示 300 300是图片的大小
         var rect = SKRect.Create(35, 50, 300, 300);
         //把图绘制在画布上
         canvas.DrawImage(avatarImage, rect);
     }
     //恢复画布状态为全图绘制状态,在画布上绘制后填写
     //canvas.Restore();


     //创建一个绘制对象,以便绘制可以以更高的分辨率进行
     using (var paint = new SKPaint())
     {
         //字体
         paint.Typeface = SKTypeface.FromFamilyName(
        "SimHei",
        SKFontStyleWeight.Normal,
        SKFontStyleWidth.Normal,
        SKFontStyleSlant.Upright);
         //字体大小
         paint.TextSize = 40;
         //字体颜色
         paint.Color = SKColor.Parse("#333333");
         //抗锯齿不能少
         paint.IsAntialias = true;
         //把文字绘制在画布上
         canvas.DrawSingleLineText("我是文字我是文字我是文字我是1", 30, 50, paint);
         canvas.DrawSingleLineText("我是文字我是文字我是文字我是2", 50, 100, paint);
     }
     //canvas.Restore();


     //最后把你画的图片保存到本地
     var image = surface.Snapshot();
     // encode the image as a PNG+
     var data = image.Encode(SKEncodedImageFormat.Png, 100);

     //string filname = "ss.png";
     //using (var stream = File.OpenWrite(filname))
     //{
     //    data.SaveTo(stream);
     //}

     var arrByte = data.ToArray();


     return new FileContentResult(arrByte, "image/jpeg");
 }

 

 

转:.NET Core使用canvas的常用方法 SKPath SKPaint SKRect等-蒲公英云 (dandelioncloud.cn)

posted @ 2024-01-13 22:28  ucdos2023  阅读(44)  评论(0编辑  收藏  举报