get specific image location from PixtureBox inside Panel - c#

I have a PixtureBox inside Panel in c#, and I am trying to get the image location when i click on a certain image location. So I am using mouse location to get x and y, when the mouse is hovering over the image. This works fine if the image size is less than the size of the panel (no sliding bar). As soon as I get an image bigger than the panel size, I will get sliding bar. However, i get the same location if i moved the slide bar down. Is there a better way to get the specific image location with or without the sliding bar? any help is much appreciated :)

The Mouse_Hover event has a default EventArgs so it doesn't send the current mouse position when the event is invoked. The Mouse_Move event does provide the mouse location relative to the scroll position. If you store the location received in an Mouse_Move event from your PictureBox in local variable the Hover event can then use that position.
The position is absolute to the origin of the control so it doesn't matter if the picturebox is scrolled inside a panel.
Point last; // hold the last mousemove location
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
// store the location in last
last.X = e.Location.X;
last.Y = e.Location.Y;
}
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
// do whatever we want to do with the last location
Trace.WriteLine(last);
}

Related

Click interface Clarification (Entry-Level)

I'm trying to create a graph editor using WinForms.
I have a picture box, whenever I click on it the program draws a vertex by creating a label about 15px in size where I store a string, the location, etc.
I can draw the edges by drawing lines from location to location, but I need other boxes to do this, I wonder if there is a way to do this purely by touch (with the mouse cursor).
I need some kind of object that if clicked will start an event that will draw an edge up to the vertex I click next. I considered adding little picture boxes instead of labels, but the labels are convenient for storing the name of the vertexes, also I think adding both a label and an other box in the same position may hide one of the objects.
You can get the x and y coordinate's of the mouse on a user controls click event.
I would store the coordinate's of the last point you clicked on outside of the mouse click event and then draw a line from the last point to the new point.
lastPoint = null;
private void userControl_MouseClick(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left)
{
Point newPoint = e.Location;
if(lastPoint != null)
{
drawLine(lastPoint, newPoint);
}
lastPoint = newPoint;
}
}
Hope this helps.

c# wpf - Can I get the coordinates to a pixel clicked in an image control

I have an Image control running in my MainWindow.xaml, I was wondering if I can get the X and Y coordinates to the pixel clicked from the RoutedEventArgs on the MouseUp event for the control? I don't need the color or anything at that pixel, I would just like to know where that pixel was. Thank you for the help.
You can try handling the MouseLeftButtonUp event:
private void image1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e){
Point pos = e.GetPosition(image1);
}

how to detect a mouse click on drawn image?

I am new to c# Windows Form Application Development.
I created a form with a panel in which the user can draw image on it. How do I check if the image is clicked?
In designer mode, right-click on the panel, go to Properties.
In the Properties window, choose EVENTS (Lightning icon).
Double click on Click, then this code will be generated:
private void panel1_Click(object sender, EventArgs e)
{
//--what to do when user clicks on panel--
MessageBox.Show("Clicked");
}
Just double-click on the form's image-panel (or whatever object you wish to detect click events on) and Visual Studio will automatically generate a OnClick() event. Needless to say, I'm talking about the forms designer, not the actual form you will see when testing your code.
Alternatively, you can set which events you want to implement through the object's properties. That way you can also implement OnKeyDown() or OnFocus() or any other kind of events.
Edit: If the image doesn't cover the entire panel, you'll have to check if the mouse position is within the image's dimension. Assuming the image is drawn at position (imgOriginX, imgOriginY) and has the size (imgWidth, imgHeight):
// Fires, when user clicks on panel
private void panel_Click(object sender, EventArgs e)
{
// Cast to MouseEventArgs
MouseEventArgs mouse = (MouseEventArgs)e;
// If mouse is within image
if (mouse.X >= imgOriginX && mouse.Y >= imgOriginY && mouse.X < imgOriginX + imgWidth && mouse.Y < imgOriginY + imgHeight)
{
// do something here
}
}

draw and remove a line with gdi+

I owned complex drawing code with GDI+ which draws something like a chart on a user control.
If the user clicks with control pressed, a vertical marker line in dash style should be shown.
Now I look for a way to extend the drawing code without touching the complex drawing code.
I created a marker class which attaches to the mouse-up event of the user control.
In the eventhandler a check against (ModifierKeys == Keys.Control) is done.
If the user holds the control key and click with the left mouse button the draw method of the marker class is called with the Graphics object of the usercontrol as a parameter.
The current behaviour is that for each click a new line is drawn, but the line should be deleted and a new one should be drawn.
How can i erase the drawed line?
Do I have to redraw the comlete content of the user control?
You cannot delete a drawn line because there is no way to restore the underlying graphics.
What you could do is either:
Redraw the entire graph (without the line)
or
Stack a second transparent usercontrol on top and use that to display the line. Removing and drawing it when needed.
The answer here is clearly yes. With GDI+ you just draw directly on a bitmap buffer, so if you want to undo a previous drawing operation you can do one of those things (depending on the complexity of the issue and performance):
restore the bytes that have been changed on the bitmap buffer
reload a previous state of the drawing bitmap
A simple solution would be to have 2 bitmaps (something like that is usually called double buffering). One that is shown currently (and contains the final state) and one that is used for preview only. The preview one is always a copy of the first one - just with current modifications.
So basic algorithm for this simple implementation:
start with two bitmaps (blank but identical sizes) [named A and B]
if the user draws a line always make a copy of the bitmap A in B and draw on B - show B
if the user finishes the line then make a copy of B in A and again - show B
So always show the preview bitmap, which is just a modified bitmap of the original one.
Here is an example programming code in C# (assuming all events are connected and the preview Bitmap, B, is the picture box itself (which is just named pictureBox1 here):
Bitmap bmp;
bool isDrawing;
Point previous;
void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
isDrawing = true;
previous = e.Location;
}
void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isDrawing = false;
}
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isDrawing)
{
using (Graphics g = Graphics.FromImage(bmp))
{
double wf = (double)bmp.Width / (double)pictureBox1.Width;
double hf = (double)bmp.Height / (double)pictureBox1.Height;
g.ScaleTransform((float)wf, (float)hf);
g.DrawLine(Pens.Black, e.Location, previous);
}
pictureBox1.Refresh();
previous = e.Location;
}
}
This code will do everything to display drawing a straight line from a point to another by simply pressing the left mouse button.

C# WebBrowser get mouse click position

I have WebBrowser class and image loaded in it. After mouse click on the browser, I need to get mouse position - what is the best way to do it?
this is actually pretty simple if your just looking for the screen coordinates.
// this probably should be in your form initialization
this.MouseClick += new MouseEventHandler(MouseClickEvent);
void MouseClickEvent(object sender, MouseEventArgs e)
{
// do whatever you need with e.Location
}
if your strictly looking for the point in the browser, you need to consider the functions
browser.PointToClient();
browser.PointToScreen();

Categories

Resources