C#/WPF 梯形图像转换

发布于 2024-10-21 01:50:20 字数 393 浏览 2 评论 0 原文

我有一个图像,我想在 WPF 中将其缩放/拉伸到梯形上。有简单的方法吗?我想实现3D效果/透视/弯曲。基本上拍摄 2D 图像并在 3D 空间中弯曲它。

这就是我想要完成的任务:

original:          new:

 *  *               *  *

 *  *             *      *

另一个注意事项,我需要这一切很快发生。我尝试使用此代码,但性能无法使用: http://www.vcskicks.com/image -畸变.php

任何帮助将不胜感激。预先非常感谢!

I have an image which I want to scale/stretch it over a trapezoid in WPF. Is there an easy way of doing it? I want to achieve 3D effect / perspective / bending. Basically taking a 2D image and bending it in 3D space.

This is what I want to accomplish:

original:          new:

 *  *               *  *

 *  *             *      *

Another note, I need this to happen very fast. I tried using this code, and the performance is unusable: http://www.vcskicks.com/image-distortion.php

Any help would be greatly appreciated. Thanks a lot in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隐诗 2024-10-28 01:50:20

我认为快速做到这一点的最简单方法是使用 WPF 3 D。只需设置一个带有透视投影的视口,然后将要转换内容的四边形放在那里,GPU 将完成剩下的工作。

WPF 有一个非常好的 MatrixTransform 类提供 GPU 加速转换几乎任何东西(及其衍生物,简化了例如旋转变换的应用)。遗憾的是,梯形变换不可能用简单的矩阵变换来完成,这些变换在数学上仅限于缩放、剪切和旋转(及其组合)。

编辑:您还可以在此处看到一些示例,3D 在WPF。

编辑2:

这里是显示透视图中显示的通用按钮的代码。不要尝试与它交互,如果你想要的话,谷歌“WPF Interactive3D”。

如果您只想在 3D 视图中显示图像,则不必使用 VisualBrush。

此外,如果您确实希望映射的内容适合视口的边缘,则必须固定坐标,您可能需要进行大量实验或一些数学来计算坐标。如果您得到一些不错的结果,请发布结果(清理代码后,我刚刚编辑了我在某处找到的示例)。

现在你正式欠我你的灵魂了: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>

I think the easiest way to do this fast is using the WPF 3D. Just set-up a viewport with perspective projection and put a quad with the content being transformed there, GPU will do the rest.

WPF has a very nice MatrixTransform class which offers GPU-accelerated transformations of pretty much anything (and its derivatives which simplify application of for example rotation transformation). Sadly, the trapezoid transformation is not possible to do with simple matrix transform, these are mathematically limited to scaling, shearing and rotations (and their combinations).

EDIT: You can also see some examples here, 3D is really trivial in WPF.

EDIT 2:

here is code which displays a common button displayed in perspective view. Don't try to interact with it, google "WPF Interactive3D" if you want that.

If you want only image to be displayed in the 3D view, you don't have to use the VisualBrush.

Also, the coordinates will have to be fixed if you really want the mapped content to fit with the edges of the viewport, you will likely have to do a lot of experimenting or some math to calculate the coordinates. lease post the result if you get something nice (after you cleanup the code, I just edited an example I found somewhere).

And you now officially owe me your soul :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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文