Storing an image in a blob.

Storing an image in a blob.

Postby stan » Thu 04 Oct 2007 15:31

I am trying to save a selected Jpeg image file's contents to a
blob field called fldPicture. The following is the code.
The update works somewhat but the actual byte array data is not
stored in the blob. All I get is "System.Byte[]"
What do I need to do to store the contents of the byte[] to
the blob field in database?
Thanks
Stan
// Save Jpeg image to blob (fldPicture)
CoreLab.MySql.MySqlConnection myConnection = new CoreLab.MySql.MySqlConnection(commonConstants.myConnectionString);
myConnection.Open();
Image bmp = Bitmap.FromFile(myDi.FileName);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
string myCommandString = "UPDATE dbvolunteer.tblvolunteer SET fldPicture = '"
+ ms.GetBuffer() + "' WHERE fldNumberKey = '" + volKey + "'";
CoreLab.MySql.MySqlDataAdapter myAdapter = new CoreLab.MySql.MySqlDataAdapter(myCommandString, myConnection);
myAdapter.Fill(myDs, "dbvolunteer.tblvolunteer");
myAdapter.Dispose();
myDs.Dispose();
myConnection.Close();
stan
 
Posts: 3
Joined: Thu 04 Oct 2007 15:13

Postby Alexey » Fri 05 Oct 2007 10:06

Please take a look at the following code:
Code: Select all
            MySqlConnection myConnection = new MySqlConnection(commonConstants.myConnectionString);
            myConnection.Open();
            Image bmp = Bitmap.FromFile(myDi.FileName);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            string myCommandString = "insert into dbvolunteer.tblvolunteer values(:param)";
            MySqlCommand cmd = new MySqlCommand(myCommandString, myConnection);
            cmd.Parameters.Add("param", MySqlType.Blob);
            cmd.Parameters[0].Value = ms.GetBuffer();
            cmd.ExecuteNonQuery();
            myConnection.Close();

Also examine our Pictures demo project.
Alexey
 
Posts: 2756
Joined: Mon 13 Mar 2006 07:43

Now I need the rest of the story

Postby stan » Thu 11 Oct 2007 21:34

Alexey

Thank you for your help on storing a Jpeg image into a blob field.
The following code that you have given me works fine.
Now I need to get the blob picture and store it into a picture box.
See my code for this. I have looked a your pictures example but it
deals with databinding an grids. My project doesn't do that.
I also looked at the dataaccessquickstart example but can't seem to
generate some code that works although is was helpfull in geting your
example working.


Could you give me an example for reading (SELECT) a blob from the database
into a picture box that is similar to your new (INSERT) code that you have already
given me.


// Store new record with a picture in a blob. (THIS WORKS)
OpenFileDialog myDi = new OpenFileDialog();
myDi.Filter = "Volunteer Photos (*.jpg;)|*.jpg;";
if (myDi.ShowDialog() == DialogResult.OK)
{
if (myDi.FileName != "")
{
picSecurity.Image = System.Drawing.Image.FromFile(myDi.FileName);
lblPhotoFilePath.Text = myDi.FileName;
lblPhotoFilePath.Text = string.Concat("@", lblPhotoFilePath.Text);
// Save image in database
MySqlConnection myConnection = new MySqlConnection(commonConstants.myConnectionString);
DataSet myDataSet = new DataSet();
myConnection.Open();
Image bmp = Bitmap.FromFile(myDi.FileName);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
string myCommandString = "INSERT dbvolunteer.tblpicture (fldVolKey,fldPicture) "
+ "values(:volunteer,:picture)";
MySqlCommand cmd = new MySqlCommand(myCommandString, myConnection);
cmd.Parameters.Add("volunteer", MySqlType.Int);
cmd.Parameters.Add("picture", MySqlType.Blob);
cmd.Parameters[0].Value = volKey;
cmd.Parameters[1].Value = ms.GetBuffer();
cmd.ExecuteNonQuery();
myConnection.Close();

}
}

This is my 25th attempt to get a picture from a blob to a picture box.
I have been on the NET and have look through your examples. I know it
is very simple but I just can't get it.

// Get image from database. (THIS NEEDS SOME WORK pointer problem)
MySqlConnection myConnection1 = new MySqlConnection(commonConstants.myConnectionString);
DataSet myDataSet = new DataSet();
myConnection1.Open();
myDataTable.SelectCommand = new
MySqlCommand ("SELECT fldPicture FROM dbvolunteer.tblpicture WHERE "
+ "fldVolKey = '" + volKey + "'");
myDataTable.Active = true;
myRow = myDataTable.Rows[0];
Byte[] img = (Byte[])myRow[myDataTable.Columns["fldPicture"]];
System.IO.MemoryStream ms = new System.IO.MemoryStream();
int offset = 0;
ms.Read(img, offset, img.Length - offset);
Image bmp = Bitmap.FromStream(ms);
picSecurity.Image = bmp;

I also need to know how to update and existing blob with a new image.
This is not as critical since I can delete and insert new if I have to.

Thank you so very much for your help.

Stan
stan
 
Posts: 3
Joined: Thu 04 Oct 2007 15:13

Postby Alexey » Fri 12 Oct 2007 06:49

Change your code this way:
Code: Select all
MySqlConnection myConnection1 = new MySqlConnection(commonConstants.myConnectionString);
DataSet myDataSet = new DataSet();
myConnection1.Open();
myDataTable.SelectCommand = new MySqlCommand ("SELECT fldPicture FROM dbvolunteer.tblpicture WHERE " + "fldVolKey = '" + volKey + "'");
myDataTable.Active = true;
myRow = myDataTable.Rows[0];
Byte[] img = (Byte[])myRow[myDataTable.Columns["fldPicture"]];
picSecurity.Image = Bitmap.FromStream(new MemoryStream(img));
Alexey
 
Posts: 2756
Joined: Mon 13 Mar 2006 07:43

Parameter is not valid.

Postby stan » Fri 12 Oct 2007 14:23

Alexey
Thank you for your patience. The last line of the code is:

picSecurity.Image = Bitmap.FromStream( new MemoryStream(img));

This line returns a exception "Parameter is not valid"

I know that the image is in the fldPicture blob and I get a row count of 1 from the select command.

When I hightlite img in the debug mode it has a dimension of 22. Is there a piece of code missing?

Stan
stan
 
Posts: 3
Joined: Thu 04 Oct 2007 15:13

Postby Alexey » Mon 15 Oct 2007 07:28

Please send me your project including the definition of your own database objects.
Use e-mail address provided in the Readme file.
Do not use third party components.
Alexey
 
Posts: 2756
Joined: Mon 13 Mar 2006 07:43


Return to dotConnect for MySQL