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




我正在数据库中插入图像。

这是我的代码

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;
        }
    }


我调用uploadimage的aspx.cs
      图片CroppedWaterMarkImage
        ......

    ImageUtils.uploadImage(memberid, CroppedWaterMarkImage);


uploadimage函数错误:

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



  System.ArgumentException:参数无效。


谢谢
太阳

最佳答案

由于内存泄漏,这些人在Images和MemoryStream()上遇到了类似的问题:


http://blog.lavablast.com/post/2007/11/29/The-Mysterious-Parameter-Is-Not-Valid-Exception.aspx


通过调用System.Drawing.Bitmap而不是System.Drawing.Image来解决此链接:


http://forums.asp.net/t/1705636.aspx/1
http://msdn.microsoft.com/en-us/library/ms524632%28v=vs.90%29.aspx


两者(内存泄漏/损坏和/或API的选择)都可能适用于您的方案。

还要确保图像文件有效。

关于c# - System.ArgumentException:参数无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8843860/

10-12 00:11
查看更多