thumbnail conundrum

  • Thread starter Thread starter Chris Perry
  • Start date Start date
C

Chris Perry

How do you create a thumbnail constrained to a box, without distorting and
but making where it doesn't fill the box transparent.

I'm two thirds there :

Size fitImageSize = this.GetScaledImageDimensions(
myBitmap.Width, myBitmap.Height, intThumbWidth,
intThumbHeight);

Bitmap imgOutput = new Bitmap(myBitmap, fitImageSize.Width,
fitImageSize.Height);

imgOutput.Save(Server.MapPath(sSavePath + sThumbFile),
System.Drawing.Imaging.ImageFormat.Jpeg);

But its the third aspect that is tricky,

Cheers all.
 
You should calculate the resizing Ratio... It is not possible to fit it in a
box exactly unless all your images have the width=height
otherwise you find out what the ratio is for the new width to the old width,
then you use that ratio to calculate the new height.

This will ensure that your image is proportional. I have some code that does
that, if you want I'll post it here


Hope that helps
 
Hi Abdellah,

Thanks for your reply. I do have a function that calculates the best fit for
the thumbnail,. thats my 'GetScaledImageDimensions'

However I want the total thumbnail size to be the size of the 'box' but
contain the scaled image inside it and have the parts where it's too small
be white or tranparent.

Its so I dont break the designers precious layout ;)


----------------------------
| |<----- transparent
| |
|-------------------------- |
| |
| thumb |
|-------------------------- |
| |
|-------------------------- |

Thanks
 
You should be able to create a new bitmap which is the exact size of the box
your are trying to fill. Make that bitmap one solid color, white for
example, and then copy your thumbnail into the center of the new bitmap.
Then save the new bitmap as your thumbnail. You'd have to calculate where to
place your thumbnail based on which of it's dimensions are too small.
 
Hi Matt,

Thats a great idea but I can't seem to find any suitable methods for copying
into the second bitmap.

Can anyone help ?

Thanks,

Chris.
 
Chris,

I believe you need the DrawImage function of the Graphics object. You can
create a Graphics object using Graphics.FromImage (using your new image) and
draw your thumbnail onto it. Check out the Graphics object in MSDN.
I did something similar myself a while back, but I don't remember exactly
what I did and I don't have the code handy right now.

Matt
 
Back
Top