user3306226
user3306226

Reputation: 11

System.Drawing.Bitmap Parameter is not valid

I am having this error:

parameter is not valid.

On this line:

System.Drawing.Bitmap("~\\father\\chocolate.png");

Upvotes: 0

Views: 2448

Answers (2)

user7297973
user7297973

Reputation:

It is probably a little late for this suggestion, but for the sake of anyone else viewing this post:

If you are using/opening your image somewhere in your code, this could be a good reason.

Make sure you close any memory stream or instances of the image object after you are done.

I just came across this error in the form of: an HttpException (0x80004005): Parameter is not valid.

After debugging I realized that disposing of the image had everything to do with this error.

The amazing part was that while the image was already stored in our database and not being used, it would seem the function holds on to it.

If you have instantiated the png file as an Image object, make sure you close any MemoryStream/Stream objects and dispose of the image (set it to nothing) and perform a garbage collection to release it from memory.

hope this helps!

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503529

It's very unlikely that "~\\father\\chocolate.png" is a valid filename on its own - I suspect you want to map that from an ASP.NET somewhat-relative filename to a real local filename first.

For example:

var bitmap = new Bitmap(Server.MapPath("~/father/chocolate.png"));

(I'd personally suggest using forward slashes instead of backslashes here - they work just as well under Windows; they'll still work under Linux; they don't need escaping.)

Upvotes: 3

Related Questions