Share via


Upload and change an image results in Parameter is not valid

Question

Wednesday, August 13, 2014 11:51 AM

Hi,

I have problems when trying to upload a picture, and do some stuff with them.
I sometimes, not every time, get error message: Parameter is not valid.

In my aspx.cs page I have this code where FileUploadMyImage is a asp:FileUpload:

byte[] file = constants.UploadPicture(FileUploadMyImage);
FileUploadMyImage.Dispose();
resizedpic200 = constants.ResizePicture(file, 200);
resizedpic600 = constants.ResizePicture(file, 600);

I also have this constants class with:

public static class constants
    {
        public static byte[] UploadPicture(System.Web.UI.WebControls.FileUpload fileupload)
        {
            fileupload.PostedFile.InputStream.Position = 0;
            byte[] bUFile = new byte[fileupload.PostedFile.InputStream.Length];
            fileupload.PostedFile.InputStream.ReadAsync(bUFile, 0, bUFile.Length);
            return bUFile;
        }

        public static byte[] ResizePicture(byte[] file, int MaxXYpixels)
        {
            System.Drawing.Image img;
            System.Drawing.ImageConverter convimg = new System.Drawing.ImageConverter();
            img = (System.Drawing.Image)convimg.ConvertFrom(file); //Crashes
// Here I do a lot of stuff
img.Dispose();
            System.Drawing.ImageConverter conv = new System.Drawing.ImageConverter();
            byte[] smallpic = (byte[])conv.ConvertTo(bm, typeof(byte[]));
            return smallpic;
  }
}

It is always the row img = (System.Drawing.Image)convimg.ConvertFrom(file); that crashes.
As you can see, it calls the ResizePicture two times per upload.

And it works like 50% of the times. Now it seems that the error appear the second time I try to upload when MaxXYpixels is 200.

Please help
/Magnus

Magnus Burk

All replies (2)

Thursday, August 14, 2014 6:06 AM âś…Answered

Hi Magnus,

There may be some restrictions if you use ImageConverter to convert the byte array to Image using this method. The second overloaded ConvertFrom method with 3 parameters are used to convert to Image whereas the method you use is actually from TypeConverter class. So the second method is better than the first one.

http://msdn.microsoft.com/en-us/library/system.drawing.imageconverter.convertfrom(v=vs.110).aspx

Also try to use Image.FromStream to convert byte array to Image, because the ImageConverter.ConvertFrom method is actually use Image.FromStream method inside it:

public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}

And save the Image back to byte array with this method:

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
 MemoryStream ms = new MemoryStream();
 imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
 return  ms.ToArray();
}

To resize a Image, I recommend you use the method in this article:

C#: Image resize, convert, and save

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.


Wednesday, August 20, 2014 7:21 PM

Hi,

Sorry for not getting back earlier. I have decided to change from storing images in SQL to store as files. Probably that will be a good path. I read your link C#: Image resize, convert, and save, and found a lot of good stuff in there.

Hopefully my original issue is automatically solved when using file storage and your info.

Thanks a lot!
/Magnus

Magnus Burk