首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Viewport3D外部的RenderTargetBitmap和ViewPort3D渲染边距

Viewport3D外部的RenderTargetBitmap和ViewPort3D渲染边距
EN

Stack Overflow用户
提问于 2013-11-20 08:20:32
回答 2查看 754关注 0票数 0

这是一个奇怪的问题,但我发现RenderTargetBitmap函数呈现了ViewPort3D左边的边距。当我保存位图(本例中为PNG)时,最终图像的左手边有一个间隙,而右手边被切掉了相同的量。ViewPort3D的尺寸为550px高,1024px宽。我在RenderTargetBitmap方法中使用了相同的维度。我很难理解为什么它会超出定义的界限。

代码语言:javascript
运行
复制
    <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中-对不起):

代码语言:javascript
运行
复制
    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

我认为这是它所在的容器(网格)可能是问题所在。

EN

回答 2

Stack Overflow用户

发布于 2013-11-20 09:52:32

我有一个答案-定义网格的行和列,并将列宽设置为与ViewPort3D相同(只有1列)。我还必须在窗口中左对齐网格(如图所示)。不幸的是,ViewPort是左对齐的-尽管它被设置为居中-参见代码-这没有任何意义,但我可以处理丑陋的屏幕。

代码语言:javascript
运行
复制
    <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="40" ></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1024" MaxWidth="1024" MinWidth="1024"></ColumnDefinition>           
    </Grid.ColumnDefinitions>
    <Button Content="Save Snapshot" Grid.Row="0" HorizontalAlignment="Left" Name="btnSaveSnapshot"  VerticalAlignment="Top" Width="100" Margin="8,8,0,0"/>
    <Viewport3D Name="vp3dTiles"  Grid.Row="1" 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>
票数 0
EN

Stack Overflow用户

发布于 2014-03-27 01:31:41

渲染时,视觉大小需要与渲染大小匹配,并且需要更新。我使用以下代码:

代码语言:javascript
运行
复制
Dim imageWidth As Double = 2448
Dim imageHeight As Double = 2050
myviewport.Width = imageWidth / 2
myviewport.Height = imageHeight / 2
myviewport.Measure(New Size(imageWidth / 2, imageHeight / 2))
myviewport.Arrange(New Rect(0, 0, imageWidth / 2, imageHeight / 2))
'Capture Image here
myviewport.Width = Double.NaN 'restore previous value here
myviewport.Width = Double.NaN 'restore previous value here
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20085003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档