我想将字节数组转换为图像。字节数组到图像转换
这是从哪里获得的字节数组我的数据库代码:
public void Get_Finger_print()
{
try
{
using (SqlConnection thisConnection = new SqlConnection(@"Data Source=" + System.Environment.MachineName + "\\SQLEXPRESS;Initial Catalog=Image_Scanning;Integrated Security=SSPI "))
{
thisConnection.Open();
string query = "select pic from Image_tbl";// where Name='" + name + "'";
SqlCommand cmd = new SqlCommand(query, thisConnection);
byte[] image =(byte[]) cmd.ExecuteScalar();
Image newImage = byteArrayToImage(image);
Picture.Image = newImage;
//return image;
}
}
catch (Exception) { }
//return null;
}
我的转换代码:
public Image byteArrayToImage(byte[] byteArrayIn)
{
try
{
MemoryStream ms = new MemoryStream(byteArrayIn,0,byteArrayIn.Length);
ms.Write(byteArrayIn, 0, byteArrayIn.Length);
returnImage = Image.FromStream(ms,true);//Exception occurs here
}
catch { }
return returnImage;
}
当我到了一个注释行,会出现以下异常:Parameter is not valid.
如何解决导致此异常的问题?
您是否检查过查询中的图像字节是否有效?你可以做一个File.WriteAllBytes(“myimage.jpg”,byteArrayIn)来验证。 – Holstebroe 2012-02-07 09:59:22