c# multi - point selection on an image - c#-to-vb.net

I'm designing interface with visual c# for my image processing project. I need point selection on an image and classification these points for using in my image processing code at vhdl.
Original image will stay at a picturebox and I will use selected image in another picturebox.
How I can select points on an image?

If you need to get pixel color and coord when mouse is down over picturebox, you could use
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
PictureBox pb = (PictureBox)sender;
Bitmap bmp = (Bitmap)pb.Image;
Color col = bmp.GetPixel(e.X, e.Y);
// Do what you please with pixel coord and color
}

Related

How To Draw clickable Polygons

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
}

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));
}

Is it possible to change an ImageBrush color inside WPF?

I'm trying to do something quite simple, but I'm not sure if it's possible. I have this image:
Inside my board game project, I use this to indicate the mouse position in the board and stuff like that (being the board a 8x8 grid with a rectangle inside). I'm also setting the grid cell background brush as, say, a SolidColorBrush with Blue color and .25 opacity, while the rectangle that lies above it with the target, or vice-versa, because I couldn't achieve my desired effect, which is, the part that is black to turn full blue, as my brush, while the rest remains with the opacity and etc. The way I did, it was possible to make the black part become blueish, with a transluscent background, but the color in the border isn't quite right yet:
I don't know if I was able to make myself clear, but I guess the picture shows what is my goal.
The code:
ImageBrush targetBrush = new ImageBrush();
targetBrush.ImageSource = new BitmapImage(new Uri("Resources/GameScreen/selectedTile.png", UriKind.Relative));
targetBrush.Opacity = 1;
SolidColorBrush backBrush = new SolidColorBrush(Colors.Blue);
backBrush.Opacity = .25;
tile.Children.Add(new Rectangle());
foreach (object target in tile.Children)
{
if (target.GetType() == typeof(Rectangle))
{
((Rectangle)target).Fill = backBrush;
}
}
tile.Background = targetBrush;
Thanks in advance.
As the image is just a mask you could use a Rectangle with a blue Fill and an OpacityMask set to your image.

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 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