Out of memory exception while loading images

前端 未结 4 1556
予麋鹿
予麋鹿 2021-01-14 06:23

I am using the following piece of code to load images as thumbnails to a FlowLayoutPanel control. Unfortunately i get an OutOfMemory exception.

As you already guess

相关标签:
4条回答
  • 2021-01-14 06:38

    You can solve this in a few steps:

    • to get free from the File-dependency, you have to copy the images. By really drawing it to a new Bitmap, you can't just copy it.
    • since you want thumbnails, and your source-bitmaps are rather large, combine this with shrinking the images.
    0 讨论(0)
  • 2021-01-14 06:43

    Documentation for Image.FromFile (which is related to your FromStream) says that it will throw OutOfMemoryException if the file is not a valid image format or if GDI+ doesn't support the pixel format. Is it possible you're trying to load an unsupported image type?

    Also, documentation for Image.FromStream says that you have to keep the stream open for the lifetime of the image, so even if your code loaded the image you'd probably get an error because you're closing the file while the image is still active. See http://msdn.microsoft.com/en-us/library/93z9ee4x.aspx.

    0 讨论(0)
  • 2021-01-14 06:44

    Couple of thoughts:

    First off, as Jim has stated, when using Image.FromStream the stream should remain open for the lifetime of the Image as remarked on the MSDN page. As such, I would suggest to copy the contents of the file to a MemoryStream, and use the latter to create the Image instance. So you can release the file handle asap.

    Secondly, the images you're using are rather big (uncompressed, as they would exist in memory, Width x Height x BytesPerPixel). Assuming the context you use them in might allow for them to be smaller, consider resizing them, and potentially caching the resized versions somewhere for later use.

    Lastly, don't forget to Dispose the image and the Stream when they are no longer needed.

    0 讨论(0)
  • 2021-01-14 06:59

    I had the same problem. Jim Mischel answer led me to discover loading an innocent .txt file was the culprit. Here's my method in case anyone is interested.

    Here's my method:

    /// <summary>
    /// Loads every image from the folder specified as param.
    /// </summary>
    /// <param name="pDirectory">Path to the directory from which you want to load images.  
    /// NOTE: this method will throws exceptions if the argument causes 
    /// <code>Directory.GetFiles(path)</code> to throw an exception.</param>
    /// <returns>An ImageList, if no files are found, it'll be empty (not null).</returns>
    public static ImageList InitImageListFromDirectory(string pDirectory)
    {
        ImageList imageList = new ImageList();
    
        foreach (string f in System.IO.Directory.GetFiles(pDirectory))
        {
            try
            {
                Image img = Image.FromFile(f);
                imageList.Images.Add(img);
            }
            catch
            {
                // Out of Memory Exceptions are thrown in Image.FromFile if you pass in a non-image file.
            }
        }
    
        return imageList;
    }
    
    0 讨论(0)
提交回复
热议问题