tags:

views:

250

answers:

2

i am using this code to take a jpg image and save it as a thumbnail but it seems very slow ..

        Image thumbNail = image.GetThumbnailImage(width, height, null, new IntPtr());

is there any faster way to generate a large amount of thumbnails from a directory of images?

+3  A: 

Try Draw Image - Re Edited

    public Image ResizeImage(Image openImage, int NewWidth, int NewHeight) {
        var openBitmap = new Bitmap(openImage)
        var newBitmap = new Bitmap(NewWidth, NewHeight)
        using (Graphics g = Graphics.FromImage(openBitmap))
        {
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.DrawImage(newBitmap, new Rectangle(0, 0, NewWidth, NewHeight));
        }
        openBitmap.Dispose(); //Clear The Old Large Bitmap From Memory

        return (Image)newBitmap;
    }

Typical 3-4mb Image Takes Between 4-8ms

Elijah Glover
what if i want it to be a jpg
ooo
as the original image is a jpg
ooo
this is inside a function that returns an "Image" is there anyway to have this code return an image object that is a jpg versus saving it ?
ooo
jpeg is a storage form, why do you need it as a jpg?
Lasse V. Karlsen
@mek above is a sample of a function that will resize an image. Saving can be done using save method on the bitmap
Elijah Glover
@mek -> newBitmap.Save(TargetStream, ImageFormat.Jpeg); will save the bitmap to the stream of your choosing as a Jpeg. Other formats are found in the ImageFormat enum
Wolfwyrd
since the current implementation: image.GetThumbnailImage() returns an Image object, i want to maintain the same interface so i can create some IThumbnailGenerator abstraction .. i need a way to return an image object.
ooo
@mek the bitmap class inherits from the image class
Elijah Glover
+1 for time and effort
J M
A: 

Try it:

public bool GenerateThumbNail(string fileName, string thumbNailFileName,
    ImageFormat format, int height, int width)
{
    try
    {
        using (Image img = Image.FromFile(fileName))
        {
            Image thumbNail = new Bitmap(width, height, img.PixelFormat);
            Graphics g = Graphics.FromImage(thumbNail);
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            Rectangle rect = new Rectangle(0, 0, width, height);
            g.DrawImage(img, rect);
            thumbNail.Save(thumbNailFileName, format);
        }
        return true;
    }
    catch (Exception)
    {
        return false;
    }
}

It uses DrawImage too.

eKek0