0

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)

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150

1 Answers1

1

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;
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • Thanks! Is the zoom the same for Xs and Ys?? Perhaps I should use Zoom rather than Stretch? – KansaiRobot Aug 06 '18 at 08:27
  • 2
    @KansaiRobot, it depends if you zoom or stretch picture box, and if you maintain aspect ratio or not. here I have assumed that you maintain aspect ratio, however, if not, you can have two zoom factors, `int zoomx = picturebox.Width / picturebox.Image.Width;` and `int zoomy = picturebox.Height / picturebox.Image.Height;` and use multiply zoomx, to x, andzoomy to y. – Ashkan Mobayen Khiabani Aug 06 '18 at 08:30
  • 1
    @KansaiRobot, Yes to maintain ratio, you should use zoom, If you want to make sure that picturebox will be filled completely (no empty space) you can go reverse, for example if max allowed width is 800 and max allowed height is 600, you can do this: `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;` – Ashkan Mobayen Khiabani Aug 06 '18 at 08:45
  • It worked but the zoom had to be defined opposite. Since the Image is bigger than the picture box it had to be `int zoom picImage.Image.Width/ picImage.Width ;` – KansaiRobot Aug 06 '18 at 08:47
  • Tiny nitpick: If the pbox has a border use pbox.ClientSize.Width etc..! - Also: Smaller images may be centered or have emtpy stripes; See [here](https://stackoverflow.com/questions/39303881/how-to-draw-on-a-zoomed-image/39305038#39305038) and [here](https://stackoverflow.com/questions/40705823/free-form-crop-selection-doesnt-work-properly/40706594#40706594) for some code – TaW Aug 06 '18 at 08:47
  • @KansaiRobot please mark as answer and upvote if it helped. – Ashkan Mobayen Khiabani Aug 06 '18 at 08:55
  • I will :). Could you please edit to annotate that it worked for me with the zoom calculated with `pictureboxImage.Width/pictureboxWidth`? (I updated your answer already ) – KansaiRobot Aug 06 '18 at 09:03