【问题标题】:Image scaling quality on 3D surface problem in WPFWPF中3D表面问题的图像缩放质量
【发布时间】:2011-04-05 04:49:15
【问题描述】:

我有一个使用 WPF 3D 图形创建的图像查看器。那里的图像质量真的很差,所以我开始研究这个问题,创建了一个简单的应用程序,它在窗口的顶部使用 2D 图形显示图像,在底部使用 3D 图形显示相同的图像。我注意到图像在 3D 表面上看起来比在 2D 上要差得多。 3D 表面上的颜色饱和度较低,没有清晰的边界。请注意,我将 线性位图缩放 模式应用于根网格。其他奇怪的事情是,当我将位图缩放模式更改为“Fant”或“NearestNeighbor”时,它会影响 2D 图形,但 3D 表面上的图像保持不变!我为此示例使用图像,高度 = 466 像素,宽度 = 490 像素。我在代码(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;
            }
        }
    }

提前感谢您的帮助!

【问题讨论】:

  • 略有不同 - 但不要忘记,通过透视变换渲染 3D 图形比 2D 图像、几何图形和视觉效果涉及更多的变换。
  • @ChrisF,感谢您的评论。当我使用此代码显示高清照片时,差异更为显着。尤其是当我缩小图像时。

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


【解决方案1】:

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

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

尝试在您的应用程序和显卡设置中也启用抗锯齿功能。

【讨论】:

  • 嗨@djdanlib,感谢您的重播。如何更改视频卡的插值模式?我认为在所有客户端机器上配置显卡会很困难,是否有任何选项可以更改 WPF 的插值模式,而不是显卡?感谢您的帮助。
  • WPF中好像不能关闭抗锯齿(一直开启)discussion on the social msdn
  • 这将是在视频卡软件中完成的每台机器设置(每个制造商都不同),通常需要用户进行设置。不过,我从未见过它默认强制将其降至最低设置,只有当有人进入并手动调整它以在某处获得更多 FPS 时。如果您不调整 GFX 设置,它可能不会这样做。该 BitmapScalingMode 调用看起来就像您为 WPF 应用程序设置它的方式。嘿,值得一试!我找到了一个可能对您有所帮助的论坛帖子:forums.silverlight.net/forums/p/82277/223689.aspx
  • 谢谢,我会尝试应用自定义像素着色器,并通知您。
【解决方案2】:

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

【讨论】:

  • 嗨@Erno,实际上我正在使用环境光。 codemainViewPort3D.Children.Add(new ModelVisual3D { Content = new AmbientLight() });code.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-22
  • 2015-05-30
  • 1970-01-01
  • 2021-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多