加载 System.Drawing.Image 时出现“参数无效”异常

问题描述 投票:0回答:10

为什么我的代码中出现“参数无效”异常:

MemoryStream ms = new MemoryStream(byteArrayIn);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);

byteArrayIn
的长度是169014。尽管事实上它的值不大于255,但我还是得到了这个异常。

c# image stream argumentexception
10个回答
21
投票

我遇到了同样的问题,显然现在已经解决了,尽管这个问题和其他一些 gdi+ 异常非常具有误导性,但我发现实际上问题是发送到位图构造函数的参数无效。我有这个代码:

using (System.IO.FileStream fs = new System.IO.FileStream(inputImage, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite))
{
    try
    {
        using (Bitmap bitmap = (Bitmap)Image.FromStream(fs, true, false))
        {
            try
            {
                bitmap.Save(OutputImage + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                GC.Collect();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
    catch (ArgumentException aex)
    {
        throw new Exception("The file received from the Map Server is not a valid jpeg image", aex);
    }
}

以下行导致错误:

Bitmap bitmap = (Bitmap)Image.FromStream(fs, true, false)

文件流是根据从地图服务器下载的文件构建的。我的应用程序错误地发送了获取图像的请求,服务器返回了带有 jpg 扩展名的内容,但实际上是一个 html 告诉我发生了错误。所以我拍摄了该图像并尝试用它构建位图。 修复方法是控制/验证图像以获得有效的 jpeg 图像。

希望有帮助!


15
投票

我的猜测是

byteArrayIn
不包含有效的图像数据。

请提供更多信息:

  • 哪一行代码抛出异常?
  • 消息是什么?
  • 您从哪里获得
    byteArrayIn
    ,您确定它应该包含有效的图像吗?

8
投票
byte[] fileData = null;
using (var binaryReader = new BinaryReader(Request.Files[0].InputStream))
{
    fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength);
}
ImageConverter imageConverter = new System.Drawing.ImageConverter();
System.Drawing.Image image = imageConverter.ConvertFrom(fileData) as System.Drawing.Image;
image.Save(imageFullPath, System.Drawing.Imaging.ImageFormat.Jpeg);

7
投票

Image.FromStream()
抛出的“参数无效”异常告诉您该流不是“有效”或“可识别”格式。观察内存流,特别是当您从文件中获取各种字节偏移量时。

// 1. Create a junk memory stream, pass it to Image.FromStream and 
// get the "parameter is not valid":
MemoryStream ms = new MemoryStream(new Byte[] {0x00, 0x01, 0x02});
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);`

// 2. Create a junk memory stream, pass it to Image.FromStream
// without verification:
MemoryStream ms = new MemoryStream(new Byte[] {0x00, 0x01, 0x02});
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms, false, true);

示例 2 将起作用,请注意 useEmbeddedColorManagement 必须为 false 才能使 validateImageData 有效。

通过将内存流转储到文件并检查内容可能是最容易调试的。


3
投票

哪一行抛出异常?

new MemoryStream(...)
?还是
Image.FromStream(...)
?什么是
byteArrayIn
?是
byte[]
吗?我只是因为评论“并且其中的任何值都不大于 255”而问 - 这当然对于
byte[]
来说是自动的。

一个更明显的问题:二进制文件实际上包含合理格式的图像吗?

例如,以下代码(虽然不是很好的代码)可以正常工作:

    byte[] data = File.ReadAllBytes(@"d:\extn.png"); // not a good idea...
    MemoryStream ms = new MemoryStream(data);
    Image img = Image.FromStream(ms);
    Console.WriteLine(img.Width);
    Console.WriteLine(img.Height);

1
投票

此错误是由于将二进制数据插入缓冲区而引起的。 要解决这个问题,您应该在代码中插入一条语句。

此声明为:

obj_FileStream.Read(Img, 0, Convert.ToInt32(obj_FileStream.Length));

示例:

FileStream obj_FileStream = new FileStream(str_ImagePath, FileMode.OpenOrCreate, FileAccess.Read);
Byte[] Img = new Byte[obj_FileStream.Length];
obj_FileStream.Read(Img, 0, Convert.ToInt32(obj_FileStream.Length));         
dt_NewsFeedByRow.Rows[0][6] = Img;

0
投票
来自

.NET 文档的 Image.FromStream() 方法

异常 ArgumentException 该流没有有效的图像 格式。

OutOfMemoryException 该流没有有效的图像格式。

我的意思是,主要问题是字节数组的格式不正确。

您可以使用

validateImageData
参数,但它仍然会抛出异常.. 我注意到,如果您的字节数组是从
Image.FormStream()
png
等旧扩展创建的,则
jpg
方法只能正确流式传输。我使用了带有
webp
的图像,它抛出异常。您必须转换为旧格式(例如 jpeg)才能正确使用。有几个用于转换图像的库。

我建议SixLabors.ImageSharp.Web

第一

dotnet add package SixLabors.ImageSharp.Web

安装库后

var imageAsStream = new MemoryStream(imageasByteArray);

Image image = null;
try {
    image = Image.FromStream(imageAsStream);
}
catch(ArgumentException) {
    using var imageSharp = SixLabors.ImageSharp.Image.Load(new ReadOnlySpan<byte>(imageasByteArray));
    imageAsStream = new MemoryStream();
    imageSharp.Save(imageAsStream, new JpegEncoder());
    image = Image.FromStream(imageAsStream);
}

-2
投票

给出的所有解决方案都不起作用..不要只关注检索部分。很高兴插入图像。我也犯了同样的错误。我从硬盘中取出图像并将其保存到数据库中。问题出在插入命令上。很高兴看到我的故障代码..:

 public bool convertImage()
    {
        try
        {
            MemoryStream ms = new MemoryStream();
            pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
            photo = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(photo, 0, photo.Length);
            return true;
        }
        catch
        {
            MessageBox.Show("image can not be converted");
            return false;
        }
    }
    public void insertImage()
    {
       // SqlConnection con = new SqlConnection();
        try
        {
            cs.Close();
            cs.Open();
            da.UpdateCommand = new SqlCommand("UPDATE All_students SET disco = " +photo+" WHERE Reg_no = '" + Convert.ToString(textBox1.Text)+ "'", cs);
            da.UpdateCommand.ExecuteNonQuery();
            cs.Close();
            cs.Open();
            int i = da.UpdateCommand.ExecuteNonQuery();
            if (i > 0)
            {
                MessageBox.Show("Successfully Inserted...");
            }

        }
        catch
        {
            MessageBox.Show("Error in Connection");
        }
        cs.Close();
    }

上面的代码显示成功插入...但实际上它以错误的数据类型的形式保存图像..而数据类型必须bt“图像”..所以我改进了代码..

  public bool convertImage()
    {
        try
        {
            MemoryStream ms = new MemoryStream();
            pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
            photo = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(photo, 0, photo.Length);
            return true;
        }
        catch
        {
            MessageBox.Show("image can not be converted");
            return false;
        }
    }
    public void insertImage()
    {
       // SqlConnection con = new SqlConnection();
        try
        {
            cs.Close();
            cs.Open();
            //THIS WHERE THE CODE MUST BE CHANGED>>>>>>>>>>>>>>

            da.UpdateCommand = new SqlCommand("UPDATE All_students SET disco = @img WHERE Reg_no = '" + Convert.ToString(textBox1.Text)+ "'", cs);
            da.UpdateCommand.Parameters.Add("@img", SqlDbType.Image);//CHANGED TO IMAGE DATATYPE...
            da.UpdateCommand.Parameters["@img"].Value = photo;
            da.UpdateCommand.ExecuteNonQuery();
            cs.Close();
            cs.Open();
            int i = da.UpdateCommand.ExecuteNonQuery();
            if (i > 0)
            {
                MessageBox.Show("Successfully Inserted...");
            }

        }
        catch
        {
            MessageBox.Show("Error in Connection");
        }
        cs.Close();
    }

100% 保证检索时不会出现 PARAMETER NOT VALID 错误....已解决!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!


-4
投票

大多数情况下,发生这种情况是 SQL 列中的数据错误。这是插入图像列的正确方法:

INSERT INTO [TableX] (ImgColumn) VALUES (
(SELECT * FROM OPENROWSET(BULK N'C:\....\Picture 010.png', SINGLE_BLOB) as tempimg))

大多数人这样做都是错误的:

INSERT INTO [TableX] (ImgColumn) VALUES ('C:\....\Picture 010.png'))

-6
投票

只需按照此操作将值插入数据库

//连接字符串

  con.Open();

sqlQuery = "INSERT INTO [dbo].[Client] ([Client_ID],[Client_Name],[Phone],[Address],[Image]) VALUES('" + txtClientID.Text + "','" + txtClientName.Text + "','" + txtPhoneno.Text + "','" + txtaddress.Text + "',@image)";

                cmd = new SqlCommand(sqlQuery, con);
                cmd.Parameters.Add("@image", SqlDbType.Image);
                cmd.Parameters["@image"].Value = img;
            //img is a byte object
           ** /*MemoryStream ms = new MemoryStream();
            pictureBox1.Image.Save(ms,pictureBox1.Image.RawFormat);
            byte[] img = ms.ToArray();*/**

                cmd.ExecuteNonQuery();
                con.Close();
© www.soinside.com 2019 - 2024. All rights reserved.