【发布时间】: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