3

I want everytime , the user resizes the Form , the Image in the pictureBox , also resizes with the same Values ( proportionally ) ,

I searched on the internet for some codes and found this answer in StackOverFlow https://stackoverflow.com/a/6501997/3264464

static public Bitmap ScaleImage(Image image, int maxWidth, int maxHeight)
{
    var ratioX = (double)maxWidth / image.Width;
    var ratioY = (double)maxHeight / image.Height;
    var ratio = Math.Min(ratioX, ratioY);
    var newWidth = (int)(image.Width * ratio);
    var newHeight = (int)(image.Height * ratio);
    var newImage = new Bitmap(newWidth, newHeight);
    Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
    Bitmap bmp = new Bitmap(newImage);
    return bmp;
}

I added the function for my code , and am not sure about the MaxHeight,MaxWidth thing , i mean why do i need to send it via parameters

and In the Form1_Resize event handler i wrote:

private void Form1_Resize(object sender, EventArgs e)
{
    Bitmap NewImg = ScaleImage(pictureBox1.Image, 1000, 1000);
    pictureBox1.Image = NewImg;
}

but It won't work .. Nothing Happens when I resize the form

UPDATE: Tried everything with same results

Look at the pictures below , The black point is the Left of the PictureBox and it must not move , what you suggested is good , but i want , The left of the pictures stays on the same point at the beggining

Before Resize:

Before

After Resize

enter image description here

Community
  • 1
  • 1
GabourX
  • 283
  • 2
  • 5
  • 14

2 Answers2

8

In Winforms you can use the Picturebox properties to do this, without code:

Add a picture box and go the control properties

enter image description here

This gives you 5 choices. The effect of which are of the same image on a Winform below:

enter image description here

  • Normal just shows the image and fits (I believe from pixel 0,0) at no scaling or moving.
  • StretchImage will scale and force a fit of the image, even if it means skewing the image
  • AutoSize in this case shows the full image, in this case the image is bigger than the form which is why there is just a huge blue chunk.
  • Center image leaves the control intact and shows the center region of the image
  • Zoom zooms in or out of the image so that it shows in its entirety in the picturebox control.

Use this in combination with anchor or dock of the control to keep the control where you want it, and also to scale the image.

It sounds like you might want Zoom.

Zoom in action, with Anchor points set to all sides (And a group box added to simulate a control box:

enter image description here

I can then resize the form, in this case maximized and the PictureBox control takes care of itself with out additional code:

enter image description here

Note, because I am using a very rectangular image I have set a gray background to show where the control is versus the image. You could set control color background to make this less obvious, or use stretchImage instead of zoom to force the image to always fill the control, although this creates plenty of ugly artifacts on non-square images.

example of stretched artifacts:

enter image description here

Austin T French
  • 5,022
  • 1
  • 22
  • 40
  • I guess my question then, @GabourX is what exactly are you trying to so with the code? Always see the full image? What is it you want to code to do that the Winform control can't do for you? Remove the code (comment out) and see if it does what you need with just the properties. – Austin T French Jun 01 '14 at 17:16
  • What i need is explained in the Updated answer in below , but did you get exactly what i want ? – GabourX Jun 01 '14 at 17:19
  • @GabourX I can by setting the sizeMode to "Zoom" for example, and then setting anchor to top,right,left, bottom. Or SizeMode to Stretch to get rid of the bars/ box. – Austin T French Jun 01 '14 at 17:44
  • @GabourX I gave you some more examples of how it works. – Austin T French Jun 01 '14 at 17:52
4

I suggest you using 2 GroupBoxes controls, add the buttons in the left one and the image in the right one and use the Dock property of them. Here is an example which suits your needs:


Before resizing

Before resizing

After resizing After resizing

The Dock property of the groupBox1 is Left and the same property of the other groupBox2 is Fill. Also, the Dock property of the image (which is inside the second groupBox) is set to Fill.

George Chondrompilas
  • 3,167
  • 27
  • 35
  • Works 100% , but one little problem , now the MouseEvents doesn't occur. because the GroupBox is covering the image maybe ? – GabourX Jun 01 '14 at 18:06
  • Ah! I removed the picture and re-add it again , and forgot to add the handlers one more time , my bad :) – GabourX Jun 01 '14 at 18:13