I want to scale a System.Drawing.Bitmap
to at least less than some fixed width and height. This is to generate thumbnails for an image gallery on a website, so I want to keep the aspect ratio the same.
I have some across quite a few solutions but none seem to really do what I need; they revolve around scaling based on keeping the width or the height the same but not changing both.
An example:
If I have a 4272 by 2848 image and I want to scale it to a size of 1024 by 768, then the resulting image should be 1024 by 683 and padded (with a black border) to 1024 by 768.
How can I do this with images larger than the required size and smaller than the require sized and also pad images which don't come out to the exact size I need once scaled?
Just to add to yamen's answer which is prefect for images but not so much for text. If you are trying to use this to scale text.. like say a word document(which is in this case in bytes from word interop) you will need to make a few modifications or you will get giant bars on the side. May not be perfect but works for me!
The bitmap constructor has resizing built in.
http://msdn.microsoft.com/en-us/library/0wh0045z.aspx
If you want control over interpolation modes see this post.
Target parameters:
Your original file:
Target sizing (scale factor):
The resize including brushing canvas first:
And don't forget to do a
bmp.Save(filename)
to save the resulting file.