Hi,
In that case, loading the bitmap is easy:
Bitmap image = new Bitmap(@"c:\image008.jpg");
Changing the size of the image requires you to copy the image:
image = new Bitmap(image, scaleWidth, scaleHeight);
To find the scaled width and height you need to scale both dimensions by
the
larger of the two factors that need to be scaled. In other words, if the
width is 10px bigger than it should be, and the height is only 5px
bigger,
you'll have to scale both the width and height by the scale at which the
image width is bigger than the PictureBox (picture.Width / image.Width)
to
fit the entire image in the PictureBox while keeping its aspect ratio.
// assuming that picture is an instance of a PictureBox control
int width = image.Width;
int height = image.Height;
int widthOffset = width - picture.Width;
int heightOffset = height - picture.Height;
float scaleFactor = 0;
if (widthOffset > 0 && widthOffset > heightOffset)
scaleFactor = picture.Width / (float) width;
else if (heightOffset > 0 && heightOffset > widthOffset)
scaleFactor = picture.Height / (float) height;
picture.Image = (scaleFactor > 0)
? new Bitmap(image,
(int) (width * scaleFactor),
(int) (height * scaleFactor))
: image;
Thanks...
Note that the calculation isn't all that precise, and I didn't account
for
an image that must be scaled up to fit the PictureBox, although that
shouldn't be difficult to do either. If you need that behavior,
consider it
to be good practice trying to figure out how to accomplish it on your
own