System.Drawing.Image.FromFile OutOfMemory Exception For Image Converter

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi everyone, I am new here, hopefully someone can help.

I am just making a small program for myself with C# Express and I am getting some odd Exceptions with the System.Drawing.Image.FromFile() Method.

This program is meant to watch a folder for new files, the convert tiff files to jpeg files (My scanner only scans into tiff).

This program always works for the first file added to c:temp, but fails and throws an OutOfMemory exception the second or third time.

Any info would be greatly appreciated.

Thanks



Here is the source

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;


namespace ConsoleApplication1
{
    class Program
    {
              public static System.Drawing.Image image1;
     
        public static void Main()
        {


            Console.WriteLine("  BBC Image Conversion Program");
            Console.WriteLine("To Quit, Type q then press enter");
            Console.WriteLine("--------------------------------");
            string ext = "tif";
            string folder = "c:\temp";

            // Create a new FileSystemWatcher and set its properties.
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = folder;
            /* Watch for changes in LastAccess and LastWrite times, and
               the renaming of files or directories. */
            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
               | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            // Only watch text files.
            watcher.Filter = ext;

            // Add event handlers.
            watcher.Created += new FileSystemEventHandler(OnChanged);

            // Begin watching.
            watcher.EnableRaisingEvents = true;

            // Wait for the user to quit the program.
            while (Console.Read() != q) ;
        }

        // Define the event handlers.
        private static void OnChanged(object source, FileSystemEventArgs ep)
        {
            // Specify what is done when a file is changed, created, or deleted.
   
                        ChangeFile(ep.FullPath);
                        GC.Collect();

        }
        private static void ChangeFile(string FileName)
        {
            // Specify what is done when a file is changed, created, or deleted.

                      try
                {
                    image1 = System.Drawing.Image.FromFile(FileName, true);
                    Console.WriteLine("Found File " + FileName);
                    image1.Save(@FileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                    Console.WriteLine("Converted to JPG for you");
                }
                catch (OutOfMemoryException e)
                {

                    Console.WriteLine(e.Message);
                }
                finally
                {

                    image1 = null;

                }

             }

    }
}



View the full article
 
Back
Top