简体   繁体   English

如何将 BLOB 转换为图像

[英]How to convert BLOB to Image

I have a problem in displaying the picture which is stored in my MySQL database.我在显示存储在我的 MySQL 数据库中的图片时遇到问题。 I don't know if I have stored it successfully but using this function which converts image to a blob file, here is the function:我不知道我是否已成功存储它,但使用此函数将图像转换为 blob 文件,这是函数:

private byte[] imageToByteArray(Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();
}

When I check my database, it says BLOB with blue highlight.当我检查我的数据库时,它显示带有蓝色突出显示的 BLOB。 Now, I would like to display the image in my picturebox.现在,我想在我的图片框中显示图像。 I have also a function to convert byte array to image..我还有一个将字节数组转换为图像的函数。

private Image byteArrayToImage(byte[] byteArrayIn)
{
    MemoryStream ms = new MemoryStream(byteArrayIn);
    ms.Position = 0;
    Image returnImage = Image.FromStream(ms);
    return returnImage;
}

When I run the application, it says:当我运行应用程序时,它说:

ArgumentException was unheld, Parameter is not valid ArgumentException 未被保留,参数无效

I have tried using this syntax:我试过使用这种语法:

pictureBox1.Image = byteArrayToImage(dr["img"] as byte[]);

Or I'm thinking if I should convert BLOB to Byte Array first?或者我在想是否应该先将 BLOB 转换为字节数组? then use the function to convert the byte array into Image?然后使用函数将字节数组转换为Image?
when I click the name, it should display the information, unfortunately, I'm receiving the argument exception..当我单击名称时,它应该显示信息,不幸的是,我收到了参数异常..

int i = dataGridView1.SelectedCells[0].RowIndex;
string firstname = dataGridView1.Rows[i].Cells[0].Value.ToString();
string lastname = dataGridView1.Rows[i].Cells[1].Value.ToString();

Connection connect = new Connection();
MySqlConnection mySqlConnect = new MySqlConnection(connect.connString());
mySqlConnect.Open();

string s = "SELECT * FROM tbl_contacts WHERE username = '" + label1.Text + "' and (fname = '" + firstname + "' and lname = '" + lastname + "')";

MySqlCommand mySQL = new MySqlCommand(s, mySqlConnect);
mySQL.ExecuteNonQuery();
MySqlDataReader dr = mySQL.ExecuteReader();

if (dr.HasRows)
{
    dr.Read();
    txtFname.Text = dr["fname"].ToString();
    txtLname.Text = dr["lname"].ToString();
    txtBday.Text = dr["birthday"].ToString();
    txtEmail.Text = dr["email"].ToString();
    txtMobile.Text = dr["mobile"].ToString();
    txtAddress.Text = dr["address"].ToString();
    txtNotes.Text = dr["notes"].ToString();
    pictureBox1.Image = byteArrayToImage(dr["img"] as byte[]);
}

尝试这样的事情:

byteArrayToImage(dr.GetSqlBytes(dr.GetOrdinal("img")).Buffer);

ArgumentException was unheld, Parameter is not valid is due to corrupt image. ArgumentException was unheld, Parameter is not valid是由于图像损坏。 Your image did not save correctly.您的图像没有正确保存。 Save byte[] you have received in some file in the disk and try to rename it as .jpg or .png and try to open.将您收到的byte[]保存在磁盘中的某个文件中,并尝试将其重命名为.jpg.png并尝试打开。

Corruption of binary data happens due to various reasons, Length of binary data wasn't saved or trimmed.由于各种原因,二进制数据的损坏发生,二进制数据的Length没有被保存或修剪。 If your database is configured to store 5kb someone wrote code to trim data to 5kb instead of raising exception or appending buffer wasn't coded correctly resulting in missing last few bytes.如果您的数据库配置为存储5kb有人编写代码将数据修剪为5kb而不是引发异常或附加缓冲区未正确编码导致丢失最后几个字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM