2

Possible Duplicate:
“Parameter not valid” exception loading System.Drawing.Image

I am inserting a image in the DB.

Here's my code

public class ImageUtils
{
    const int sizeThumb = 69;

    public static int uploadImage(int memberid, Image thumbimage)
    {
        MemoryStream stream = new MemoryStream();
        thumbimage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
        Byte[] thumbbytes = stream.ToArray();

        //int length = Convert.ToInt32(data.Length);
        //byte[] thumbimage = new byte[length];
        //data.Read(thumbimage, 0, length);
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["FMMImages"].ConnectionString);
        SqlCommand command = new SqlCommand("update Images_temp set thumbimage = @thumbimage where memberid=@memberid", connection);
        SqlParameter param0 = new SqlParameter("@thumbimage", SqlDbType.Image);
        param0.Value = thumbbytes;
        command.Parameters.Add(param0);

        connection.Open();

        object result = command.ExecuteScalar();
        connection.Close();
        if (result != null)
        {
            return System.Convert.ToInt32(result);
        }
        else
        { 
            return 0;
        }
    }

aspx.cs where I'm calling the uploadimage image CroppedWaterMarkImage ......

    ImageUtils.uploadImage(memberid, CroppedWaterMarkImage);

Error in the uploadimage function:

     MemoryStream stream = new MemoryStream();
     thumbimage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
     Byte[] thumbbytes = stream.ToArray();

System.ArgumentException: Parameter is not valid.

Thanks Sun

Community
  • 1
  • 1
user575219
  • 2,346
  • 15
  • 54
  • 105
  • If you debug, which line does it stop on? – Joe Jan 12 '12 at 23:29
  • If the C# database libraries are anything like the Java ones, then wouldn't you pass "thumbimage" to the `SqlParameter` constructor instead of "@thumbimage"? Also, you don't seem to be setting a value for the "@memberid" parameter. – aroth Jan 12 '12 at 23:30
  • http://stackoverflow.com/questions/629955/parameter-not-valid-exception-loading-system-drawing-image – Tim Schmelter Jan 12 '12 at 23:31

1 Answers1

1

These guys encountered a similar problem with Images and MemoryStream() because of a memory leak:

This link was resolved by calling System.Drawing.Bitmap instead of System.Drawing.Image:

Either (memory leak/corruption and/or choice of API) could be applicable to your scenario.

Also make sure the image file is valid.

paulsm4
  • 114,292
  • 17
  • 138
  • 190