Out of memory exception while loading images

前端 未结 4 1558
予麋鹿
予麋鹿 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: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.

提交回复
热议问题