我正在尝试将保存在MySQL数据库中的图像作为BLOB图像保存到我的Windows应用程序的图片框中。
这是我的代码。
private Image GetFaceFromDB()
{
Image fetchedImage;
if (rowNumber >= 0)
{
byte[] faceTobyte = (byte[])dataTable.Rows[rowNumber]
["Image"];
MemoryStream stream = new MemoryStream(faceTobyte);
fetchedImage = Image.FromStream(stream); **//Error at this
line**
return fetchedImage;
}else
{
MessageBox.Show("There are no images in thedatabase,please
insert images.");
return null;
}
}
这是按钮点击事件
private void btnFirst_Click(object sender, EventArgs e)
{
RefreshDBConnection();
rowNumber = 0;
pbTrainingSetImages.Image = GetFaceFromDB();
tbUpdateFaceName.Text = dataTable.Rows[rowNumber]["Name"].ToString();
lbFaceNumber.Text = (rowNumber + 1).ToString();
if(pbTrainingSetImages.Image != null)
{
MessageBox.Show("You have reached the First image in the
database!");
}
}
我还尝试了以下代码
A
private void btnFirst_Click(object sender, EventArgs e)
{
RefreshDBConnection();
rowNumber = 0;
pbTrainingSetImages.Image = GetFaceFromDB();
tbUpdateFaceName.Text = dataTable.Rows[rowNumber]
["Name"].ToString();
lbFaceNumber.Text = (rowNumber + 1).ToString();
if(pbTrainingSetImages.Image != null)
{
MessageBox.Show("You have reached the First image in the
database!");
}
}
乙
private Image GetFaceFromDB()
{
Image fetchedImage;
if (rowNumber >= 0)
{
byte[] faceTobyte= (byte[])dataTable.Rows[rowNumber["Image"];
MemoryStream stream = new MemoryStream();
stream.Position = 0;
return fetchedImage = Image.FromStream(stream, false);
}
else
{
MessageBox.Show("There are no images in the database, please
insert
images.");
return null;
}
}
C
private Image GetFaceFromDB()
{
Image fetchedImage;
if (rowNumber >= 0)
{
byte[] faceTobyte = (byte[])dataTable.Rows[rowNumber]["Image"];
MemoryStream stream = new MemoryStream();
stream.Position = 0;
return fetchedImage = Image.FromStream(stream, false);
}
else
{
MessageBox.Show("There are no images in the database,please insert
images.");
return null;
}
}
d
private Image GetFaceFromDB()
{
Image fetchedImage;
if (rowNumber >= 0)
{
byte[] faceTobyte = (byte[])dataTable.Rows[rowNumber]
["Image"];
using(var stream = new MemoryStream(faceTobyte))
{
fetchedImage = Image.FromStream(stream);
return fetchedImage;
}
}
else
{
MessageBox.Show("There are no images in the database, please
insert images.");
return null;
}
}
我也尝试了这些Stack Overflow链接中提供的大部分答案:
这是错误消息参数在此行无效
fetchedImage = Image.FromStream(stream);
这是我的插入声明
private void AddFacesToDB(Image inputImage, string inputLabel)
{
if (connection.State.Equals(ConnectionState.Closed))
{
connection.Open();
}
try
{
//convert the face image to byte format for storing in db
byte[] facesToByte = ConvertFaceToDBFormat(inputImage);
totalRows++;
//At what location is the image being stored to
MessageBox.Show($"Adding row {totalRows.ToString()}");
//inserting the image into the row
MySqlCommand insertCommand = new MySqlCommand("insert into
testimages.image_information(FaceID, FaceNo, Name, Image) values('" +
totalRows.ToString() + "','" + totalRows.ToString() + "', '" +
tbSetFaceLabel.Text + "', @IMG)", connection);
MySqlParameter imageParam =
insertCommand.Parameters.AddWithValue("@IMG","Image");
imageParam.Value = facesToByte;
imageParam.Size = facesToByte.Length;
int rowAffected = insertCommand.ExecuteNonQuery();
MessageBox.Show("Data Stored Successfully in " +
rowAffected.ToString() + " Rows");
}catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
RefreshDBConnection();
}
}
这是我的数据库
答案 0 :(得分:0)
我终于解决了它。花了6个小时后。坚持不懈。我的插入声明是问题所在,
byte[] faceTobyte = (byte[])dataTable.Rows[rowNumber]["Image"];
我一直在解析" Image"字节不知道我没有解析有效的图像格式。所以我编辑了我的insert语句:
//At what location is the image being stored to
MessageBox.Show($"Adding row {totalRows.ToString()}");
//inserting the image into the row
MySqlCommand insertCommand = new MySqlCommand("insert into testimages.image_information(FaceID, FaceNo, Name, Image)
values(@faceid, @faceno, @name, @img)", connection);
MySqlParameter imageParam;
insertCommand.Parameters.Add("@faceid", MySqlDbType.Int16,25);
insertCommand.Parameters.Add("@faceno", MySqlDbType.Int16, 25);
insertCommand.Parameters.Add("@name", MySqlDbType.VarChar, 25);
insertCommand.Parameters.Add("@img", MySqlDbType.Binary);
insertCommand.Parameters["@faceid"].Value = totalRows.ToString();
insertCommand.Parameters["@faceno"].Value = totalRows.ToString();
insertCommand.Parameters["@name"].Value = tbSetFaceLabel.Text;
insertCommand.Parameters["@img"].Value = facesToByte;
int rowAffected = insertCommand.ExecuteNonQuery();
MessageBox.Show("Data Stored Successfully in "
+ rowAffected.ToString() + " Rows");
现在,正在传递正确的图像格式,图像显示在图片框中。