Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 错误:";参数无效";,将字节[]转换为图像时_C#_Sql_Windows Forms Designer - Fatal编程技术网

C# 错误:";参数无效";,将字节[]转换为图像时

C# 错误:";参数无效";,将字节[]转换为图像时,c#,sql,windows-forms-designer,C#,Sql,Windows Forms Designer,因此,我试图将字节数组转换为图像,但出现以下错误: System.ArgumentException:'参数无效。'字节[]来自SQL server中数据类型为“image”的数据库 我已经尝试了StackOverflow中其他问题中提供的所有解决方案,但这些解决方案对我来说并不奏效;这是我的密码: //biblioteca.herramientas is where I make the connection with the server DataSet dataset_Image; dat

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

System.ArgumentException:'参数无效。'字节[]来自SQL server中数据类型为“image”的数据库

我已经尝试了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:

就这些,如果你需要更多的信息,我会与你分享,谢谢你的时间,希望你有一个美好的一天。(:

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

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

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

看起来存储在数据库中的图像数据有问题。我尝试过类似于
var bytes=File.ReadAllBytes(@“path\to\an\image.png”);var ms=new MemoryStream(bytes)的代码;Image img=Image.FromStream(毫秒);
,它可以按预期工作。相反,如果我传入一个随机字节数组,我还会得到您提到的
ArgumentException
。可能,您的数据库中有一个不受支持的图像类型。该图像是什么格式的?您是否尝试将其保存为文件以查看它是否是有效图像?可能数据已损坏。可能是重复的您是否已确认您的代码正在获取整个图像,而不仅仅是第一个X字节?@nollidge我已经实现了该页面上提供的解决方案,它仍然会给我错误。):