奇怪的问题,但我发现 RenderTargetBitmap 函数正在渲染 ViewPort3D 左侧的边距。当我保存位图(在这种情况下为 PNG)时,最终图像的左侧有一个间隙,而右侧被切掉了相同的数量。ViewPort3D 的尺寸为 550 像素高 x 1024 像素宽。我在 RenderTargetBitmap 方法中使用相同的尺寸。我很困惑为什么它超出了定义的界限。
<Grid>
<Button Content="Save Snapshot" HorizontalAlignment="Left" Name="btnSaveSnapshot" VerticalAlignment="Top" Width="100" Margin="8,8,0,0"/>
<Viewport3D Name="vp3dTiles" Height="550" Width="1024" UseLayoutRounding="True" HorizontalAlignment="Center" VerticalAlignment="Bottom" >
<Viewport3D.Camera>
<PerspectiveCamera Position="384,1750,174" LookDirection="0,-.65,-0.25" UpDirection="0,0,1" FieldOfView="65"/>
</Viewport3D.Camera>
<ModelVisual3D>
<ModelVisual3D.Content>
<Model3DGroup>
<AmbientLight Color="white" />
<GeometryModel3D>
<GeometryModel3D.Geometry>
<MeshGeometry3D Positions="0,0,0 768,0,0 768,1408,0 0,1408,0 " TriangleIndices="0 1 3 1 2 3" TextureCoordinates="0,0 760,0 760,1408 0,1408 "/>
</GeometryModel3D.Geometry>
<GeometryModel3D.Material>
<DiffuseMaterial>
<DiffuseMaterial.Brush>
<ImageBrush ViewportUnits="Absolute" TileMode="Tile" ImageSource="FlorVintage_FINAL.png" Viewport="0,0,768,1408" ViewboxUnits="Absolute" Stretch="None" AlignmentY="Top" RenderOptions.BitmapScalingMode="NearestNeighbor" AlignmentX="Left" />
</DiffuseMaterial.Brush>
</DiffuseMaterial>
</GeometryModel3D.Material>
</GeometryModel3D>
</Model3DGroup>
</ModelVisual3D.Content>
</ModelVisual3D>
</Viewport3D>
</Grid>
后面的代码(在 VB 中 - 抱歉):
Private Sub btnSaveSnapshot_Click(sender As Object, e As RoutedEventArgs) Handles btnSaveSnapshot.Click
Dim bmp As New RenderTargetBitmap(1024, 550, 96, 96, PixelFormats.Pbgra32)
bmp.Render(Me.vp3dTiles)
Dim png As New PngBitmapEncoder()
png.Frames.Add(BitmapFrame.Create(bmp))
Using stm As Stream = File.Create(My.Settings.FileSavePath & "\" & "TestSnapshot.png")
png.Save(stm)
End Using
End Sub
我认为它所在的容器(网格)可能是问题所在。