バイト配列を画像に変換したい。
これはバイト配列を取得するデータベース コードです。
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.
この例外の原因を修正するにはどうすればいいでしょうか?
ベストアンサー1
メモリ ストリームに 2 回書き込み、使用後にストリームを破棄していません。また、イメージ デコーダーに埋め込み色補正を適用するように要求しています。
代わりにこれを試してください:
using (var ms = new MemoryStream(byteArrayIn))
{
return Image.FromStream(ms);
}