RenderTargetBitmap

http://www.ericsink.com/wpf3d/3_Bitmap.html

 

 

3: RenderTargetBitmap

This entry is part 3 of a 12-part series on WPF 3D.

Saving a WPF 3D Scene to a Bitmap

WPF has some really handy classes in it.  Have you seen RenderTargetBitmap?  Basically, it's a simple way to capture a bitmap of any WPF Visual.  It works with 3D too:

RenderTargetBitmap bmp = new RenderTargetBitmap (
width, height, 96, 96, PixelFormats .Pbgra32);

bmp.Render(myViewport3D);

Wanna copy that image to the clipboard so you can paste it into some other application?

Clipboard .SetImage(bmp);

Wanna save that image to a PNG file?

PngBitmapEncoder png = new PngBitmapEncoder ();

png.Frames.Add(BitmapFrame .Create(bmp));

using (Stream stm = File .Create(filepath))

{

    png.Save(stm);

}

Nifty.  The pictures in this series of blog entries were rendered in just this fashion.  But there are a couple of details worth mentioning.

When using RenderTargetBitmap, you should probably erase it before you draw anything into it:

Rectangle vRect = new Rectangle ();

vRect.Width = width;

vRect.Height = height;

vRect.Fill = Brushes .White;

vRect.Arrange(new Rect (0, 0, vRect.Width, vRect.Height));

bmp.Render(visual);

If your Viewport3D was created offscreen, it's not ready to draw.  You need to give it a size (the same as the bitmap you're rendering into) and call Measure() and Arrange() to get it ready:

myViewport3D.Width = width;

myViewport3D.Height = height;

myViewport3D.Measure(new Size (width, height));

myViewport3D.Arrange(new Rect (0, 0, width, height));

Note that none of the above is specific to the 3D features of WPF.  RenderTargetBitmap just works with a Viewport3D like any other visual.  This is one of the best things about WPF:  The 3D features are not special or weird.  They're seamlessly integrated into the framework.

If you have experience programming with some other 3D API such as OpenGL or Direct3D (upon which WPF is built, by the way), you are probably accustomed to thinking of 3D stuff as very distinct from other stuff.  Simple things like getting a 3D graphic to appear in the same window next to a listbox can require all kinds of gymnastics.  WPF doesn't have those sorts of boundaries.  If you want to put an animated 3D scene as the graphic for a toolbar button, you can.

That level of integration is deeply neato. 

Just one more detail about RenderTargetBitmap, and this one is sort-of 3D-specific:

If your Viewport3D was created offscreen and you're using our friend ScreenSpaceLines3D, you'll need to make sure your lines get scaled at least once.  Since I hacked my copy of ScreenSpaceLines3D.cs to remove the use of CompositionTarget.Rendering, I simply call my Rescale() method on every instance just after I call Measure() and Arrange() on the offscreen Viewport3D.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF中,如果要将InkCanvas上的签名保存为透明背景,可以按照以下步骤进行操作: 1. 首先,创建一个新的BitmapImage对象来保存InkCanvas上的绘画内容。使用下面的代码创建BitmapImage对象: ```C# BitmapImage bitmap = new BitmapImage(); ``` 2. 将InkCanvas上的绘画内容转换为可渲染的Visual对象。使用下面的代码创建RenderTargetBitmap对象,并使用InkCanvas的宽度和高度进行实例化: ```C# RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)inkCanvas.ActualWidth, (int)inkCanvas.ActualHeight, 96, 96, PixelFormats.Pbgra32); ``` 3. 使用RenderTargetBitmapRender方法将InkCanvas的绘画内容渲染到RenderTargetBitmap中: ```C# renderTargetBitmap.Render(inkCanvas); ``` 4. 创建一个新的DrawingVisual对象,并使用RenderTargetBitmap的绘画内容创建一个新的DrawingContext: ```C# DrawingVisual drawingVisual = new DrawingVisual(); using (DrawingContext drawingContext = drawingVisual.RenderOpen()) { drawingContext.DrawImage(renderTargetBitmap, new Rect(0, 0, renderTargetBitmap.Width, renderTargetBitmap.Height)); } ``` 5. 创建一个新的RenderTargetBitmap,用于最终保存带有透明背景的图像: ```C# RenderTargetBitmap finalBitmap = new RenderTargetBitmap((int)inkCanvas.ActualWidth, (int)inkCanvas.ActualHeight, 96, 96, PixelFormats.Default); ``` 6. 使用RenderTargetBitmapRender方法将绘画内容渲染到finalBitmap中,并设置背景为透明: ```C# finalBitmap.Render(drawingVisual); finalBitmap.Freeze(); ``` 7. 最后,将finalBitmap保存为图片文件,通过使用PngBitmapEncoder或者JpegBitmapEncoder等其他编码器来保存图片。 通过以上步骤,可以将InkCanvas上的签名保存为带有透明背景的图片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值