By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,734 Members | 832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,734 IT Pros & Developers. It's quick & easy.

PictureBox

P: n/a
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
---

Or some beter example??

--
GoGs
Feb 6 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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)

"GoGs" <ma***********@flash.com.hrwrote in message
news:op.tncl1isk2ir5um@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
---

Or some beter example??

--
GoGs
Feb 6 '07 #2

P: n/a
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
Feb 8 '07 #3

P: n/a
On Wed, 07 Feb 2007 16:55:27 -0800, GoGs <ma***********@flash.com.hr
wrote:
[...]
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
Feb 8 '07 #4

P: n/a
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
Feb 8 '07 #5

P: n/a
On Thu, 08 Feb 2007 05:33:08 +0100, Dave Sexton
<dave@jwa[remove.this]online.comwrote:
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 :)


--
GoGs
Feb 9 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.