How to generate thumbnail image in C#.NET

How to generate thumbnail image in C#.NET

A thumbnail image is a small version of an image.

You can create a thumbnail image by the following methods:

We will use: System.Drawing.Imaging

Copy the following code into your class:

       public void GenerateThumbnail(string thumbPath, int thumbWidth, int thumbHeight, string thumbNewPath)
        {
            String imageName = Path.GetFileName(thumbPath);
            int imageHeight = thumbHeight;
            int imageWidth = thumbWidth;

            Image fullSizeImg = Image.FromFile(thumbPath);
            Image.GetThumbnailImageAbort dummyCallBack = new Image.GetThumbnailImageAbort(ThumbnailCallback);
            Image thumbNailImage = fullSizeImg.GetThumbnailImage(imageWidth, imageHeight, dummyCallBack, IntPtr.Zero);
            thumbNailImage.Save(thumbNewPath, ImageFormat.Jpeg);
            thumbNailImage.Dispose();
            fullSizeImg.Dispose();
        }

        public bool ThumbnailCallback()
        {
            return false;
        }

Or you can use this optimized code:

       public void GenerateThumbnail (string thumbPath, int thumbWidth, int thumbHeight, string thumbNewPath)
        {
            Image image = new Bitmap(thumbPath);
            Image imageThumbnail = image.GetThumbnailImage(thumbWidth, thumbHeight, null, new IntPtr());
            imageThumbnail.Save(thumbNewPath);
        }

Share this post

Comment (1)

  • Benigno Reply

    Thanks for sharing, this is a fantastic blog. Really looking forward to read more. Keep writing.

    January 16, 2024 at 5:46 PM

Leave a Reply

Your email address will not be published. Required fields are marked *