简体   繁体   中英

how to solve system.outofmemoryexception in c#

When we upload a video and click into button to convert video into thumbnail image using this code it generate an error 'System.OutOfMemoryException: Out of memory.'

We are using this code

fpUplaodVideo.SaveAs(Server.MapPath("~/UploadFiles/SiteUserID_" + SiteUserID + "/UploadVideo/" + fpUplaodVideo.FileName));

error will occur on this line

System.Drawing.Image img1 = System.Drawing.Image.FromFile(Server.MapPath("~/UploadFiles/SiteUserID_" + SiteUserID + "/UploadVideo/") + fpUplaodVideo.FileName);

System.Drawing.Image.FromFile will return OutOfMemoryException for below reason

  1. The file does not have a valid image format.
  2. GDI+ does not support the pixel format of the file.

The MSDN documentation for Image.FromFile says this:

OutOfMemoryException

The file does not have a valid image format.

-or-

GDI+ does not support the pixel format of the file.

Granted, this is a bad and nonsensical use of the OutOfMemoryException , but at least it's documented.

As to why this is thrown, it's quite simple - you're trying to load a video file as an image file. The same docs show that the following formats are supported:

  • BMP
  • GIF
  • JPEG
  • PNG
  • TIFF

I assume your "UploadVideo" is none of these. In fact, I'm not sure why you assumed that this would produce a thumbnail image. Try looking for a specific library for that. This might be a good start: Thumbnail video C#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM