将 RenderTargetBitmap 与 Viewport3D 结合使用

发布于 2024-09-11 09:55:24 字数 2113 浏览 4 评论 0原文

我使用 Viewport3d 并使用继承的 UIElement3D 类显示我的 3D 元素...现在我想制作视口的不同图像(不同的视图)。这意味着我必须在运行时创建一个新的 viewport3d 并为其上的图像应用我的特定视图。

请参阅以下代码:

' Start creating Image
Dim bitmap As New RenderTargetBitmap(800,
                                     800,
                                     96,
                                     96,
                                     PixelFormats.Default)

'--------------------
'Render xamlViewport works perfect
bitmap.Render(xamlViewport)

Using stream As FileStream = New FileStream("C:\\temp\\test_xamlviewport.png", FileMode.Create)
  Dim encoder As New PngBitmapEncoder
  encoder.Frames.Add(BitmapFrame.Create(bitmap))
  encoder.Save(stream)
End Using


'---------------------
'Render local Viewport don't work
Dim oViewport As New Viewport3D

oViewport.Height = 800
oViewport.Width = 800

oViewport.Children.Add(New C3DCuboid())
oViewport.Camera = New PerspectiveCamera(New Point3D(4, 7, 6), New Vector3D(-4, -7, -6), New Vector3D(0, 1, 0), 45)

Dim oVisual As New ModelVisual3D
oVisual.Content = New DirectionalLight(Colors.White, New Vector3D(-1, -2, -3))

oViewport.Children.Add(oVisual)
oViewport.Measure(New Size(800, 800))
oViewport.Arrange(New Rect(New Size(800, 800)))

bitmap = New RenderTargetBitmap(800,
                                 800,
                                 96,
                                 96,
                                 PixelFormats.Default)

bitmap.Render(oViewport)

Using stream As FileStream = New FileStream("C:\\temp\\test_localviewport.png", FileMode.Create)
  Dim encoder As New PngBitmapEncoder
  encoder.Frames.Add(BitmapFrame.Create(bitmap))
  encoder.Save(stream)
End Using

第一张图片 (test_xamlviewport.png) 显示正确,但第二张图片是我创建自己的 Viewport3D对象并打印它是空的(test_localviewport.png)。

如何强制渲染 UIElement3D 对象,或者为什么在创建新视口与打印现有 XAML 视口实例时它不起作用?

在这里您可以找到重现我的问题的示例解决方案 http://cid-df67ca3f85229bd1.office.live.com/ self.aspx/Development/WpfApplication2.zip

问候 罗兰

I use Viewport3d and display my 3D Elements with inherited UIElement3D classes... Now I want to make different images of my viewport (different views). That means I have to make a new viewport3d at runtime and apply my specific view for the image on it..

see the following code:

' Start creating Image
Dim bitmap As New RenderTargetBitmap(800,
                                     800,
                                     96,
                                     96,
                                     PixelFormats.Default)

'--------------------
'Render xamlViewport works perfect
bitmap.Render(xamlViewport)

Using stream As FileStream = New FileStream("C:\\temp\\test_xamlviewport.png", FileMode.Create)
  Dim encoder As New PngBitmapEncoder
  encoder.Frames.Add(BitmapFrame.Create(bitmap))
  encoder.Save(stream)
End Using


'---------------------
'Render local Viewport don't work
Dim oViewport As New Viewport3D

oViewport.Height = 800
oViewport.Width = 800

oViewport.Children.Add(New C3DCuboid())
oViewport.Camera = New PerspectiveCamera(New Point3D(4, 7, 6), New Vector3D(-4, -7, -6), New Vector3D(0, 1, 0), 45)

Dim oVisual As New ModelVisual3D
oVisual.Content = New DirectionalLight(Colors.White, New Vector3D(-1, -2, -3))

oViewport.Children.Add(oVisual)
oViewport.Measure(New Size(800, 800))
oViewport.Arrange(New Rect(New Size(800, 800)))

bitmap = New RenderTargetBitmap(800,
                                 800,
                                 96,
                                 96,
                                 PixelFormats.Default)

bitmap.Render(oViewport)

Using stream As FileStream = New FileStream("C:\\temp\\test_localviewport.png", FileMode.Create)
  Dim encoder As New PngBitmapEncoder
  encoder.Frames.Add(BitmapFrame.Create(bitmap))
  encoder.Save(stream)
End Using

The first picture (test_xamlviewport.png) is shown correctly, but the second picture where I create my own Viewport3D Object and print it is empty (test_localviewport.png).

How can I force to Render my UIElement3D Objects or why it doesn't work if i make a new Viewport vs print the existing XAML viewport instance?

Here you find a sample solution which reproduce my problem
http://cid-df67ca3f85229bd1.office.live.com/self.aspx/Development/WpfApplication2.zip

Regards
Roland

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文