Jens Langenbach
Jens Langenbach

Reputation: 107

Converting Tif to Jpg takes way too long

I'm trying to convert MANY (1000+) images from tiff to jpg, but after appr. 250-300 images it takes about 5-10 seconds for any further image, even though the first 250 took 20 seconds.

This is the code I use:

foreach (string filePath in Directory.GetFiles(tifPath, "*.tif", SearchOption.AllDirectories))
{
    System.Drawing.Image.FromFile(filePath).Save(jpgPath + "\\" + Path.GetFileNameWithoutExtension(filePath) + ".jpg", ImageFormat.Jpeg);
}

Is there something wrong with my approach? Thanks in advance.

Upvotes: 1

Views: 227

Answers (1)

SynerCoder
SynerCoder

Reputation: 12776

The image needs to be disposed or else it stays in memory:

foreach (string filePath in Directory.GetFiles(tifPath, "*.tif", SearchOption.AllDirectories))
{
    using (var image = System.Drawing.Image.FromFile(filePath))
    {
        image.Save(jpgPath + "\\" + Path.GetFileNameWithoutExtension(filePath) + ".jpg", ImageFormat.Jpeg);
    }
}

See this site for more information about using statements:

http://www.dotnetperls.com/using

Upvotes: 1

Related Questions