Tuesday, June 23, 2009

How To: Create Thumbnail Images in ASP.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;


        }

I think the following Code is better or I can say it’s a good sample for optimizing the above 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);


        }

No comments:

Post a Comment