首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >RenderTargetBitmap和Viewport3D -质量问题

RenderTargetBitmap和Viewport3D -质量问题
EN

Stack Overflow用户
提问于 2010-02-02 02:24:44
回答 5查看 9K关注 0票数 16

我想将3D场景从Viewport3D导出到位图。

最明显的方法是使用RenderTargetBitmap --然而,当我这样做时,导出的位图的质量明显低于屏幕上的图像。环顾互联网,RenderTargetBitmap似乎并没有利用硬件渲染的优势。这意味着渲染是在Tier 0上完成的。这意味着没有mip映射等,因此降低了导出图像的质量。

有人知道如何以屏幕质量导出Viewport3D的位图吗?

澄清

虽然下面给出的例子没有说明这一点,但我最终需要将Viewport3D的位图导出到一个文件中。据我所知,唯一的方法是将图像导出到从BitmapSource派生的文件中。下面的图表显示,使用RenderTargetBitmap提高导出质量可以改善图像,但由于渲染仍然是在软件中完成的,因此速度非常慢。

有没有一种方法可以使用硬件渲染将渲染的3D场景导出到文件中?这真的可以吗?

您可以看到这个xaml的问题:

代码语言:javascript
复制
<Window x:Class="RenderTargetBitmapProblem.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="400" Width="500">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Viewport3D Name="viewport3D">
            <Viewport3D.Camera>
                <PerspectiveCamera Position="0,0,3"/>
            </Viewport3D.Camera>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <AmbientLight Color="White"/>
                </ModelVisual3D.Content>
            </ModelVisual3D>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <GeometryModel3D>
                        <GeometryModel3D.Geometry>
                            <MeshGeometry3D Positions="-1,-10,0  1,-10,0  -1,20,0  1,20,0"
                                            TextureCoordinates="0,1 0,0 1,1 1,0"
                                            TriangleIndices="0,1,2 1,3,2"/>
                        </GeometryModel3D.Geometry>
                        <GeometryModel3D.Material>
                            <DiffuseMaterial>
                                <DiffuseMaterial.Brush>
                                    <ImageBrush ImageSource="http://www.wyrmcorp.com/galleries/illusions/Hermann%20Grid.png"
                                                TileMode="Tile" Viewport="0,0,0.25,0.25"/>
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </GeometryModel3D.Material>
                    </GeometryModel3D>
                </ModelVisual3D.Content>
                <ModelVisual3D.Transform>
                    <RotateTransform3D>
                        <RotateTransform3D.Rotation>
                            <AxisAngleRotation3D Axis="1,0,0" Angle="-82"/>
                        </RotateTransform3D.Rotation>
                    </RotateTransform3D>
                </ModelVisual3D.Transform>
            </ModelVisual3D>
        </Viewport3D>
        <Image Name="rtbImage" Visibility="Collapsed"/>
        <Button Grid.Row="1" Click="Button_Click">RenderTargetBitmap!</Button>
    </Grid>
</Window>

这段代码是:

代码语言:javascript
复制
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        RenderTargetBitmap bmp = new RenderTargetBitmap((int)viewport3D.ActualWidth, 
            (int)viewport3D.ActualHeight, 96, 96, PixelFormats.Default);
        bmp.Render(viewport3D);
        rtbImage.Source = bmp;
        viewport3D.Visibility = Visibility.Collapsed;
        rtbImage.Visibility = Visibility.Visible;
    }
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-02-17 06:30:57

RenderTargetBitmap上没有设置告诉它使用硬件渲染,因此您将不得不退回到使用Win32或DirectX。我推荐使用this article中给出的DirectX技术。本文中的以下代码,并展示了如何完成此操作(这是C++代码):

代码语言:javascript
复制
extern IDirect3DDevice9* g_pd3dDevice;
Void CaptureScreen()
{
    IDirect3DSurface9* pSurface;
    g_pd3dDevice->CreateOffscreenPlainSurface(ScreenWidth, ScreenHeight,
        D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &pSurface, NULL);
    g_pd3dDevice->GetFrontBufferData(0, pSurface);
    D3DXSaveSurfaceToFile("Desktop.bmp",D3DXIFF_BMP,pSurface,NULL,NULL);
    pSurface->Release(); 
}

您可以创建与渲染WPF内容的位置相对应的Direct3D设备,如下所示:

  1. 在屏幕图像中的某一点调用Visual.PointToScreen
  2. MonitorFromPoint中调用User32.dll以获取Direct3DCreate9 in d3d9.dll以获得pD3D
  3. 调用pD3D->GetAdapterCount()将adapters
  4. Iterating从0计数到count-1,并调用pD3D->GetAdapterMonitor()并与先前检索的hMonitor进行比较以确定适配器D22以创建设备本身

我可能会在一个用C++/CLR编写的单独的库中完成大部分工作,因为我对这种方法很熟悉,但您可能会发现使用SlimDX.将其转换为纯C#和托管代码很容易,我还没有尝试过这种方法。

票数 5
EN

Stack Overflow用户

发布于 2010-02-16 06:09:35

我不知道mip映射是什么(也不知道软件渲染器是否做了这个和/或多级抗锯齿),但我确实记得Charles Petzold不久前的一个post,它完全是关于打印高分辨率的wpf3D视频的。

我用你的示例代码试过了,它看起来工作得很好。所以,我假设你只需要在上稍微扩展一下。

您需要在rtbImage上将Stretch设置为None,并修改Click事件处理程序,如下所示:

代码语言:javascript
复制
private void Button_Click(object sender, RoutedEventArgs e)
{
    // Scale dimensions from 96 dpi to 600 dpi.
    double scale = 600 / 96;

    RenderTargetBitmap bmp =
        new RenderTargetBitmap
        (
            (int)(scale * (viewport3D.ActualWidth + 1)),
            (int)(scale * (viewport3D.ActualHeight + 1)),
            scale * 96,
            scale * 96,
            PixelFormats.Default
        );
    bmp.Render(viewport3D);

    rtbImage.Source = bmp;

    viewport3D.Visibility = Visibility.Collapsed;
    rtbImage.Visibility = Visibility.Visible;
}

希望这能解决你的问题!

票数 4
EN

Stack Overflow用户

发布于 2010-02-16 23:31:55

我认为你得到一个空白屏幕有几个原因。首先,VisualBrush需要指向一个可视的可视对象。其次,也许您忘记了RectangleGeometry需要有尺寸(我知道我一开始是有尺寸的)。

我确实看到了一些我不太明白的奇怪的事情。也就是说,我不明白为什么我必须在VisualBrush上将AlignmentY设置为底部。

除此之外,我认为它是有效的..。我认为你应该能够很容易地根据你的实际情况修改代码。

以下是按钮单击事件处理程序:

代码语言:javascript
复制
private void Button_Click(object sender, RoutedEventArgs e)
{
    GeometryDrawing geometryDrawing = new GeometryDrawing();
    geometryDrawing.Geometry =
        new RectangleGeometry
        (
            new Rect(0, 0, viewport3D.ActualWidth, viewport3D.ActualHeight)
        );
    geometryDrawing.Brush =
        new VisualBrush(viewport3D)
        {
            Stretch = Stretch.None,
            AlignmentY = AlignmentY.Bottom
        };
    DrawingImage drawingImage = new DrawingImage(geometryDrawing);
    image.Source = drawingImage;
}

下面是Window1.xaml:

代码语言:javascript
复制
<Window
    x:Class="RenderTargetBitmapProblem.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    SizeToContent="WidthAndHeight"
>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="400"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="400"/>
        </Grid.ColumnDefinitions>

        <Viewport3D
            x:Name="viewport3D"
            Grid.Row="0"
            Grid.Column="0"
        >
            <Viewport3D.Camera>
                <PerspectiveCamera Position="0,0,3"/>
            </Viewport3D.Camera>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <AmbientLight Color="White"/>
                </ModelVisual3D.Content>
            </ModelVisual3D>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <GeometryModel3D>
                        <GeometryModel3D.Geometry>
                            <MeshGeometry3D
                                Positions="-1,-10,0  1,-10,0  -1,20,0  1,20,0"
                                TextureCoordinates="0,1 0,0 1,1 1,0"
                                TriangleIndices="0,1,2 1,3,2"
                            />
                        </GeometryModel3D.Geometry>
                        <GeometryModel3D.Material>
                            <DiffuseMaterial>
                                <DiffuseMaterial.Brush>
                                    <ImageBrush
                                        ImageSource="http://www.wyrmcorp.com/galleries/illusions/Hermann%20Grid.png"
                                        TileMode="Tile"
                                        Viewport="0,0,0.25,0.25"
                                    />
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </GeometryModel3D.Material>
                    </GeometryModel3D>
                </ModelVisual3D.Content>
                <ModelVisual3D.Transform>
                    <RotateTransform3D>
                        <RotateTransform3D.Rotation>
                            <AxisAngleRotation3D Axis="1,0,0" Angle="-82"/>
                        </RotateTransform3D.Rotation>
                    </RotateTransform3D>
                </ModelVisual3D.Transform>
            </ModelVisual3D>
        </Viewport3D>

        <Image
            x:Name="image"
            Grid.Row="0"
            Grid.Column="0"
        />

        <Button Grid.Row="1" Click="Button_Click">Render!</Button>
    </Grid>
</Window>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2179042

复制
相关文章

相似问题

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