Leon.S
Leon.S

Reputation: 61

"Parameter is not valid" exception from System.Drawing.Image.FromStream() method

I got a hard time with the Image.FromStream method in my website. The code below works perfect on my computer. But when I uploaded it to the test server, it always gives me "Parameter not valid" exception.

if (!afuImageFile.IsUploading && afuImageFile.HasFile)
{
    System.Drawing.Image imgFile = System.Drawing.Image.FromStream(afuImageFile.FileContent);
}

the afuImageFile is an AsynFileUploader control in Ajax Tool Kits. afuImageFile.FileContent is a HttpInputStream. I guess I need to add some permission to some folder. Can anyone help me?

Upvotes: 6

Views: 11948

Answers (2)

ahmed
ahmed

Reputation: 1

// this is my solution

public async Task<IActionResult> MyController()
{
  foreach (var state in ModelState)
  {
     var x = state.Value.ValidationState;
     if (x == Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Invalid)
        {
           var v = state.Value.Errors[0].ErrorMessage;
           if (v != "Parameter is not valid.")
              {
                 return View();
              }
         }
  }
.
.
.

Upvotes: 0

Maxime Rouiller
Maxime Rouiller

Reputation: 13699

Please ensure that your FileContent stream as its position set to 0.

Otherwise, you might want to deactivate image validation by changing the call from:

System.Drawing.Image imgFile = System.Drawing.Image.FromStream(afuImageFile.FileContent);

to:

System.Drawing.Image imgFile = System.Drawing.Image.FromStream(afuImageFile.FileContent, true, false);

See Image.FromStream to check on the other overloads.

Upvotes: 2

Related Questions