C#: Save a canvas as an image

private void CreateSaveBitmap(Canvas canvas, string filename)
     {
       RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
        (int)canvas.Width, (int)canvas.Height,
        96d, 96d, PixelFormats.Pbgra32);
      // needed otherwise the image output is black
       canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
       canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));
 
renderBitmap.Render(canvas);
 
//JpegBitmapEncoder encoder = new JpegBitmapEncoder();
       PngBitmapEncoder encoder = new PngBitmapEncoder();
       encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
 
using (FileStream file = File.Create(filename))
       {
         encoder.Save(file);
       }
     }
CreateSaveBitmap(myCanvas, @"C:\temp\out.png");

 

上一篇:Gavin老师Transformer直播课感悟 - 基于Retrieval的具有Fine-grained架构的对话系统(二)


下一篇:分享 | 视觉无监督学习新范式:MAE