Reputation: 283
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:
After Resize
Upvotes: 3
Views: 28383
Reputation: 1
Set pictureBox properties as follows: SizeMode to StrecthImage Anchor to Top,Bottom,Left,Right
Upvotes: 0
Reputation: 5140
In Winforms you can use the Picturebox properties to do this, without code:
Add a picture box and go the control properties
This gives you 5 choices. The effect of which are of the same image on a Winform below:
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:
I can then resize the form, in this case maximized and the PictureBox control takes care of itself with out additional code:
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:
Upvotes: 8
Reputation: 3247
I suggest you using 2 GroupBox
es 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
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
.
Upvotes: 4