I have a winform application that uses a picturebox. In this, the user can click on the picture box and depending on the position of the click do something. It works alright with small images. For reference the code for the clicking is like this
private void picImage_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
int i, n;
if (!flagDown) return;
for (i = 0; i < numRect; i++)
{
//Here we compare e with some data to see if the
//click is inside somewhere
if (e.X > pp.ar[i].minX && e.X < pp.ar[i].maxX &&
e.Y > pp.ar[i].minY && e.Y < pp.ar[i].maxY)
{
n = i;
DoSomeProcess(n);
break;
}
}
flagDown = false;
}
I am trying to modify this so as to work also with large images.
Now a picturebox has the SizeMode
property and this can be :
Normal, StretchImage, AutoSize, CenterImage,Zoom.
I have tried with Normal and AutoSize. These make the picture Box very large and can not show the large image. On the other hand the mouse checking function works well.
On the other hand the StretchImage and Zoom makes the large Image to appear as a small image so it shows entirely. However the values of the mouse obviously does not reflect the real position on the image (they were zoomed)
My question is how can I show a large image zoomed to small and also capture the mouse position with the automatically applied zoom value? How can I know this zoom value?
EDIT:
Thanks for Ashkan Mobayen Khiabani ' answer! I accepted the answer.
In my code I had to invert the order of calculation of zoom though as int zoom = picturebox.Image.Width/ picturebox.Width ;
otherwise it would havew been zero (since the Image is larger than the picture box)
Well you can calculate the zoom factor:
int zoom = picturebox.Width / picturebox.Image.Width;
and then:
if(e.X* zoom > pp.ar[i].minX && e.X * zoom < pp.ar[i].maxX
&& e.Y*zoom > pp.ar[i].minY && e.Y*zoom < pp.ar[i].maxY)
If you want to have the best result with zooming picturebox, you can first calculate the zoom and then resize your picturebox accordingly:
int zoom = 1;
if(picturebox.Image.Width>800|| picturebox.Image.Height>600)
zoom = Math.Min(800 /picturebox.Image.Width, 600 / picturebox.Image.Height); picturebox.Width = picturebox.Image.Width * zoom;
picturebox.Height= picturebox.Image.Height * zoom;