简体   繁体   中英

How to convert BLOB to Image

I have a problem in displaying the picture which is stored in my MySQL database. 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:

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. 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

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? then use the function to convert the byte array into 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. 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.

Corruption of binary data happens due to various reasons, Length of binary data wasn't saved or trimmed. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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