Get number of pixels between two points in an Image - c#

I am working on a project which requires a number of pixels between two points which are horizontal in an image. I am doing this in a windows application form.
Basically when a user clicks on one point in image and then on other point then he should get horizontal distance in form on number of pixels.
I am not getting any idea to do it.
please help.

For an image container, I am just using a pictureBox, though this would work with a label or whatever control you want.
Outside of any function:
private Boolean clicked_once = false;
private Point point1;
private Point point2;
And then I added a MouseClick event (not a Click event) :
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (clicked_once == false)
{
clicked_once = true;
point1 = e.Location;
}
else if (clicked_once == true)
{
clicked_once = false;
point2 = e.Location;
int distance = Math.Abs(point1.X - point2.X);
MessageBox.Show("Distance of pixels horizontally: " + distance.ToString());
}
}
Should work.

Assuming your image is displayed using any control descended from Control then you will have access to the Control.MouseClick event (link).
This event uses MouseEventArgs (link), which has a property X.
Should be pretty clear from that point.
EDIT: Added this very simple example:
private int? x1;
private void MyImageControl_MouseClick(object sender, MouseEventArgs e)
{
if (x1.HasValue)
{
MessageBox.Show("Difference of " + Math.Abs(e.X - x1.Value).ToString());
x1 = null;
}
else
{
x1 = e.X;
}
}

Related

C# draggable map resets to default after releasing and moving mouse

I have been trying to fix an error for the last week.
I got a panel and inside this panel there is a picturebox (a map).
Whenever you press the mouse button and you move it to somewhere it moves the map, but whenever you release it and press it again and move it, it goes back to the default position of the map (but still dragable).
I need it to be that whenever someone release the button press, and click and move it again it proceed from the position it is currently.
I am sure it has something to with my MouseMove event and I have tried a lot of things and couldn't manage to fix it.
Here are my codes for the MouseUp, MouseDown and MouseMove events.
private bool Dragging;
private Point lastLocation;
private void countryMapImage_MouseUp(object sender, MouseEventArgs e)
{
Dragging = false;
lastLocation = e.Location;
}
private void countryMapImage_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Dragging = true;
lastLocation = e.Location;
}
}
private void countryMapImage_MouseMove(object sender, MouseEventArgs e)
{
if (Dragging == true)
{
int dx = e.X - lastLocation.X;
int dy = e.Y - lastLocation.Y;
countryMapImage.Padding = new Padding(Padding.Left + dx, Padding.Top + dy, Padding.Right - dx, Padding.Bottom - dy);
countryMapImage.Invalidate();
}
}
Would appreciate the help!
I have tried changing some values on the mousemove padding event, mousedown and mouseup events and nothing solved it. I have already tried to look for some answers but couldn't find any that solves the problem.
Try this: When user clicks the Map, capture the location of the cursor (in "screen" coordinates) and also the current location of the Map control. Now, as you move the mouse (and only if LButton is down) calculate a delta of the current cursor position (in "screen" coordinates) relative to the cursor position captured on the MouseDown event. Then, set the new position of the Map control to be the sum of its location that you captured on the MouseDown event plus the delta.
One key to having this work properly is to take the e.Location of the handler events and convert the point to screen coordinates relative to the sender.
public MainForm()
{
InitializeComponent();
countryMapImage.MouseMove += onMapMouseMove;
countryMapImage.MouseDown += onMapMouseDown;
}
Point
// Where's the cursor in relation to screen when mouse button is pressed?
_mouseDownScreen = new Point(),
// Where's the 'map' control when mouse button is pressed?
_controlDownPoint = new Point(),
// How much has the mouse moved from it's original mouse-down location?
_mouseDelta = new Point();
private void onMapMouseDown(object sender, MouseEventArgs e)
{
if (sender is Control control)
{
_mouseDownScreen = control.PointToScreen(e.Location);
Text = $"{_mouseDownScreen}";
_controlDownPoint = countryMapImage.Location;
}
}
private void onMapMouseMove(object sender, MouseEventArgs e)
{
if (MouseButtons.Equals(MouseButtons.Left))
{
if (sender is Control control)
{
var screen = control.PointToScreen(e.Location);
_mouseDelta = new Point(screen.X - _mouseDownScreen.X, screen.Y - _mouseDownScreen.Y);
Text = $"{_mouseDownScreen} {screen} {_mouseDelta}";
var newControlLocation = new Point(_controlDownPoint.X + _mouseDelta.X, _controlDownPoint.Y + _mouseDelta.Y);
if(!countryMapImage.Location.Equals(newControlLocation))
{
countryMapImage.Location = newControlLocation;
}
}
}
}
The MouseUp event shouldn't need to be handled in this simple scenario.

Reach data from other event

We can get X and Y points which by mouse move on the picturebox like;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
double Xcoordinate = e.X;
double Ycoordinate = e.Y;
label1.Text = Xcoordinate.ToString();
label2.Text = Ycoordinate.ToString();
}
My question How can I get Xcoordinate and Ycoordinate from other events for ex; MouseClick event or my new defined function?
Actually I want to reach XCoordinate and Ycoordinate parameter from FormLoad. How can I do that?
Use Cursor Position property..
private void MoveCursor()
{
// Set the Current cursor, move the cursor's Position,
// and set its clipping rectangle to the form.
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
Cursor.Clip = new Rectangle(this.Location, this.Size);
}
The MouseMove event happens to give you the mouse position. This is not included in other EventArgs. You can always get the mouse positon through Cursor.Position.
The static method Control.MousePosition will get you the absolute position of the mouse pointer on the screen. You can translate it using Control.PointToClient to get the coordinates local to a control of interest.
If I remember correctly, the one caveat is that MouseEventArgs gives you the mouse position as it was when the message was posted to the event loop, while Control.MousePosition gives you the position right now. For most applications, this difference is probably not a big deal.
You can Use this Solution to Get co-ordinate of picture Box at the time of other event
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
textBox1.Text = e.X.ToString();
textBox2.Text = e.Y.ToString();
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
textBox1.Text = e.X.ToString();
textBox2.Text = e.Y.ToString();
}
}
Or try It also
pictureBox1.MouseClick += (s, e) => MessageBox.Show(String.Format("Mouse Clicked at X: {0} Y: {1}", e.X, e.Y));

Open a popup from just below of current selected cell of datagrid view

I have a datagridview with some data. I have added a custom button which will show when i click on a cell. By clicking this button a popup form will be opened. My requirement is only set the location of popup form that is should show just below the selected cell of the datagridview. below is the screen shot:
This doesn't work 100% but it's a starting point, based off my comment:
Main Form:
public Form1()
{
InitializeComponent();
}
private Point _cellClick;
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
var h = dataGridView1.Rows[0].Height;
if (MousePosition.Y % h == 0)
{
_cellClick = new Point(MousePosition.X, MousePosition.Y);
}
else
{
var y = MousePosition.Y;
do
{
y++;
} while (y % h != 0);
_cellClick = new Point(MousePosition.X, y);
}
}
private void button1_Click(object sender, EventArgs e)
{
var f = new Form2(_cellClick);
f.ShowDialog(this);
}
Child Form:
private Point loc;
public Form2(Point location)
{
InitializeComponent();
loc = location;
}
private void Form2_Load(object sender, EventArgs e)
{
this.SetDesktopLocation(loc.X, loc.Y);
}
Edit This is really close to what you're looking for, the only issue is that the child form doesn't show up "adjacent" to the cell, but exactly where the mouse was when they clicked on the cell.
You could probably do some basic arithmetic to figure out the height of a cell to offset MousePosition.Y so that the child form shows up adjacent to it. I think you just need to round the coordinate to the nearest multiple of N, where N is dataGridView1.Rows[0].Height, rounding up.
Edit 2 I just edited the code to try something like this, and now the child form tends to show up just a little bit below the row.

How to recognize if a rectangle contains a Text block within in

I'm creating this program in which the user drags/drops a text block into a rectangle, and then store the contents of that specific textblock when he/she drops it into the rectangle.
I figured out how to do the drag/drop, but I just can't figure out how to check if the rectangle contains the textblock.
Here's the code so far:
bool captured = false;
double x_shape, x_canvas, y_shape, y_canvas;
UIElement source = null;
private void shape_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
source = (UIElement)sender;
Mouse.Capture(source);
captured = true;
x_shape = Canvas.GetLeft(source);
x_canvas = e.GetPosition(LayoutRoot).X;
y_shape = Canvas.GetTop(source);
y_canvas = e.GetPosition(LayoutRoot).Y;
}
private void shape_MouseMove(object sender, MouseEventArgs e)
{
if (captured)
{
double x = e.GetPosition(LayoutRoot).X;
double y = e.GetPosition(LayoutRoot).Y;
x_shape += x - x_canvas;
Canvas.SetLeft(source, x_shape);
x_canvas = x;
y_shape += y - y_canvas;
Canvas.SetTop(source, y_shape);
y_canvas = y;
}
}
private void shape_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Mouse.Capture(null);
captured = false;
}
private void rectangle1_MouseEnter(object sender, MouseEventArgs e)
{
if (Mouse.Capture(null))
{
textBox1.Text = "test";
}
}
The "Shape" events apply to the textblock by the way, just for clarification.
I tried to find a shortcut and make it so with the rectangle1_MouseEnter event that if the mouse isn't clicked, then store the values (didn't include the code for the storing values). However, the problem is, this idea doesn't work because textBox1.Text="test" isn't registered, and I don't see why it isn't.

C# Resizing a panel from right to left

I spent a few hours searching the internet for ways to take my panel and grab it from the left side and pull it left. I found many sources and I tried to alter it to my needs, but it always goes from left to right. My code I currently have is:
bool allowResize = false;
private void PanelResize_MouseUp(object sender, MouseEventArgs e)
{
allowResize = false;
}
private void PanelResize_MouseMove(object sender, MouseEventArgs e)
{
if (allowResize)
{
FavoritesPanel.Width = PanelResize.Left + e.X;
}
}
private void PanelResize_MouseDown(object sender, MouseEventArgs e)
{
allowResize = true;
}
"PanelResize" is a picturebox pushed to the left side of the panel. "FavoritesPanel" is the panel. Both are anchored to the top, bottom, and right side.
My overall question is, how can I correct my code to drag my panel from right to left?
problem is that default setting of changing Width of some panel means that it makes panel more width in way that right side is shifting to right direction.
You need two things. This what you have (resizing width) and second is that you need to shift whole panel to left side as long as you make it more width.
I have similar code here. I made code which allows you to roll up upper side of panel. So I had to do two things again: make my panel more height and shift whole panel more up. Here is my code:
public partial class Form1 : Form
{
private int actualCursorY;
private int lastCursorY;
private bool isDragged;
public Form1()
{
InitializeComponent();
}
private void barRadPanel_MouseDown(object sender, MouseEventArgs e)
{
lastCursorY = PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y)).Y;
isDragged = true;
}
private void barRadPanel_MouseUp(object sender, MouseEventArgs e)
{
isDragged = false;
}
private void barRadPanel_MouseMove(object sender, MouseEventArgs e)
{
if (isDragged)
{
actualCursorY = PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y)).Y;
mainRadPanel.Location = new Point(mainRadPanel.Location.X, actualCursorY);
if (lastCursorY != actualCursorY)
{
mainRadPanel.Height -= actualCursorY - lastCursorY;
lastCursorY = actualCursorY;
}
}
}
}
I tried to use
e.Y
instead of
PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y)).Y
but it made some errors.
This is what it does:

Categories