簡體   English   中英

如何從圖片框獲取圖像坐標(點),而不考慮平移和縮放

[英]How to get the image coordinates(points) from a picturebox irrespective of panning and zooming

我成功地從Picturebox的Mousedown位置獲得了圖像坐標,無論是Picturebox的各種尺寸模式。 但是在平移操作或縮放操作之后難以找到中心點。 目前,我僅在Picturebox的Normal Size模式下工作才能達到以下目的。

我的主要目標:

1.畫一個以Mousedown位置為中心的圓

2.接下來找到在Picturebox(不是在圖像上)上繪制的圓的半徑(以像素為單位),即使在平移和縮放之后,該半徑也應始終相同。 即使縮放或平移后,如果我畫一個圓(或橢圓),它也應該在圖像上給我半徑。我知道點到像素的轉換。


我已經使用Mousedown,上移和移動事件實現了平移,並使用軌跡欄實現了Zoom。 我現在需要以下部分的幫助::條件1:我打開圖像>>將其平移一定距離>>啟用圓形工具>>以圖像邊界內的中心繪制它,然后繪制該圓圈並存在條件2:我打開圖像>>畫一個圓,平移它,圓的平移量應與平移圖像時的平移量相同。 這是我的MouseDown,MouseUp,MouseMove和Paint方法:

請告訴我您是否需要任何函數體或其他內容。因為它只是我正在處理的一個大雜亂的應用程序

告訴我您是否需要任何其他符合我上述主要目標的設計方法。

在TerryBozzio的答案之后進行編輯::

        Bitmap _bmp;
        private bool _isMouseDown = false;
        private Point _mouseDownLocation = Point.Empty;
        int _xDifference, _yDifference;
        public Form1()
        {
            InitializeComponent();
            _bmp = RenderImageOfSpecifiedSize(@"C:\Users\Public\Pictures\Sample Pictures\2 duck.bmp");
            _pictureBox.Image = _bmp;
        }

        private Bitmap RenderImageOfSpecifiedSize(string fileLocation)
        {
            //return new Bitmap(new Bitmap(fileLocation),new Size(300,300)); 
            return new Bitmap(fileLocation);
        }

        private void _pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            _isMouseDown = true;
            _mouseDownLocation = new Point(e.X, e.Y);
        }
        private void _pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isMouseDown)
            {
                _xDifference = _mouseDownLocation.X > e.X ? _mouseDownLocation.X - e.X : e.X - _mouseDownLocation.X;
                _yDifference = _mouseDownLocation.Y > e.Y ? _mouseDownLocation.Y - e.Y : e.Y - _mouseDownLocation.Y;
                _pictureBox.Invalidate();
            }
        }
        private void _pictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            _isMouseDown = false;
        }
        private void _pictureBox_Paint(object sender, PaintEventArgs e)
        {
             _txtCenter.Text= _xDifference.ToString() + " - " + _yDifference.ToString();
             e.Graphics.DrawEllipse(Pens.Red, _mouseDownLocation.X - _xDifference, _mouseDownLocation.Y - _yDifference, _xDifference * 2, _yDifference * 2);
        }

如果我說對了,這將繪制一個以中心為中心的橢圓作為mousedown捕獲的點,同時在鼠標移動事件中拖動鼠標以擴大或不擴展橢圓,它將以在mousedown位置捕獲的點為中心。將在圖片框內的代碼中(在mousemove事件內),我放置了兩個變量Xdiff和Ydiff以完成工作,並且還以標題形式顯示它們的值(始終隨着移動而更新),它們的值是x和y的半徑,因為請注意您可能會繪制一個橢圓形橢圓:

    bool ismouseDown = false;
    Point p;
    int Xdiff, Ydiff;
    Bitmap bmp;

    public Form1()
    {
        InitializeComponent();
        bmp = (Bitmap)Image.FromFile(@"C:\..\YourImage.jpg");
        pictureBox1.Image = bmp;
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        this.Text = Xdiff.ToString() + " - " + Ydiff.ToString();

        e.Graphics.DrawEllipse(Pens.Black, p.X - Xdiff, p.Y - Ydiff, Xdiff * 2, Ydiff * 2);
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        ismouseDown = true;
        p = e.Location;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (ismouseDown)
        {
            Xdiff = p.X > e.X ? p.X - e.X : e.X - p.X;
            Ydiff = p.Y > e.Y ? p.Y - e.Y : e.Y - p.Y;
            pictureBox1.Invalidate();
        }
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        ismouseDown = false;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM