Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top