c# - 系统参数异常 : Parameter is not valid

标签 c#

<分区>

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

aspx.cs 我调用上传图片的地方 图像 CroppedWaterMarkImage ……

    ImageUtils.uploadImage(memberid, CroppedWaterMarkImage);

上传图片函数出错:

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

System.ArgumentException: Parameter is not valid.

谢谢 太阳

最佳答案

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

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

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

还要确保图像文件有效。

关于c# - 系统参数异常 : Parameter is not valid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8843860/

相关文章:

c# - 如何修复 List<List<string>> 中的数据

c# - 无法将源接口(interface)类型转换为目标接口(interface)类型

javascript - 如何将参数从列表传递到javascript

c# - 将 IoC/DI 容器与运行时相关的构造函数参数一起使用

c# - C# 中的三元运算符和 if 语句之间有区别吗?

c# - 如何检查文件自上次读取以来是否已更改? (C#)

C# 动态 Linq : Strings are assumed to be integers?

c# - 如何获取System.Net.Mail.Attachment的内容

c# - AJAX 进度条显示页面加载的加载进度百分比

c# - BindablePicker 的 SelectedItem 在 MvvmCross + xamarin 表单中的 Bindable stackLayout 中不起作用