我有一个图像,我想在 WPF 中将其缩放/拉伸(stretch)到梯形上。有简单的方法吗?我想实现3D效果/透视/弯曲。基本上拍摄 2D 图像并在 3D 空间中弯曲它。
这就是我想要完成的:
original: new:
* * * *
* * * *
另一个注意事项,我需要这发生得非常快。我尝试使用此代码,但性能无法使用:http://www.vcskicks.com/image-distortion.php
任何帮助将不胜感激。提前非常感谢!
最佳答案
我认为快速做到这一点的最简单方法是使用 WPF 3D .只需设置一个带有透视投影的视口(viewport)并在其中放置一个四边形,其中包含正在转换的内容,GPU 将完成剩下的工作。
WPF 有一个很好的MatrixTransform类,它提供几乎任何东西的 GPU 加速转换(及其简化应用程序的衍生物,例如旋转转换)。可悲的是,梯形变换不可能用简单的矩阵变换来完成,这些在数学上仅限于缩放、剪切和旋转(以及它们的组合)。
编辑:您还可以查看一些示例 here , 3D 在 WPF 中真是微不足道。
编辑2:
这是显示在透视图中显示的通用按钮的代码。不要尝试与之交互,如果需要,请使用谷歌“WPF Interactive3D”。
如果您只想在 3D View 中显示图像,则不必使用 VisualBrush。
此外,如果您真的希望映射的内容适合视口(viewport)的边缘,则必须固定坐标,您可能需要进行大量实验或一些数学运算来计算坐标。如果你得到一些不错的结果,请发布结果(在你清理代码之后,我刚刚编辑了一个我在某处找到的示例)。
你现在正式欠我你的灵魂:D
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<DockPanel>
<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>
</DockPanel>
</Window>
关于wpf - 梯形上的 C#/WPF 图像转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5237824/