none
System.Drawing.Image.FromFile OutOfMemory Exception For Image Converter RRS feed

  • Question

  • 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;

                    }

                 }

        }
    }

    • Edited by Shanos Thursday, November 27, 2008 12:22 AM
    Wednesday, November 26, 2008 10:54 PM

All replies

  • Not sure if this has anything to do with your problem, but the filter should be "*.tif", not just "tif".

    I would also call Dispose() in your finally block.


    Ron Whittle - If the post is helpful or answers your question, please mark it as such.
    Wednesday, November 26, 2008 11:13 PM
  • Sorry, I did have that *.tif in there originally (In a configuration file), tried to post code without the xml stuff to clear it up.
    Thursday, November 27, 2008 12:18 AM
  • 1 finally {  
    2     image1.Dispose();  
    3

     Did you try changing your 'finally' to the above?

    Ron Whittle - If the post is helpful or answers your question, please mark it as such.
    Thursday, November 27, 2008 1:50 AM
  • Yeah, same issue unfortunately with .dispose() .close() and every other one I could find.
    I have also tried it with a fileStream instead.

    I have been reading some info that the exception may have something to do with file permissions and how Windows locks the files.

    Any ideas?


    Thursday, November 27, 2008 7:28 AM
  • Hi,

    As I learnt from your posts,when you add a specific type of file to 'c:\temp\', an OutOfMemory exception is thrown.

    This may caused  by  file format or the size of the file:
    When "The file does not have a valid image format" ,a "OutOfMemoryException"  will be throw.(Please See:http://msdn.microsoft.com/en-us/library/4sahykhd.aspx)
    The reasons of this are:
    1 the file is empty.
    2 the image have invalid format ,like random text .

    If the file is empty ,we can first read the file into a stream and check if it is empty .You can replace the the try block in my example below with one that  you are using.

                    try  
                    {  
                        Stream s = File.Open(FileName, FileMode.Open);  
                        if (s != null)  
                        {  
                            if (s.Length != 0)  
                            {  
                                Image image1 = Image.FromStream(s);  
                                //  Bitmap image1 = (Bitmap)Image.FromFile(@FileName, true);  
                                // 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");  
                            }  
                            else  
                            {  
                                Console.WriteLine("a new file created");  
                            }  
                        }  
                    } 

    If the file has a invalid format ,we can catch the exception .

    Best regards,
    Harry
    • Marked as answer by Harry Zhu Friday, December 5, 2008 1:35 AM
    • Unmarked as answer by Harry Zhu Monday, December 8, 2008 8:28 AM
    Thursday, December 4, 2008 4:41 AM
  • Hi,

    I am loading a multi page tiff image. It works fine for black & white images, but for grayscale images code throws 'Out of memory' exception for this code.
    System.Drawing.Image.FromFile(sFileName);


    On changing to code as suggested above I get a 'Parameter is not valid' exception.

    Stream s = File.Open(sFileName,FileMode.Open); if (s != null) { if (s.Length != 0) { Image = System.Drawing.Image.FromStream(s,false); } }
    • Edited by Thejassu Monday, December 7, 2009 2:54 PM Code
    Monday, December 7, 2009 2:52 PM
  • my problem is same as Thejassu said.

    is there any advise for using grayscale tiff images?

     

    Monday, July 19, 2010 10:49 AM
  • my problem is same as Thejassu said.

    is there any advise for using grayscale tiff images?

    Use the Presentation Framework.
    Monday, July 19, 2010 11:33 AM
  • My problem is same when taking tiff file it works well but when I am taking pdf file shows OutOfMemoryException No use ofDispose and null

    Please help.

    Thanks in advance


    P.
    Tuesday, December 20, 2011 12:21 PM
  • pdf is not an image format.
    Tuesday, December 20, 2011 12:48 PM
  • yes i know but It is possible to take pdf file and then scan take number on that file ansd save in folder using that number.

    thanks in advance


    P.
    Tuesday, December 20, 2011 1:58 PM