C# 将SQL Server CE中的varbinary位图加载到picturebox中
我正在尝试将预保存的图像从SQL Server CEC# 将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()) {
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保存图像?