带有Viewport3D的WPF RenderTargetBitmap

时间:2014-08-08 08:55:35

标签: c# wpf xaml rendertargetbitmap

我正在使用XamlReader加载XAML UserControl,然后我尝试使用RenderTargetBitmap将其转换为图像。

使用简单的UserControl工作正常。

但是如果我使用这个Viewport3D,它就不起作用了。创建的图像没有3D部分......:

    <Viewport3D>
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <GeometryModel3D>
                    <GeometryModel3D.Geometry>
                        <MeshGeometry3D
                            Positions="-0.5  0.5  -0.5,  0.5  0.5  -0.5,
                                       -0.5  0  0.5,  0.5  0  0.5"
                            TriangleIndices=" 0  2  1,  1  2  3"
                            TextureCoordinates="0 0, 1 0, 0 1, 1 1" />
                    </GeometryModel3D.Geometry>

                    <GeometryModel3D.Material>
                        <DiffuseMaterial>
                            <DiffuseMaterial.Brush>
                                <VisualBrush>
                                    <VisualBrush.Visual>
                                        <Button>Hi</Button>
                                    </VisualBrush.Visual>
                                </VisualBrush>
                            </DiffuseMaterial.Brush>
                        </DiffuseMaterial>                              
                    </GeometryModel3D.Material>

                    <!-- Non-Affine Matrix Transform. -->
                    <GeometryModel3D.Transform>
                        <MatrixTransform3D>
                        </MatrixTransform3D>
                    </GeometryModel3D.Transform>

                </GeometryModel3D>
            </ModelVisual3D.Content>
        </ModelVisual3D>

        <!-- Light sources. -->
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <Model3DGroup>
                    <AmbientLight Color="#404040" />
                    <DirectionalLight Color="#C0C0C0" Direction="0 -2 -1" />
                </Model3DGroup>
            </ModelVisual3D.Content>
        </ModelVisual3D>

        <!-- Camera. -->
        <Viewport3D.Camera>
            <PerspectiveCamera Position="0 0.2 1"
                               LookDirection="0 0 -1.5"
                               UpDirection="0 1 0"
                               FieldOfView="100">
            </PerspectiveCamera>
        </Viewport3D.Camera>
    </Viewport3D>

将UserControl转换为图像的方法:

private static byte[] ConvertUserControlToPng(UserControl userControl)
    {
        userControl.Measure(new Size(userControl.Width, userControl.Height));
        userControl.Arrange(new Rect(new Size(userControl.Width, userControl.Height)));
        var renderTargetBitmap = new RenderTargetBitmap((int)(userControl.Width * 300 / 96),
                                                        (int)(userControl.Height * 300 / 96),
                                                        300, 300, PixelFormats.Pbgra32);
        renderTargetBitmap.Render(userControl);
        BitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Clear();
        encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));

        byte[] result;
        using (var stream = new MemoryStream())
        {
            encoder.Save(stream);
            result = stream.ToArray();
        }
        return result;
    }

我错过了什么吗?

2 个答案:

答案 0 :(得分:2)

我能够在png保存到文件中看到一些东西,我刚刚在调用Measure and Arrange之后添加了对UpdateLayout()的调用:

var viewport= (Viewport3D)XamlReader.Parse("... xaml ....");

viewport.Measure(new Size(viewport.Width, viewport.Height));
viewport.Arrange(new Rect(new Size(viewport.Width, viewport.Height)));
viewport.UpdateLayout();

var renderTargetBitmap = new RenderTargetBitmap((int)viewport.Width * 300 / 96,
                                                (int)viewport.Height * 300 / 96,
                                                300, 300, PixelFormats.Pbgra32);

renderTargetBitmap.Render(viewport);
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Clear();
encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
encoder.Save(new FileStream(@"D:\Test.png", FileMode.Create, FileAccess.Write));

答案 1 :(得分:0)

所以,我的猜测是它在某种程度上与渲染虚拟化了。也许不是 - 但我在上面的代码中注意到的最好的事情是它使用的是UserControl而不是Control(Viewport3D)。当你运行代码时,它是否完全通过它?或者是否会引发异常?