PictureBox

G

GoGs

How this vb6 code convert in c# dotnet2

-----
Image1.Visible = False
Image1.Picture = LoadPicture("c:\image008.jpg")
Dim x, y As Single
x = Image1.Width
y = Image1.Height
Do While x > Picture1.Width Or y > Picture1.Height
x = x / 1.01
y = y / 1.01
Loop
Picture1.PaintPicture Image1.Picture, 0, 0, x, y //problem with this
 
D

Dave Sexton

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");

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in Visual Studio 2005)

How this vb6 code convert in c# dotnet2

-----
Image1.Visible = False
Image1.Picture = LoadPicture("c:\image008.jpg")
Dim x, y As Single
x = Image1.Width
y = Image1.Height
Do While x > Picture1.Width Or y > Picture1.Height
x = x / 1.01
y = y / 1.01
Loop
Picture1.PaintPicture Image1.Picture, 0, 0, x, y //problem with this
 
G

GoGs

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
 
P

Peter Duniho

[...]
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 ratioof
this picture when form is maximised or something...

As far as I know, you have to calculate that yourself. It's not
hard...just calculate the aspect ratio for the container and the image,
and depending on which is greater, scale the image to fit horizontally or
vertically.

If anyone knows of something more automatic, I'd love to know. But this
is how I've done it in the past.

Pete
 
D

Dave Sexton

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)

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
 
G

GoGs

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top