I stored a collection of PNG images in my MySQL database of type LONGBLOB. I am trying to retrieve them using C#. This is my first attempt of retrieving type LONGBLOB, and everything that I read didn't seem to help so far. The ones on StackOverflow relate but use different languages or different types of SQLs, so I haven't found an exact answer for this specific language and SQL version, as well as how to convert it over to System.Drawing.Image.
This is my structure:
public struct HeadshotOBJ
{
public int ID;
public System.Drawing.Image Headshot;
public int HairColorID;
public int HairLengthID;
public int HairTypeID;
public int EyeColorID;
public int GenderID;
public int SkinToneID;
public int BodyTypeID;
}
Next, I have the following to retrieve my fields:
query.Append("SELECT * FROM headshots;");
cmd = new MySqlCommand(query.ToString(), conn);
// Execute query
dr = cmd.ExecuteReader();
while (dr.Read())
{
HeadshotOBJ headshot;
headshot.ID = Convert.ToInt32(dr["id"]);
headshot.Headshot = dr["headshot"]; // Need to convert somehow to System.Drawing.Image here.
headshot.HairColorID = Convert.ToInt32(dr["hair_color_id"]);
headshot.HairLengthID = Convert.ToInt32(dr["hair_length_id"]);
headshot.HairTypeID = Convert.ToInt32(dr["hair_type_id"]);
headshot.EyeColorID = Convert.ToInt32(dr["eye_color_id"]);
headshot.GenderID = Convert.ToInt32(dr["gender_id"]);
headshot.SkinToneID = Convert.ToInt32(dr["skin_tone_id"]);
headshot.BodyTypeID = Convert.ToInt32(dr["body_type_id"]);
lstHeadShots.Add(headshot);
}
I'm certain I need to read the bytes in, but I'm lost on how to do so and how that should look in code. I'm not new to SQL, and my job used BLOBs in the past, but I never tried retrieving one before on my own, especially when storing the data into a System.Drawing.Image object.