【问题标题】:Centering a canvas on a given point在给定点上居中画布
【发布时间】:2011-08-24 05:53:02
【问题描述】:

我试图将一个矩形置于一个点的中心,但无法计算出所需的数学运算。

所使用的上下文是一种绘制位图并允许用户在指定点放大并在放大时平移/滚动的表单。

这是我的代码,目前用于将 CanvasBounds 居中于 ClientRectangle 的中间:

    private void UpdateCanvas()
    {
        int canvasWidth = (int)(_bitmap.Width * _zoomRatio);
        int canvasHeight = (int)(_bitmap.Height * _zoomRatio);

        Point canvasLocation = new Point((ClientRectangle.Width - canvasWidth) / 2, (ClientRectangle.Height - canvasHeight) / 2);

        CanvasBounds = new Rectangle(canvasLocation, new Size(canvasWidth, canvasHeight));
    }

_zoomRatio 是调整画布大小的缩放比例。 1.0 是 100%,2.0 是 200%,等等。

基本上,我想为这个函数提供一个鼠标输入点,并将该点用作 canvasBounds 矩形的中心。然后当用户操作水平和垂直滚动条时,它可以改变_centerPoint并更新CanvasBounds。

【问题讨论】:

    标签: c# .net canvas point


    【解决方案1】:

    我认为你只需要用画布大小来抵消你的“点”:

    private void UpdateCanvas(Point mousePoint)
    {
      int canvasWidth = (int)(_bitmap.Width * _zoomRatio);
      int canvasHeight = (int)(_bitmap.Height * _zoomRatio);
      int canvasX = (mousePoint.X - (canvasWidth / 2));
      int canvasY = (mousePoint.Y - (canvasHeight / 2));
      CanvasBounds = new Rectangle(canvasX, canvasY, canvasWidth, canvasHeight);
    }
    

    如果这不是您要查找的内容,可以通过简单的屏幕截图编辑您的帖子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-28
      • 1970-01-01
      • 2021-03-15
      • 2010-11-12
      • 2018-03-04
      • 1970-01-01
      • 2013-06-20
      • 1970-01-01
      相关资源
      最近更新 更多