WPF中3D表面问题的图像缩放质量

时间:2011-04-02 18:09:41

标签: c# .net wpf 3d image-manipulation

我有一个用WPF 3D图形创建的图像查看器。图像质量真的很糟糕,所以我开始研究这个问题,创建了一个简单的应用程序,它使用窗口顶部的2D图形显示图像,底部使用3D图形显示相同的图像。我注意到3D图像上的图像看起来比2D上差。 3D表面上的颜色饱和度较低,边界不清晰。请注意,我将线性位图缩放模式应用于根网格。其他奇怪的是,当我将位图缩放模式更改为'Fant'或'NearestNeighbor'时,它会影响2D图形,但3D表面上的图像仍然相同!我正在使用此样本的图像,高度= 466px,宽度= 490px。我在代码(2D和3D实现)中稍微缩小它以查看缩放质量下降。代码是:

<Window x:Class="Scaling3DSample.Window2"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="340">
        <Grid x:Name="backgroundGrid">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
        </Grid>
</Window>

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Shapes;
    namespace Scaling3DSample
    {
        public partial class Window2 : Window
        {
            private static double _distanceFromCamera = 0.62618;

            public Window2()
            {
                InitializeComponent();
                RenderOptions.SetBitmapScalingMode(backgroundGrid, BitmapScalingMode.Linear);
                Create2DGraphics();
                // THE SAME IMAGE ON 3D SURFACE LOOKS MUCH WORSE
                Create3DGraphics();
            }

            private void Create2DGraphics()
            {
                Rectangle exampleRectangle = new Rectangle();
                Grid.SetRow(exampleRectangle, 0);

                exampleRectangle.Width = 335;
                exampleRectangle.Height = 317;
                exampleRectangle.Fill = GetBrush();
                backgroundGrid.Children.Add(exampleRectangle);
            }

            private void Create3DGraphics()
            {
                Viewport3D mainViewPort3D = new Viewport3D();
                Grid.SetRow(mainViewPort3D, 1);

                mainViewPort3D.Camera = new PerspectiveCamera { LookDirection = new Vector3D(-1, 0, 0), UpDirection = new Vector3D(0, 0, 1), FieldOfView = 77.0942 };
                mainViewPort3D.Children.Add(new ModelVisual3D { Content = new AmbientLight() });

                MeshGeometry3D geometry3D = new MeshGeometry3D();

                Point3D topLeft = new Point3D(-_distanceFromCamera, 0.5, -0.5);
                Point3D bottomRight = new Point3D(-_distanceFromCamera, -0.5, 0.5);

                geometry3D.Positions.Add(bottomRight);
                geometry3D.Positions.Add(new Point3D(-_distanceFromCamera, topLeft.Y, bottomRight.Z));
                geometry3D.Positions.Add(new Point3D(-_distanceFromCamera, bottomRight.Y, topLeft.Z));
                geometry3D.Positions.Add(topLeft);

                geometry3D.TriangleIndices.Add(1);
                geometry3D.TriangleIndices.Add(0);
                geometry3D.TriangleIndices.Add(2);

                geometry3D.TriangleIndices.Add(2);
                geometry3D.TriangleIndices.Add(3);
                geometry3D.TriangleIndices.Add(1);

                geometry3D.TextureCoordinates.Add(new Point(0, 0));
                geometry3D.TextureCoordinates.Add(new Point(1, 0));
                geometry3D.TextureCoordinates.Add(new Point(0, 1));
                geometry3D.TextureCoordinates.Add(new Point(1, 1));

                Material material = new DiffuseMaterial(GetBrush());

                ModelVisual3D modelForGeometry = new ModelVisual3D { Content = new GeometryModel3D(geometry3D, material) };
                mainViewPort3D.Children.Add(modelForGeometry);
                backgroundGrid.Children.Add(mainViewPort3D);
            }

            private ImageBrush GetBrush()
            {
            // put any other image URI here, image Height = 466px, Width = 490px
                ImageBrush brush = new ImageBrush(new BitmapImage(new Uri("lion.jpg", UriKind.Relative)));
                brush.Stretch = Stretch.Fill;
                return brush;
            }
        }
    }

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

猜测:你没有定义任何灯光或任何法线。有时这会导致图像比您预期的更暗。

答案 1 :(得分:1)

还有一些其他变量需要考虑。

尽管WPF要求更好看的东西,但您的显卡设置可能会强制插值模式下降。 WPF的3D在第2层硬件上进行硬件加速,因此请检查驱动程序的控制软件。 WPF可能无法更好地请求任何内容!

尝试在应用程序和图形卡设置中启用消除锯齿功能。