How To Draw clickable Polygons - winforms

I am developing a winforms project. What I need is, draw a clickable polygon on picturebox. Or draw a polygon and detect click if mouse is in polygon. I can draw a polygon but however I can't discover the click part.
Basically, take coordinates and add to array when I click on panel which has picturebox with image. And draw the polygon when clicked 7 times.
But I don't have a idea how to detect if mouse click is in polygon? Or we can say, how to convert these polygons to buttons?
private void panelPictureBoxBuilding_MouseUp(object sender, MouseEventArgs e)
{
if (startMoving)
{
Point coord = new Point(e.Location.X, e.Location.Y);
coordinates.Add(coord);
if (coordinates.Count == 7)
{
Graphics g = pbBuilding.CreateGraphics();
g.DrawPolygon(Pens.Black, coordinates.ToArray());
}
}
}
EDIT : Also how can i start drawing polygon since first mouse click? Current code is drawing polygon when clicked 7 times.
EDIT2 : I got it. Solution is in the below:
GraphicsPath path = new GraphicsPath(FillMode.Winding);
path.AddPolygon(coordinates.ToArray());
// Its mouse click event, after here
if(path.IsVisible(e.Location.X, e.Location.Y))
{
//Do something
}

Related

How To Get Mouse Click Coordinates Of An Image Inside A Picture Box(Not Picture Box Mouse Click Coordinates But Image Pixel Coordinates)

I have been trying to place a marker at the mouse location on an image when the user clicks on the image box, the problem is that the marker has a certain offset which is exponential, and I think it's because the image size is not equal to the image box size. I also want to place the marker after zooming in on the image.
This is a windows forms project and it uses emgucv, OpenCV, and c++/cli.
Note I place the marker using an OpenCV function by passing the mouse click coordinates.
Can anyone please help?.
I found the solution in this article https://www.codeproject.com/Articles/20923/Mouse-Position-over-Image-in-a-PictureBox you need to translate the PictureBox coordinates to image coordinates however, it depends on the size mode of the PictureBox.
I found a simple way to get mouse click coordinates with respect to a image coordinate when using ImageBox (add Emgu.CV.UI via Nuget). Add MouseClick event to your ImageBox and put that subroutine as below
private void imageBox1_MouseClick(object sender, MouseEventArgs e)
{
var ib = (ImageBox)sender;
var x_offset = ib.HorizontalScrollBar.Value;
var y_offset = ib.VerticalScrollBar.Value;
var x = e.Location.X;
var y = e.Location.Y;
var zoom = ib.ZoomScale;
int X = (int)((x / zoom) + x_offset);
int Y = (int)((y / zoom) + y_offset);
//MessageBox.Show(String.Format("{0}, {1}", X, Y));
}

Calculating angle (in degrees) from pixel values

For instance I click and drag a line directly to the left, that would be about 270 degrees, directly bottom would be 180 degrees dragging to the north east would be 45 and so forth. I want to display the exact value in degrees.
I am using 2D graphics in WPF (very new to this). I can see that it's pixels start at the top left corner of the screen (0,0) and increase x towards the right and y towards the bottom.
I have the code to calculate angles for two points in the x,y coordinate system, but I'm not sure how to achieve this in WPF.
If you guys can give me some ideas it would be very helpful.
Point start;
Point endPoint;
Canvas canvas; //or whatever control the user is clicking
private void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//The start of the user dragging
start = e.GetPosition(canvas)
}
private void canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
//The user is done dragging, calculate angle
endPoint = e.GetPosition(canvas)
//Do whatever you want with start and end
}

Dynamic drawing of opacity mask

I've got two background pictures, one is blurry, the other one is the same picture, but more colorful.
The default background image is the blurry one.
When I move the cursor, I'd like to change the background image from the blurry to the colorful one,
but only in a circle around the cursor,
and when I move the cursor forward, the changed background stays where the circle around the cursor went earlier.
(like when you scratch a lottery ticket with a coin)
I think I have to handle the MouseMove event, and use the MouseEventArgs cursor position,
but I cannot go through, and I really need help!
Thanks in advance!
You might want to try following this path:
Add a Canvas to your page, with the same size as both of your images
Create a clipping path in the shape of an ellipse (<Ellipse ...>) and position it outside of your image, in the canvas
Put the "blurry image" on your canvas first (below), and then the "sharp image", filling the whole canvas.
Let the ellipse be the clipping mask of your "sharp image" (using Image.Clip or YourUIElement.Clip (reference on MSDN)
Move your ellipse with the mouse cursor. The code might look like this (note: I didn't test the code):
-
imageCanvas.MouseMove += imageCanvas_MouseMove;
private void imageCanvas_MouseMove(object sender, MouseEventArgs e)
{
Point mousePosition = e.GetPosition();
Canvas.SetTop(myEllipse, mousePosition.Y - myEllipse.ActualHeight / 2);
Canvas.SetLeft(myEllipse, mousePosition.X - myEllipse.ActualWidth / 2);
}
If this works, you can increment your visual design adding animations on MouseEnter/MouseLeave, etc.

How do I get the co-ordinates of a mouse click on a transformed WPF control?

I'm playing with a simple WPF application. One part of it includes a grid containing several controls. The grid is rotated using a LayoutTransform and a RotateTransform. I need to get the co-ordinates of a mouse-click relative to the top-left of the grid, taking the rotation into account.
To be clear, let's say I have a single drawing surface within the grid and no transform had been applied. I then click at location X = 20, Y = 10 and put a dot on the drawing surface at that point. If the grid is now rotated by 30 degrees and I click on the dot (which is also moved by the rotation), the click position should still be X = 20, Y = 10.
MouseEventArgs has a GetPosition method that takes a UIElement. It returns the position of the mouse event relative to the specified element. So if you want to transform a mouse click into coordinates of a grid, pass that grid to the GetPosition method.

How to draw a rectangle in WinForm app in the correct location

I have a WinForm app that has an image displayed in a PictureBox that has the added functionality of allowing a user to draw a rectangle on the image by clicking and dragging.
The Location, Height and Width of the rectangle are saved to disk.
When the image is viewed again I would like to automatically redraw that rectangle in the same position on the image.
When I redraw it, however, the Height and Width are fine but the location is always off.
The location is being captured in the MouseDown Event like so
private void pbSample_MouseDown(object Sender, MouseEventArgs e)
{
if (SelectMode)
{
StartLocation.X = e.X;
StartLocation.Y = e.Y;
//later on these are saved as the location of the rectangle
}
}
And I am redrawing it like so
public void DrawSelectedArea(Rectangle rect)
{
Graphics g = this.pbSample.CreateGraphics();
Pen p = new Pen(Brushes.Black);
g.DrawRectangle(p, rect);
}
Given the location from the MouseEventArgs captured during the MouseDown Event how can I calculate the correct location to redraw my rectangle?
The mouse-click points you're capturing are probably relative to the Form rather than the picture box. You either need to make sure you're capturing the coordinates properly or offset them.
Could you include a screenshot as an example?

Resources