Click here to Skip to main content
16,004,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have this problem regarding table management, that simply loads up client data. But my issue is that I have large images that are being displayed on the gridview making the rows enormous.

What I simply want is to add the original picture to the field `Photo` and a resized image to the field `Thumbnail` on my database.

What I have tried:

I have this code, but it simply inserts the same image on the database, instead of inserting a resized one. Any ideias?


string cb = "insert into Customer(C_ID,CustomerID,Name,Address,City,ContactNo,Email,Photo,Thumbnail) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9)";
                cc.cmd = new SqlCommand(cb);
                cc.cmd.Connection = cc.con;
                cc.cmd.Parameters.AddWithValue("@d1", txtID.Text);
                cc.cmd.Parameters.AddWithValue("@d2", txtCustomerID.Text);
                cc.cmd.Parameters.AddWithValue("@d3",txtCustomerName.Text);
                cc.cmd.Parameters.AddWithValue("@d4", txtAddress.Text);
                cc.cmd.Parameters.AddWithValue("@d5", txtCity.Text);
                cc.cmd.Parameters.AddWithValue("@d6", txtContactNo.Text);
                cc.cmd.Parameters.AddWithValue("@d7",txtEmailID.Text);
                MemoryStream ms = new MemoryStream();
                Bitmap bmpImage = new Bitmap(Picture.Image);
                bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] data = ms.GetBuffer();
                SqlParameter p = new SqlParameter("@d8", SqlDbType.Image);
                p.Value = data;
                cc.cmd.Parameters.Add(p);
                MemoryStream thumb = new MemoryStream();
                Bitmap bmpThumb = new Bitmap(Picture.Image);
                bmpImage.GetThumbnailImage(80, 80, () => false, IntPtr.Zero);
                byte[] thumbnail = thumb.GetBuffer();
                SqlParameter tp = new SqlParameter("@d9", SqlDbType.Image);
                tp.Value = thumbnail;
                cc.cmd.Parameters.Add(tp);
                cc.cmd.ExecuteReader();
                cc.con.Close();
                st1 = lblUser.Text;
                st2 = "Adicionou o cliente '" + txtCustomerName.Text + "' com o ID: '" + txtCustomerID.Text + "'";
                cf.LogFunc(st1, System.DateTime.Now, st2);
                btnSave.Enabled = false;
                MessageBox.Show("Gravado com sucesso", "Registo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
Posted
Updated 29-May-17 2:50am
v2

1 solution

Two things: firstly, if your images are large, then it's better to not store them in the DB - use a file and store the path to the file in the DB instead.

Second, your code tells it to store the large image twice because you do nothing with the thumbnail image once you have created it, just add the ms buffer twice. You need to save the new thumbnail image to the thumb stream and fetch that buffer before you set the parameter value.

Your code would also be a little more obvious if you used "proper" names fro teh SQL parameters, and AddWithValue instead of Add:
string cb = "insert into Customer(C_ID,CustomerID,Name,Address,City,ContactNo,Email,Photo,Thumbnail) VALUES (@ID,@CID,@NAM,@ADDR,@CITY,@CNO,@EML,@IMAGE,@THUMB)";
cc.cmd = new SqlCommand(cb);
...
byte[] data = ms.GetBuffer();
cc.cmd.Parameters.AddWithValue("@IMAGE", data);
...
byte[] thumbnail = thumb.GetBuffer();
cc.cmd.Parameters.AddWithValue("@THUMB", thumbnail);
...
 
Share this answer
 
Comments
Scribling Doodle 26-May-17 10:29am    
Didn't quite understand your second part. I'm a beginner and I actually cannot seem to find a way to fix this problem. Could you be more explicit?
OriginalGriff 26-May-17 10:51am    
You wrote the code to get the image data from the image, yes? So you need to do something similar to get the thumbnail data.
Scribling Doodle 26-May-17 11:30am    
That's what I did, the actual code does insert the 2 images, but the thumbnail is actually inserting as the same resolution as the normal one, instead of 120x120 like I've done in the code.
OriginalGriff 26-May-17 11:39am    
Yes, because you used the same data buffer for both instead of accessing the thumbnail data after you created it.
Look at your code: what do you do with the thumbnail image when you call GetThumbnailImage? Where do you store the thumbnail image itself?
Scribling Doodle 26-May-17 12:13pm    
I store it on the d9 field that represents the thumbnail column in my database. I have a photo column and a thumb, but only the thumb should be during the view of all the data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900