标签:ack using 效果 actual jpg 设置 extension elf system
使用 RenderTargetBitmap 的注意事项:
要渲染的Canvas元素要放在Border元素内,并且此Border元素不能设置边框宽度(BorderThickness),不然生成的位图会出现错误。如果需要边框效果,可再加一层Border。
例:
cs:
RenderTargetBitmap bmp = new RenderTargetBitmap((int)canvas.ActualWidth, (int)canvas.ActualHeight, 96, 96, PixelFormats.Pbgra32); bmp.Render(canvas); // string file = @"D:\xxx.jpg"; string Extension = System.IO.Path.GetExtension(file).ToLower(); BitmapEncoder encoder = new JpegBitmapEncoder(); //可设置其它格式的位图编码器 encoder.Frames.Add(BitmapFrame.Create(bmp)); using (Stream stm = File.Create(file)) { encoder.Save(stm); }
xaml:
<Border BorderBrush="Black" BorderThickness="10" Width="300" Height="300" Grid.Row="1"> <Border> <Canvas x:Name="canvas" Background="White"> <Image Source="96x96-2.jpg" Canvas.Left="96" Canvas.Top="96"></Image> </Canvas> </Border> </Border>
WPF Canvas转换为位图 (RenderTargetBitmap)
标签:ack using 效果 actual jpg 设置 extension elf system
原文地址:http://www.cnblogs.com/gmcn/p/6691296.html