Net Core教程

C#: Save a canvas as an image

本文主要是介绍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");

 

这篇关于C#: Save a canvas as an image的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!