In my project, I want to generate thumbnail from images that uploaded by user and show it in a gallery ( showing in a div box with size : 180 x 250). Below is code that i'm using to do it.
Size thumbnailSize = GetThumbnailSize(img);
System.Drawing.Image thumbnail = img.GetThumbnailImage(thumbnailSize.Width, thumbnailSize.Height, null, IntPtr.Zero);
.....
private Size GetThumbnailSize(System.Drawing.Image original)
{
const int maxPixels = 300;
int originalWidth = original.Width;
int originalHeight = original.Height;
double factor;
if (originalWidth > originalHeight)
{
factor = (double)maxPixels / originalWidth;
}
else
{
factor = (double)maxPixels / originalHeight;
}
return new Size((int)(originalWidth * factor), (int)(originalHeight * factor));
}
My problem is, the generated thumbnail is only suitable for image that large size, when user upload image that are smaller(eg:30 x 30), the thumbnail that generated will be zoom in and not clear shown like below. So what to do in order to generate thumbnail that are in ratio and can show properly in my gallery div box no matter the image is large or small?