C#WPF更改平移或缩放的画布坐标 [英] C# WPF change canvas coordinates for pan or zoom

查看:625
本文介绍了C#WPF更改平移或缩放的画布坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个画布,我在其中存储了一组曲线



have a canvas on which I have a set of curves stored in

private List<System.Windows.Shapes.Path> paths = new List<System.Windows.Shapes.Path>();





里面可以有几条曲线,其中一些它们可以是具有非常多点数的PolyLineSegments。



绘制曲线后我可能需要平移/缩放画布。在互联网上我找到了这个非常出色的解决方案:





in it there can be several curves of which some of them can be PolyLineSegments with a very large number of points.

AFTER having drawn the curves I may have to pan/zoom the canvas. On the internet I've found this very brilliant solution:

public static class CanvasUtils
{
    public static Canvas SetCoordinateSystem(this Canvas canvas, Double xMin, Double xMax, Double yMin, Double yMax)
    {
        var width = xMax - xMin;
        var height = yMax - yMin;
        var translateX = -xMin;
        var translateY = height + yMin;
        var group = new TransformGroup();
        group.Children.Add(new TranslateTransform(translateX, -translateY));
        group.Children.Add(new ScaleTransform(canvas.ActualWidth / width, canvas.ActualHeight / -height));
        canvas.RenderTransform = group;
        return canvas;
    }
}







不幸的是它不适合我这扩大了使曲线看起来很可怕的一切(就像通过镜头看它一样),中风很粗。这就像一个真正的镜头变焦。



所以我想到的唯一解决方案就是重新获取所有点并将其中的每一个转换为新的坐标系。但那并不是非常聪明和耗时。还有其他更精彩的解决方案吗?



先谢谢Patrick




Unfortunately it doesn't do for me since this enlarges everything making the curve look horrible (just like looking at it through a lens), with a thick stroke. This just like a real lens zoom.

So the only solution which comes to my mind is to REPROCESS all points and transform EACH of them to the new coordinate system. But that's not very smart and time consuming. Any other more brilliant solution?

Thanks in advance Patrick

推荐答案

这就是WPF的工作原理。你的意思是拥有相同线条粗细的所有东西,无论缩放级别如何?

如果是这样,我会重新考虑这个要求:它看起来不太合理。 镜头变焦有什么问题?这是真正的变焦。



如果你真的希望变焦保持可见的绝对厚度,你仍然需要按照你的建议重新处理。我所能提供的建议是:通过以下方式简化此重新处理:对于 Canvas 子类的所有类型,创建派生类并仅使用它们。在那些派生类中,抽象出线厚度因子属性,该属性通过某种因素改变所有线的所有厚度(必须存储在实例字段中)。也就是说,如果您遍历所有 Canvas 子项并且仅更改此因子,则不会缩放任何内容,只有线条粗细将按比例更改。现在,将此过程与缩放相结合:如果您按Z中的因子缩放;将线厚度系数改为1 / Z.



-SA
This is how WPF works. Do you mean to have everything with the same line thickness, regardless of the zoom level?
If so, I would reconsider this requirement: it does not look very reasonable. What's really wrong with "lens zoom"? This is real zoom.

If you really want the zoom preserving visible absolute thickness, you still have to reprocess, as you suggested. All I can advice is: streamline this "reprocessing" in the following way: for all the types of the Canvas children, create derived classes and use only them. In those derived class, abstract out the "line thickness factor" property, which changes all thicknesses of all lines (which have to be stored in the instance fields) by some factor. That is, if you traverse all your Canvas children and change only this factors, nothing will zoom, only the line thicknesses will change proportionally. Now, combine this process with zoom: if you zoom by factor in Z; change "line thickness factor" by 1/Z.

—SA


这篇关于C#WPF更改平移或缩放的画布坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆