1 public static void ExportToPng(this Canvas canvas,Uri path) 2 { 3 if(path==null) 4 { 5 return; 6 } 7 Transform transform =canvas.LayoutTransform; 8 canvas.LayoutTransform=null; 9 Size size=new Size(900,600); 10 canvas.Measure(size); 11 canvas.Arrange(new Rect(size)); 12 RenderTargetBitmap renderTargetBitmap=new RenderTargetBitmap((int)size.Width,(int)size.Height,96d,96d,PixelFormats.Pbgra32); 13 renderTargetBitmap.Render(canvas); 14 using(FileStream outstream=new FileStream(path.LocalPath,FileMode.Create)) 15 { 16 PngBitmapEncoder encoder=new PngBitmapEncoder(); 17 encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap)); 18 encoder.Save(outstream); 19 } 20 canvas.LayoutTransform; 21 }
上述代码将ExportToPng方法写成Canvas类的扩展方法形式,方便调用。
标签:Canvas,LayoutTransform,renderTargetBitmap,canvas,new,WPF,png,size From: https://www.cnblogs.com/follow-discoverer/p/17359916.html