Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.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# 将SQL Server CE中的varbinary位图加载到picturebox中_C#_Winforms_Bytearray_Sql Server Ce_Memorystream - Fatal编程技术网

C# 将SQL Server CE中的varbinary位图加载到picturebox中

C# 将SQL Server CE中的varbinary位图加载到picturebox中,c#,winforms,bytearray,sql-server-ce,memorystream,C#,Winforms,Bytearray,Sql Server Ce,Memorystream,我正在尝试将预保存的图像从SQL Server CEVarBinary列加载到PictureBox中 列内容是以varbinary格式存储的位图图像 MemoryStream ms = new MemoryStream(); byte[] outbyte = new byte[100]; Int32 ordinal = 0; conn.Open(); SqlCeDataReader reader = cmd.ExecuteReader(); while (reader.Read()) {

我正在尝试将预保存的图像从SQL Server CE
VarBinary
列加载到
PictureBox

列内容是以
varbinary
格式存储的位图图像

MemoryStream ms = new MemoryStream();
byte[] outbyte = new byte[100];
Int32 ordinal = 0;

conn.Open();
SqlCeDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
     ordinal = reader.GetOrdinal("FaceStamp");//FaceStamp: VarBinary column storing Bmp.
     outbyte = (byte[])reader[ordinal];
     ms.Write(outbyte, 0, outbyte.Length);
     ms.Seek(0, SeekOrigin.Begin);
     pictureBox1.Image = Image.FromStream(ms);
}          

conn.Close();

// Code below is the code I used to save the Bitmap image to the database
Bitmap bmi = cam.GetBitmap(); // Capture image from webcam which I've tested working.
ImageConverter converter = new ImageConverter();
byte[] byteArray = new byte[0];
byteArray = (byte[])converter.ConvertTo(bmi, typeof(byte[])); 
insert.Parameters.AddWithValue("@image", byteArray);
insert.ExecuteNonQuery();
我在以下行中得到一个错误:

pictureBox1.Image = Image.FromStream(ms);

{“参数无效。”}

有什么建议吗


谢谢。

在转换保存为字节数组的图像时,我通常会执行以下操作:

byte[] outbyte = (byte[])reader[ordinal];
using (MemoryStream ms = new MemoryStream(outbyte))
{
    Bitmap image = new Bitmap(ms);
    pictureBox1.Image = image;
}

要将原始图像转换为字节数组,您可以查看。

您确定您的
图像
已正确地作为
字节[]保存到数据库中吗?至少你必须知道如何将图像转换成
字节[]
才能相应地转换回来。是的,我检查了字段,可以看到其中包含二进制数据。不,二进制数据并不意味着可以通过这种方式将图像转换成
。如果使用
序列化
将位图数据转换为
字节[]
,则必须使用
反序列化
将其从
字节[]
中取回。我在位图图像=新位图(ms)行中遇到相同错误。您是否按照另一个SO问题中的MemoryStream示例使用System.Drawing.Imaging.ImageFormat.Bmp而不是Png保存图像?