RenderTargetBitmap缺陷
2013-12-26 18:55
211 查看
1)渲染范围不正确
利用RenderTargetBitmap.Render渲染一个Canvas对象的图像,如果Canvas被一个Border包含则没有问题,但如果这个Canvas没有被Border所包含,那渲染出来的图像范围会有问题。
有Border截图:
无Border截图:
2)渲染速度慢
相对于GDI+而言,通过WPF提供的RenderTargetBitmap.Render函数渲染一个Visual对象获得图像的方式显得过于耗时。如果做软件界面截屏功能还可以接受,但如果是连续截屏制作视频文件,则很难接受。
输出结果:
耗时 19ms for 509x291像素
耗时 29ms for 670x401像素
耗时 24ms for 813x481像素
耗时 38ms for 964x626像素
耗时 42ms for 1,150x714像素
耗时 39ms for 1,280x941像素
额,这次的测试结果还算不错,但在某台机器上测试像素较小时也是20ms左右,但1000x700像素的耗时居然要170+ms。。。
利用RenderTargetBitmap.Render渲染一个Canvas对象的图像,如果Canvas被一个Border包含则没有问题,但如果这个Canvas没有被Border所包含,那渲染出来的图像范围会有问题。
private void miScreenshot_Click(object sender, RoutedEventArgs e) { FrameworkElement visual = myChart as FrameworkElement; RenderTargetBitmap bmp = new RenderTargetBitmap((int)visual.ActualWidth, (int)visual.ActualHeight , 96, 96, PixelFormats.Pbgra32); bmp.Render(visual); BmpBitmapEncoder encoder = new BmpBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bmp)); SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "图像文件 (*.bmp)|*.bmp"; if (dlg.ShowDialog().Value) { using (Stream stream = File.Create(dlg.FileName)) { encoder.Save(stream); } if(File.Exists(dlg.FileName)) { Process.Start(dlg.FileName); } } }
<Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Menu Grid.Row="0"> <MenuItem x:Name="miViews" Header="视图演示"> <!--视图DEMO--> </MenuItem> <MenuItem Header="设置"> <MenuItem x:Name="miOverlap" Header="视图叠加" IsCheckable="True"/> <Separator/> <MenuItem Header="截图" Click="miScreenshot_Click"/> </MenuItem> </Menu> <Border Grid.Row="1"> <Canvas x:Name="myChart"/> </Border> <!--渲染范围不对 <Canvas x:Name="myChart" Grid.Row="1"/> --> </Grid>
有Border截图:
无Border截图:
2)渲染速度慢
相对于GDI+而言,通过WPF提供的RenderTargetBitmap.Render函数渲染一个Visual对象获得图像的方式显得过于耗时。如果做软件界面截屏功能还可以接受,但如果是连续截屏制作视频文件,则很难接受。
Stopwatch sw = new Stopwatch(); private void miScreenshot_Click(object sender, RoutedEventArgs e) { FrameworkElement visual = myChart as FrameworkElement; RenderTargetBitmap bmp = new RenderTargetBitmap((int)visual.ActualWidth - 1, (int)visual.ActualHeight - 1 , 96, 96, PixelFormats.Pbgra32); sw.Restart(); bmp.Render(visual); sw.Stop(); Console.WriteLine("耗时 {0}ms for {1:N0}x{2:N0}像素", sw.ElapsedMilliseconds, visual.ActualWidth, visual.ActualHeight); BmpBitmapEncoder encoder = new BmpBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bmp)); SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "图像文件 (*.bmp)|*.bmp"; if (dlg.ShowDialog().Value) { using (Stream stream = File.Create(dlg.FileName)) { encoder.Save(stream); } if(File.Exists(dlg.FileName)) { Process.Start(dlg.FileName); } } }
输出结果:
耗时 19ms for 509x291像素
耗时 29ms for 670x401像素
耗时 24ms for 813x481像素
耗时 38ms for 964x626像素
耗时 42ms for 1,150x714像素
耗时 39ms for 1,280x941像素
额,这次的测试结果还算不错,但在某台机器上测试像素较小时也是20ms左右,但1000x700像素的耗时居然要170+ms。。。
相关文章推荐
- RenderTargetBitmap把visual转换为图像
- wpf 利用 RenderTargetBitmap把控件保存为图片
- RenderTargetBitmap
- Problem of saving images in WPF (RenderTargetBitmap)zz
- 【WP8.1开发】RenderTargetBitmap类的特殊用途
- android studio问题rendering problems no render target selected
- Ogre1.8源码剖析之OgreRenderTarget(一)
- [ZZ] 景深效果(Depth of Field) , Pass1 将场景渲染到一个RenderTarget,做为清晰版, Pass2: BluredRT , Pass3: WDepth = Depth / Far_Z_Clip
- Android传递Bitmap的两种简单方式及其缺陷
- 介绍RenderTexture,RenderSurface和SetRenderTarget
- 什么是渲染目标(render target)&& 渲染到纹理(Render To Texture, RTT)详解
- sample a texture as a rendertarget
- bitmap占用的内存: imgWidth*(targetDen/imgDen) * imgHeight(targetDen/imgDen) * imgPixByte
- three.js(七) 绘制到WebGLRenderTarget
- D3D9下的获得RenderTarget有2种方法
- 【什么是渲染目标(render target)】
- 关于android示例程序(bitmapfun)——高效加载图片的缺陷
- IDirect3DDevice9::GetRenderTargetData
- 浅谈 Glide - BitmapPool 的存储时机 & 解答 ViewTarget 在同一View显示不同的图片时,总用同一个 Bitmap 引用的原因
- IDirect3DDevice9::GetRenderTargetData