Thursday, April 1, 2010

Thumbnail creation using C#

You can simply create a thumbnail using following code:-

 

private void CreateThumbNail(string fileName)

        {

            // create an image object for source file

            System.Drawing.Image image = System.Drawing.Image.FromFile(fileName);

            // get another image instance using GetThumbnailImage method  NOTE:- A CALLBACK DELEGATE IS NEED HERE

            System.Drawing.Image thumbNail = image.GetThumbnailImage(100, 100, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);

            // Save the thumbnail to another directory with same name

            fileName = fileName.Replace("\\gallery500\\", "\\thumbnail\\");

            // Create a file stream to be passed to thumbnail save method

            System.IO.FileStream fs = new FileStream(fileName, FileMode.Create);

            // Save the thumbnail

            thumbNail.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);

            // close file stream

            fs.Close();

        }

        public bool ThumbnailCallback()

        {

            return true;

        }

No comments: