Getting black image after loading it from DB

323
March 25, 2017, at 02:45 AM

After quite some time of trying and looking arround here for some answers, i am unable to load an image from MySQL.

Basicly, a user choses a picture from and OpenFileDialog which is then loaded in a PictureBox (this works fine).
Then the user clicks a button which will save the picture in the db by converting it to a byte[].
Finally, when I try to load the picture in another PictureBox, it's all black.

When adding the picture to the db:

public void PictureToDB(Image img)
{
    MemoryStream tmpStream = new MemoryStream();
    img.Save(tmpStream, ImageFormat.Png);
    tmpStream.Seek(0, SeekOrigin.Begin);
    byte[] imgBytes = new byte[5000];
    tmpStream.Read(imgBytes, 0, 5000);
    string query = "Update TABLE Set IMG = @imgBytes";
    MySqlParameter param = new MySqlParameter("@img", imgBytes);
    //Skipping connection to db and all... it works
}

Getting picture from db:

public void DBToPicture()
{
    string query = "Select IMG From TABLE Where ...";
    //Skipping Command lines ...
    MySqlDataReader reader = command.ExecuteReader();
    DataTable myDT.Load(reader);
    Byte[] data = new Byte[0];
    data = (Byte[])(myDT.Rows[0]["PHOTOLOISANT"]);
    MemoryStream mem = new MemoryStream(data);
    MyPictureBox.Image = Image.FromStream(mem);
}

More info:

  • the type in MySQL is varbinary(8000) and contains this "89504e470d0a1a0a0000000d4948445200000280000001e00802000000bab34bb3000000017352474200aece1ce90000000467414d41002e2e2e" after the update
  • the image is converted to bitmap when it is loaded in the first PictureBox.
Answer 1

For storing:

conn = new MySqlConnection("server=" + hostname + ";uid=" + username + ";pwd=" + password + ";database=databaseimage;Charset=latin1;");
            conn.Open();
            FileStream fs;
            Byte[] bindata;
            MySqlParameter picpara;
            cmd = new MySqlCommand("INSERT INTO mypic (pic) VALUES(?pic)", conn);
            picpara = cmd.Parameters.Add("?pic", MySqlDbType.MediumBlob);
            cmd.Prepare();
//txtPicPath is the path of the image, e.g. C:\MyPic.png
            fs = new FileStream(txtPicPath.Text, FileMode.Open, FileAccess.Read);
            bindata = new byte[Convert.ToInt32(fs.Length)];
            fs.Read(bindata, 0, Convert.ToInt32(fs.Length));
            fs.Close();
            picpara.Value = bindata;
            cmd.ExecuteNonQuery();

To retrieve it:

if (conn == null) // Just to make sure that the connection was not severed
        {

                conn = new MySqlConnection("server=" + hostname + ";uid=" + username + ";pwd=" + password + ";database=databaseimage;Charset=latin1;");
                conn.Open();
        }
        MemoryStream ms = new MemoryStream();
        FileStream fs;
        Byte[] bindata;
        cmd = new MySqlCommand("SELECT pic FROM mypic WHERE id=3", conn);
        bindata = (byte[])(cmd.ExecuteScalar());

        ms.Write(bindata, 0, bindata.Length);
        pb2.Image = new Bitmap(ms);
        fs = new FileStream(name, FileMode.Create, FileAccess.Write);
        ms.WriteTo(fs);

I will use these kinda steps to store and retrieve image from MySql.

Sincerely,

Thiyagu Rajendran

**Please mark the replies as answers if they help and unmark if they don't.

Rent Charter Buses Company
READ ALSO
Mysql php database query

Mysql php database query

I'm stuck into something that maybe is pretty simple but not getting out of itI do a query into the database with:

338
CSV upload on front end of wordpress site (PHP/SQL)

CSV upload on front end of wordpress site (PHP/SQL)

I've made a lot of progress on this in some areas but struggling in othersHere's the objective: Existing wordpress site is being used by client

374
“unknown column in field list” error when passing custom string into MariaDB/MySQL C connector API function

“unknown column in field list” error when passing custom string into MariaDB/MySQL C connector API function

I am trying to make a function which requests the user to enter a new username and password, which then get stored in a MySQL/MariaDB databaseI used strcat_s() and strcpy_s() to concatenate strings together to then get passed as a MySQL/MariaDB query in the C API

348
How do I set a column to be unique, but only under a condition?

How do I set a column to be unique, but only under a condition?

I have a table, which has an ID, a column "A" and other columnsIf A is set to "this", there can't be another ID with it's column A set to "this"

231