Search code examples
c#asp.netimageuploaduploadify

ASP.NET Image Upload Parameter Not Valid. Exception


Im just trying to save a file to disk using a posted stream from jquery uploadify

I'm also getting Parameter not valid.

On adding to the error message so i can tell where it blew up in production im seeing it blow up on:

   var postedBitmap = new Bitmap(postedFileStream)

any help would be most appreciated

public string SaveImageFile(Stream postedFileStream, string fileDirectory, string fileName, int imageWidth, int imageHeight)
{
    string result = "";
    string fullFilePath = Path.Combine(fileDirectory, fileName);
    string exhelp = "";

    if (!File.Exists(fullFilePath))
    {
        try
        {
            using (var postedBitmap = new Bitmap(postedFileStream))
            {
                exhelp += "got past bmp creation" + fullFilePath;

                using (var imageToSave = ImageHandler.ResizeImage(postedBitmap, imageWidth, imageHeight))
                {
                    exhelp += "got past resize";

                    if (!Directory.Exists(fileDirectory))
                    {
                        Directory.CreateDirectory(fileDirectory);
                    }

                    result = "Success";
                    postedBitmap.Dispose();
                    imageToSave.Save(fullFilePath, GetImageFormatForFile(fileName));
                }

                exhelp += "got past save";
            }
        }
        catch (Exception ex)
        {
            result = "Save Image File Failed " + ex.Message + ex.StackTrace;
            Global.SendExceptionEmail("Save Image File Failed " + exhelp, ex);
        }
    }

    return result;
}

Solution

  • Use image converter to load from byte array:

    ImageConverter imageConverter = new System.Drawing.ImageConverter();
    Image image = imageConverter.ConvertFrom(byteArray) as Image;
    

    http://forums.asp.net/t/1109959.aspx