Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Image.FromFile and exceptions

Image.FromFile and exceptions

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpdotnetwinformscomgraphics
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Daniel Lo Nigro
    wrote on last edited by
    #1

    In the .NET Framework, what exception would you expect Image.FromFile to throw if the image is invalid? Something like an "InvalidImageFormatException" or similar? Nope. It throws an OutOfMemoryException. MSDN says:

    OutOfMemoryException
    The file does not have a valid image format.
    -or-
    GDI+ does not support the pixel format of the file.

    Really? I doubt the system is actually running out of memory as it returns quickly.

    L A 2 Replies Last reply
    0
    • D Daniel Lo Nigro

      In the .NET Framework, what exception would you expect Image.FromFile to throw if the image is invalid? Something like an "InvalidImageFormatException" or similar? Nope. It throws an OutOfMemoryException. MSDN says:

      OutOfMemoryException
      The file does not have a valid image format.
      -or-
      GDI+ does not support the pixel format of the file.

      Really? I doubt the system is actually running out of memory as it returns quickly.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      It does what the documentation says it does. That is positive in general, and it sounds a bit weird in this case. Anyhow, it is a lot better than getting a "General error occurred in GDI+" which can mean almost anything. :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      1 Reply Last reply
      0
      • D Daniel Lo Nigro

        In the .NET Framework, what exception would you expect Image.FromFile to throw if the image is invalid? Something like an "InvalidImageFormatException" or similar? Nope. It throws an OutOfMemoryException. MSDN says:

        OutOfMemoryException
        The file does not have a valid image format.
        -or-
        GDI+ does not support the pixel format of the file.

        Really? I doubt the system is actually running out of memory as it returns quickly.

        A Offline
        A Offline
        Abhinav S
        wrote on last edited by
        #3

        You are probably trying to read an image that is not valid. If you notice, there is no such exception like "Invalid Image" listed out.

        OutOfMemoryException
        The file does not have a valid image format.
        -or-
        GDI+ does not support the pixel format of the file.

        FileNotFoundException
        The specified file does not exist.

        ArgumentException
        filename is a Uri.

        D 1 Reply Last reply
        0
        • A Abhinav S

          You are probably trying to read an image that is not valid. If you notice, there is no such exception like "Invalid Image" listed out.

          OutOfMemoryException
          The file does not have a valid image format.
          -or-
          GDI+ does not support the pixel format of the file.

          FileNotFoundException
          The specified file does not exist.

          ArgumentException
          filename is a Uri.

          D Offline
          D Offline
          Daniel Lo Nigro
          wrote on last edited by
          #4

          Yeah, I'm validating that an uploaded image is actually a valid image. I was just ranting about the framework using an OutOfMemoryException to signal an exception that doesn't relate to memory at all :)

          L 1 Reply Last reply
          0
          • D Daniel Lo Nigro

            Yeah, I'm validating that an uploaded image is actually a valid image. I was just ranting about the framework using an OutOfMemoryException to signal an exception that doesn't relate to memory at all :)

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Daniel Lo Nigro wrote:

            I was just ranting about the framework using an OutOfMemoryException to signal an exception that doesn't relate to memory at all

            Not necessarily true. It could be a bug in GDI+ that recursively tries to load the bad image and results in a real Out of Memory situation. :-)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups