我想將字節數組轉換爲圖像。字節數組到圖像轉換
這是從哪裏獲得的字節數組我的數據庫代碼:
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