Last post Aug 06, 2008 04:39 PM by nathanaeljones
Jul 28, 2008 06:28 PM|Egdif|LINK
I've been googling like mad and can;t find a complete solution for creating thumbnail images on-the-fly. Basically I need to be able to resize user uploaded images maintaining aspect ratio where the uploaded image can be either portrait or landscape, and
the resulting thumbnail could be either orientation depending on where I'm using it. So portrait images need to be able to be resized into
So, I'd like ot be able to specify that any image, no matter what it's orientation must fit into an area with max widht and max height, whilst retaining aspect ratio.
Also, in an ideal world it would seamlessly work with transparent gifs and pngs although this is not quite as important.
I've managed to put some code together but can't figure out the formula for working out resulting height and width given the different orientation scenarios. If anyone can tell me that it would be hugely helpful
Jul 28, 2008 07:02 PM|DavidKiff|LINK
Bitmap bmp = new Bitmap("filename here");
System.Drawing.Image thumbnail = bmp.GetThumbnailImage(200, 200, null, new IntPtr());
thumbnail.Save("new filename here");
here is some code i found:
if (ImageSize > 0x200000)// 2MB-7D000) //If image size is greater than 50KB then image is too big
bool ImageIsGif = false;
bool ImageIsJpg = false;
float defaultWidthHeight = 110f;
float ImgWidthf = defaultWidthHeight;
float ImgHeightf = defaultWidthHeight;
int ImageWidth = (int)ImgWidthf;
int ImageHeight = (int)ImgHeightf;
string ImgOrentation = "L";
if (File.Exists(ImagePath + "/" + strImage)) //Check to see if the image has saved.
//Check to see if the image is a .gif, and store the thumb filename in strThumb
if (strImage.ToLower().IndexOf(".gif") > 0)
ImageIsGif = true;
strThumb = strImage.Replace(".gif", "_thumb.gif");
//Check to see if the image is a .jpg, and store the thumb filename in strThumb
if ((strImage.ToLower().IndexOf(".jpg") > 0) || (strImage.ToLower().IndexOf(".jpeg") > 0))
ImageIsJpg = true;
strThumb = strImage.Replace(".jpg", "_thumb.jpg");
//Open file as a steam
FileStream myThumbStream = new FileStream(ImagePath + "/" + strImage, FileMode.Open);
//Create a bitmap from the stream.
Bitmap myThumbBitmap = new Bitmap(myThumbStream);
//REDUCE IMAGE SIZE- WHILE KEEPING IMAGE CONSTRAINTS
//Check to see if the image's width is smaller than the height.
if (myThumbBitmap.Width > myThumbBitmap.Height)
ImgHeightf = ((float)myThumbBitmap.Height) / ((float)myThumbBitmap.Width);
ImgHeightf *= defaultWidthHeight;
ImgWidthf = ((float)myThumbBitmap.Width) / ((float)myThumbBitmap.Height);
ImgWidthf *= defaultWidthHeight;
ImgOrentation = "P";
ImageWidth = (int)ImgWidthf;
ImageHeight = (int)ImgHeightf;
System.Drawing.Image image1 = myThumbBitmap.GetThumbnailImage(ImageWidth, ImageHeight, null, new IntPtr());
image1.Save(ImagePath + "/" + strImage.Replace(".jpg", "_thumb.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
image1.Save(ImagePath + "/" + strImage.Replace(".gif", "_thumb.gif"), System.Drawing.Imaging.ImageFormat.Gif);
Aug 06, 2008 04:39 PM|nathanaeljones|LINK
Hey guys, just wanted to mention that you'll need to implement disk caching before you using image resizing in a production system.