如何在设置参数以将图像添加到SQL数据库后创建缩略图? [英] How do I create a thumbnail after setting up the parameter to add an image to SQL database?

查看:63
本文介绍了如何在设置参数以将图像添加到SQL数据库后创建缩略图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我有关于表管理的问题,它只是加载客户端数据。但我的问题是我在gridview上显示的大图像使得行很大。



我只想要将原始图片添加到字段中照片和调整后的图像到我的数据库中的字段缩略图。



我尝试过:



我有这个代码,但它只是在数据库上插入相同的图像,而不是插入一个调整大小的图像。任何想法?





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);
            }

推荐答案

两件事:首先,如果您的图片很大,那么最好不要将它们存储在数据库中 - 使用文件并将路径存储到DB中的文件中。



其次,您的代码告诉它将大图像存储两次,因为您没有对缩略图一旦创建它,​​只需添加ms缓冲区两次。您需要将新缩略图图像保存到 thumb 流并在设置参数值之前获取该缓冲区。



如果您从SQL参数中使用正确名称,并且使用AddWithValue而不是Add,那么您的代码也会更加明显:

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


这篇关于如何在设置参数以将图像添加到SQL数据库后创建缩略图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆