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;
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 :)
--
Dave Sexton
http://davesexton.com/blog http://www.codeplex.com/DocProject (Sandcastle in Visual Studio 2005)
"GoGs" <ma***********@flash.com.hrwrote in message
news:op.tneniqaa2ir5um@gogs...
On Tue, 06 Feb 2007 23:55:39 +0100, Dave Sexton
<dave@jwa[remove.this]online.comwrote:
Hi,
What are you trying to do?
If you're trying to fit an image into a PictureBox Control on a Form then
set the SizeMode property to the appropriate value:
// assuming picture is an instance of the PictureBox control on a Form
picture.SizeMode = PictureBoxSizeMode.StretchImage;
picture.Image = new System.Drawing.Bitmap(@"c:\image008.jpg");
Yes, I know this...
but problem is in picture aspect ratio...
I haw a big picture ("c:\test.jpg") 1234x1234
in form picturebox where dock = fill and how i can keep aspect ratio of
this picture when form is maximised or something...
sorry on beeeed language
>
--
GoGs