Tôi muốn chuyển đổi mảng byte thành hình ảnh.Byte Array để chuyển đổi hình ảnh
Đây là mã cơ sở dữ liệu của tôi từ nơi tôi nhận được mảng byte:
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;
}
mã chuyển đổi của tôi:
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;
}
Khi tôi đạt được phù hợp với nhận xét thì ngoại lệ sau đây xảy ra: Parameter is not valid.
Làm cách nào để khắc phục bất kỳ điều gì gây ra ngoại lệ này?
Bạn đã kiểm tra xem các byte hình ảnh trong truy vấn của mình có hợp lệ không? Bạn có thể làm một File.WriteAllBytes ("myimage.jpg", byteArrayIn) để xác minh. – Holstebroe