Resizing an Image to fit in a PictureBox

  • Thread starter Bruce Schechter
  • Start date
B

Bruce Schechter

I have a question about how to format the display of a JPEG image in a
PictureBox control. My current code is the fullowing....

Image image = Image.FromFile( strFileName );
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox.Image = image;

The images are loaded dynamically, and I do not have control of the size and
aspect ratio of the images. With the StretchImage mode selected, I get
some strange stretching (either vertically or horizontally, when the aspect
ratio of the image is noticably different from the aspect ratio of the
PictureBox.)

What mechanism can I use to effectively resize the image myself before
displaying it in the PictureBox? As yet I'm unable to find either a method
on the Image class or on the PictureBox class to resize the image before
display. I'd like to be able to adjust it to either fit snug vertically or
horizontally depending on respective aspect ratios.

Thanks,
and Cheers,
-- Bruce
 
B

Bob Powell [MVP]

Throw the picturebox out. Create your own control with an Image property and
then override the OnPaint handler to paint the image.

Then go read this article in the GDI+ FAQ.

http://www.bobpowell.net/bestfit.htm

--
Bob Powell [MVP]
C#, System.Drawing

The October edition of Well Formed is now available.
Find out how to use DirectX in a Windows Forms control
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com
 
B

Bruce Schechter

Bob,



Excellent! Amazing to see that an FAQ was already ready to roll.



I may be able to use the relatively "quick and dirty" solution mentioned in
the intro to your article, i.e., generating a re-sized image for my
exsisting PictureBox via Bitmap.GetThumbnailImage(). I coded up a prototype
this way, and it works great for most of my images (which, BTW, are all
quite a bit larger than the ClientSize area in which I am displaying them.
For some reason, a handful of the images look severely pixilated (while most
look fine.) I saw the comments in the documentation re GetThumbnailImage()
that says to beware of images with embedded thumbnails. My images are all
JPEGs. Would some of them somehow have embedded thumbnails? I don't know
how to control that, or even how to know if there are any embedded
thumbnails? Is something fundamentally broken here (see code below) that
will force me to use the more elegant solution from your article? ....Any
guidance on these points would be very much appreciated.



Thanks, and Cheers,
Bruce



Just in case, here is my code....


Image image = Image.FromFile( strFullName );
int iThumbX, iThumbY;
float fImageX = image.Width;
float fImageY = image.Height;
float fBoxX = pictureBoxMenuItem.ClientSize.Width;
float fBoxY = pictureBoxMenuItem.ClientSize.Height;
float fAspectRatioImage = fImageX/fImageY;
float fAspectRatioBox = fBoxX/fBoxY;
if ( fAspectRatioImage > fAspectRatioBox )
{
iThumbX = (int)fBoxX;
iThumbY = (int)(fBoxX/fAspectRatioImage);
}
else
{
iThumbY = (int)fBoxY;
iThumbX = (int)(fBoxY*fAspectRatioImage);
}
Image.GetThumbnailImageAbort myCallback = new
Image.GetThumbnailImageAbort( ThumbnailCallback );
Bitmap myBitmap = new Bitmap( strFullName );
pictureBoxMenuItem.SizeMode = PictureBoxSizeMode.CenterImage;
pictureBoxMenuItem.Image = image;
 
Y

Ying-Shen Yu[MSFT]

Hi Bruce,

GetThumbnailImage() returns the embedded thumbnail image, if one is
present. Frequently, the embedded thumbnail is of poor quality. Also as
document said,
GetThumbnailImage works well when the requested thumbnail image has a size
of about 120 x 120. If you request a large thumbnail image (say 300 x 300)
from an Image object that has an embedded thumbnail, there could be a
noticeable loss of quality in the thumbnail image. It might be better to
scale the main image (instead of scaling the embedded thumbnail) by calling
DrawImage.

You can determine whether a thumbnail is present by Image.GetPropertyItem
method.
Here is an list and description of the propId,
<Property Item Descriptions >
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/GDIP
lus/GDIPlusReference/Constants/ImagePropertyTagConstants/PropertyItemDescrip
tions.asp

The Property "PropertyTagThumbnailData" "PropertyTagThumbnailResolutionX
/PropertyTagThumbnailResolutionY" might help you determin if current image
has an embedded thumbnail image or not and the quality of the thumbnail
image. You can get more information using some other Properties in the link
above.

If you have any problems please be free to reply this thread to let me know.
Thanks!



Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 
B

Bruce Schechter

Ying-Shen,
You gave me exactly the information I needed to know.
Thanks,
Bruce

"Ying-Shen Yu[MSFT]" said:
Hi Bruce,

GetThumbnailImage() returns the embedded thumbnail image, if one is
present. Frequently, the embedded thumbnail is of poor quality. Also as
document said,
GetThumbnailImage works well when the requested thumbnail image has a size
of about 120 x 120. If you request a large thumbnail image (say 300 x 300)
from an Image object that has an embedded thumbnail, there could be a
noticeable loss of quality in the thumbnail image. It might be better to
scale the main image (instead of scaling the embedded thumbnail) by calling
DrawImage.

You can determine whether a thumbnail is present by Image.GetPropertyItem
method.
Here is an list and description of the propId,
<Property Item Descriptions >
http://msdn.microsoft.com/library/d...ImagePropertyTagConstants/PropertyItemDescrip
tions.asp

The Property "PropertyTagThumbnailData" "PropertyTagThumbnailResolutionX
/PropertyTagThumbnailResolutionY" might help you determin if current image
has an embedded thumbnail image or not and the quality of the thumbnail
image. You can get more information using some other Properties in the link
above.

If you have any problems please be free to reply this thread to let me know.
Thanks!



Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 

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