System.ArgumentException:参数无效

本文关键字:无效 参数 ArgumentException System | 更新日期: 2023-09-27 18:20:06

可能重复:
“参数无效”加载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,我在其中调用uploadimageimage CroppedWaterMarkImage……

    ImageUtils.uploadImage(memberid, CroppedWaterMarkImage);

上传图像功能错误:

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

System.ArgumentException:参数无效。

谢谢Sun

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的选择)都可能适用于您的场景。

还要确保图像文件有效。