首页 > 解决方案 > 错误:“参数无效”,将字节 [] 转换为图像时

问题描述

因此,我试图将字节数组转换为图像,但出现此错误:

System.ArgumentException:“参数无效。” byte[] 来自 SQL Server 中的数据库,其中数据类型为“图像”。

我已经尝试了在 StackOverflow 的其他问题中找到的所有解决方案,但它们对我不起作用;这是我的代码:

//biblioteca.herramientas is where I make the connection with the server
DataSet dataset_Image;
dataset_Image = Biblioteca.Herramientas(string.Format("SELECT * FROM Image WHERE id_Image = " + 1));

array = (byte[])dataset_Image.Tables[0].Rows[0]["image"];

public byte[] TheImage
{
    set
    {
        theImage = ImageConversor.ByteArrayToImage(value);
        PictureBox2.Image = theImage;
    }
}

这是我有错误的地方:

public class ImageConversor
{
    public static Image ByteArrayToImage(byte[] byteArrayIn)
    { //HERE THE ERROR APPEARS
        MemoryStream ms = new MemoryStream(byteArrayIn);
        Image returnImage = Image.FromStream(ms);
        return returnImage;
    }
}

The following picture is the SQL table that I am accessing to:

SQL 表

仅此而已,如果您需要更多信息,我会分享,谢谢您的宝贵时间,希望您有美好的一天。(:

编辑: 如果我直接从 SQLS 添加图片,代码工作正常,将列“图像”(存储图片的位置)数据类型从“图像”更改为“varbinary(MAX)”,然后使用以下代码插入图像:

insert into Image select * from openrowset (bulk N'picture directory', single_blob) as image

问题是我不能用它来添加图片,但我不知道,也许它有助于解决这个问题:/

标签: c#sqlwindows-forms-designer

解决方案


推荐阅读