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

Leave a Reply

Your email address will not be published.