2

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?

Example of thumbnail generated

Xion
  • 452
  • 2
  • 6
  • 25
  • 2
    This question has virtually nothing to do with c# and asp.net as very little with thumbnails as they are. It is all about the UI/UX of your website. – rs232 May 18 '18 at 13:12
  • 1
    If the source image is smaler then the thumbnail, you do not need the Thumbnail in the first place. Or you just use the Original image as your Thumbnail. The whole idea behind thumbnails is to save network load by showing a smaler preview image. It the image is alrady small enough, that does not mater. – Christopher May 18 '18 at 13:12
  • This thread might help you. [Create thumbnail image](https://stackoverflow.com/questions/2808887/create-thumbnail-image?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) – phanikurla May 18 '18 at 13:13
  • Possible duplicate of [Create thumbnail image](https://stackoverflow.com/questions/2808887/create-thumbnail-image) – VDWWD May 18 '18 at 13:19

1 Answers1

1

I would recommend to check if the Image is smaller then the size you want to have. If it is not treat it like you allready do. If it is smaller copy the image data into the center of a blank image that has the size you want to have and display this as thumbnail.

Andre P
  • 123
  • 1
  • 8