我如何能捕捉鼠标坐标上的图片框?标上、鼠标、图片

2023-09-03 00:41:01 作者:怎么说你也是个淡魂

我是新用C#。我有一个PictureBox的形象,我想在图像上画出一个矩形捕捉到的X,Y坐标,我将利用(兑图片框图像)的矩形的宽度/高度。我知道我必须做的事情上pictureBox1_MouseEnter..etc。但我不知道从哪里开始。

I am new with c#. I have a image on picturebox, i would like to draw a rectangle on the image to capture X,Y coordinate and width/height of the rectangle that i will draw (against the image on picturebox). i know i must do something on pictureBox1_MouseEnter..etc. but i don't know where to start.

    private void pictureBox1_MouseEnter(object sender, EventArgs e)
    {

        Rectangle Rect = RectangleToScreen(this.ClientRectangle);
    }

我会AP preciated如果任何人可以给我的样品code。

I would appreciated if any one can give me sample code.

感谢。

推荐答案

您不想使用的MouseEnter 可言。你想用的MouseDown ,因为你不想开始跟踪用户吸引到他们的之后的矩形点击的鼠标按钮。

You don't want to use MouseEnter at all. You want to use MouseDown, because you don't want to start tracking the rectangle the user draws until after they click the mouse button.

因此​​,在MouseDown事件处理方法,保存光标的当前坐标。这是矩形,点(X,Y)的开始位置。

So in the MouseDown event handler method, save the current coordinates of the cursor. This is the starting position of the rectangle, point (X, Y).

private Point initialMousePos;

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    this.initialMousePos = e.Location;
}

和然后,在MouseUp事件时,当用户停止拖动并释放鼠标按钮被升高,要保存的鼠标光标的最终结束点,并结合,与初始起点,构建一个矩形。建立这样一个矩形的最简单方法是使用FromLTRB在 矩形类的静态方法< /一>:

And then, in the MouseUp event, which is raised when the user stops dragging and releases the mouse button, you want to save the final ending point of the mouse cursor, and combine that with the initial starting point to build a rectangle. The easiest way to build such a rectangle is using the FromLTRB static method of the Rectangle class:

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    // Save the final position of the mouse
    Point finalMousePos = e.Location;

    // Create the rectangle from the two points
    Rectangle drawnRect = Rectangle.FromLTRB(
                                             this.initialMousePos.X,
                                             this.initialMousePos.Y,
                                             finalMousePos.X,
                                             finalMousePos.Y);

    // Do whatever you want with the rectangle here
    // ...
}

您可能想用一些绘画code。在MouseMove事件处理方法,以给用户的视觉的指示,而他们正在绘制它,他们正在绘制矩形的。

You probably want to combine this with some painting code in the MouseMove event handler method to give the user a visual indication of the rectangle that they're drawing while they're drawing it.

在右键的方式做到这一点是处理的MouseMove事件得到鼠标的当前位置,然后调用的Invalidate 的方法,这将提高的Paint事件。您的所有涂装code应该在油漆事件处理程序。这是不是一个采取了很多样品,你在网上找到,在那里你会看到类似的createGraphics 所谓的的MouseMove 事件处理方法。避免这种情况,如果可能的话。

The right way to do this is to handle the MouseMove event to get the current position of the mouse, and then call the Invalidate method, which will raise the Paint event. All of your painting code should be in the Paint event handler. This is a better approach than the one taken by a lot of samples you find on the web, where you'll see something like CreateGraphics called inside of the MouseMove event handler method. Avoid that, if possible.

实现:

private Point currentMousePos;

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    // Save the current position of the mouse
    currentMousePos = e.Location;

    // Force the picture box to be repainted
    pictureBox1.Invalidate();
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    // Create a pen object that we'll use to draw
    // (change these parameters to make it any color and size you want)
    using (Pen p = new Pen(Color.Blue, 3.0F))
    {
        // Create a rectangle with the initial cursor location as the upper-left
        // point, and the current cursor location as the bottom-right point
        Rectangle currentRect = Rectangle.FromLTRB(
                                                   this.initialMousePos.X,
                                                   this.initialMousePos.Y,
                                                   currentMousePos.X,
                                                   currentMousePos.Y);

        // Draw the rectangle
        e.Graphics.DrawRectangle(p, currentRect);
    }
}

这几件事情要注意这个code,如果你从来没有做过任何类型之前绘制的。的第一件事是,我包裹创建一个 对象(我们用它来执行实际的绘图)在 使用语句。这是一个很好的一般做法您创建一个对象,它实现了 IDisposable的接口,如刷子和笔,以prevent在你的应用程序中的内存泄漏。

A couple of things to note about this code if you've never done any type of drawing before. The first thing is that I've wrapped the creation of a Pen object (which we use to do the actual drawing) in a using statement. This is good general practice any time you create an object that implements the IDisposable interface, such as brushes and pens, to prevent a memory leak in your application.

此外, PaintEventArgs的向我们提供的 图形类,它封装在一个.NET应用程序的所有基本绘图功能。所有你所要做的就是像调用方法的DrawRectangle 的DrawImage 该类的实例,通过在适当的对象作为参数和所有的图纸被自动为您完成。够简单了吧?

Also, the PaintEventArgs provide us with an instance of the Graphics class, which encapsulates all of the basic drawing functionality in a .NET application. All you have to do is call methods like DrawRectangle or DrawImage on that class instance, passing in the appropriate objects as parameters, and all the drawing gets done for you automatically. Simple enough, right?

最后,请注意,我们所做的正是在这里同样的事情,以创建一个矩形,因为我们最终做到了的MouseUp 事件处理方法。这是有道理的,因为我们只是想在矩形的瞬时大小在用户创建它。

And finally, notice that we've done exactly the same thing here to create a rectangle as we ultimately do in the MouseUp event handler method. That makes sense, because we just want the instantaneous size of the rectangle while the user is creating it.