Created
August 16, 2021 16:37
-
-
Save bradmartin333/fa83eefbf1f1238a28f161e67cf3acc8 to your computer and use it in GitHub Desktop.
Get click pos on rotated image in PictureBox with zoom type view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Point ZoomMousePos(Point click) | |
{ | |
PictureBox pbx = Components.Vision; | |
float BackgroundImageAspect = pbx.BackgroundImage.Width / (float)pbx.BackgroundImage.Height; | |
float controlAspect = pbx.Width / (float)pbx.Height; | |
PointF pos = new PointF(click.X, click.Y); | |
if (BackgroundImageAspect > controlAspect) | |
{ | |
float ratioWidth = pbx.BackgroundImage.Width / (float)pbx.Width; | |
pos.X *= ratioWidth; | |
float scale = pbx.Width / (float)pbx.BackgroundImage.Width; | |
float displayHeight = scale * pbx.BackgroundImage.Height; | |
float diffHeight = pbx.Height - displayHeight; | |
diffHeight /= 2; | |
pos.Y -= diffHeight; | |
pos.Y /= scale; | |
} | |
else | |
{ | |
float ratioHeight = pbx.BackgroundImage.Height / (float)pbx.Height; | |
pos.Y *= ratioHeight; | |
float scale = pbx.Height / (float)pbx.BackgroundImage.Height; | |
float displayWidth = scale * pbx.BackgroundImage.Width; | |
float diffWidth = pbx.Width - displayWidth; | |
diffWidth /= 2; | |
pos.X -= diffWidth; | |
pos.X /= scale; | |
} | |
return RotatePoint(new PointF(pos.X, pos.Y), | |
new Point(pbx.BackgroundImage.Width / 2, pbx.BackgroundImage.Height / 2)); | |
} | |
private static Point RotatePoint(PointF click, Point pivot) | |
{ | |
try | |
{ | |
double angle = Math.PI * (double)Components.FormMain.numGridAngle.Value / 180.0; | |
double s = Math.Sin(-angle); | |
double c = Math.Cos(-angle); | |
PointF translate = new PointF(click.X - pivot.X, click.Y - pivot.Y); | |
double xrotate = translate.X * c - translate.Y * s; | |
double yrotate = translate.X * s + translate.Y * c; | |
return new Point((int)(xrotate + pivot.X), (int)(yrotate + pivot.Y)); | |
} | |
catch (Exception) | |
{ | |
return new Point((int)click.X, (int)click.Y); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment