【问题标题】:System.ArgumentException: Parameter is not valid [duplicate]System.ArgumentException:参数无效[重复]
【发布时间】:2012-01-12 23:25:57
【问题描述】:

可能重复:
“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;
        }
    }

aspx.cs 我在哪里调用 uploadimage 图像裁剪水印图像 ......

    ImageUtils.uploadImage(memberid, CroppedWaterMarkImage);

上传图片功能出错:

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

System.ArgumentException:参数无效。

谢谢 孙

【问题讨论】:

  • 如果你调试,它会停在哪一行?
  • 如果 C# 数据库库与 Java 库类似,那么您不会将“thumbimage”传递给SqlParameter 构造函数而不是“@thumbimage”吗?此外,您似乎没有为“@memberid”参数设置值。

标签: c#


【解决方案1】:

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

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

任何一种(内存泄漏/损坏和/或 API 选择)都可能适用于您的场景。

还要确保图像文件有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多