【问题标题】:C# How to Check If Picturebox image is already exist in DatabaseC#如何检查Picturebox图像是否已存在于数据库中
【发布时间】:2019-02-20 10:24:57
【问题描述】:

在这些代码下方,我试图验证我的图片框图像是否已存在于数据库中。更准确地说,如果用户尝试插入相同的图像,它将验证“图像已经存在”

这是我得到的错误:

(参数化查询'(@Image varbinary(8000))Select COUNT(*) from employee_product wh' 需要参数 '@Image',它不是 提供。')

我在这里做错了什么?还是我忘记了什么? 我希望有人能帮助我。谢谢

public partial class ADDProduct : MetroForm
{
    SIMSProduct _view;
    public ADDProduct(SIMSProduct _view)
    {
        InitializeComponent();
        this._view = _view;                 
    }
    DataTable dt = new DataTable();
    byte[] photobyte;
    string date = DateTime.Now.ToString("MMMM-dd-yyyy");
    public void ValidateImage(byte[] image)
    {
        using (var con = SQLConnection.GetConnection())
        {
                using (var select = new SqlCommand("Select COUNT(*) from employee_product where Image= @Image", con))
                {

                select.Parameters.Add("@Image", SqlDbType.VarBinary).Value = photobyte;
                    using (var sda = new SqlDataAdapter(select))
                    {
                        int count = (int)select.ExecuteScalar();
                        if (count > 0)
                        {
                            lbl_image.Show();
                        }
                    }
                }
        }          
    }
 private void btn_add_Click(object sender, EventArgs e)
    {
        _view.ID = txt_id.Text;
        using (var con = SQLConnection.GetConnection())
        {
            if (string.IsNullOrEmpty(cbox_supplier.Text) || string.IsNullOrEmpty(txt_code.Text) || string.IsNullOrEmpty(txt_item.Text) || string.IsNullOrEmpty(txt_quantity.Text) || string.IsNullOrEmpty(txt_cost.Text) || pictureBox1.Image == null )
            {
                CustomNotifcation.Show("Please input the required fields", CustomNotifcation.AlertType.warning);
            }
            else
            {
                ValidateItem.IsValidItem(txt_code, lbl_code);
                ValidateImage(photobyte);
                if (lbl_code.Visible == true)
                {
                    CustomNotifcation.Show("CODE ALREADY EXIST", CustomNotifcation.AlertType.error);
                    lbl_code.Visible = false;
                }
                else if (lbl_image.Visible == true)
                {
                    CustomNotifcation.Show("IMAGE ALREADY EXIST", CustomNotifcation.AlertType.error);
                    lbl_image.Visible = false;
                }
                else
                {
                    using (var select = new SqlCommand("Insert into employee_product (Image, ID, Supplier, Codeitem, Itemdescription, Date, Quantity, Unitcost) Values (@Image,@ID, @Supplier, @Codeitem, @Itemdescription, @Date, @Quantity, @Unitcost)", con))
                    {
                        var ms = new MemoryStream();
                        pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
                        photobyte = ms.GetBuffer();

                        select.Parameters.Add("@Image", SqlDbType.VarBinary).Value = photobyte;
                        select.Parameters.Add("@ID", SqlDbType.VarChar).Value = txt_id.Text;
                        select.Parameters.Add("@Supplier", SqlDbType.VarChar).Value = cbox_supplier.Text;
                        select.Parameters.Add("@Codeitem", SqlDbType.VarChar).Value = txt_code.Text.Trim();
                        select.Parameters.Add("@Itemdescription", SqlDbType.VarChar).Value = txt_item.Text.Trim();
                        select.Parameters.Add("@Date", SqlDbType.VarChar).Value = date;
                        select.Parameters.Add("@Quantity", SqlDbType.Int).Value = txt_quantity.Text.Trim();
                        select.Parameters.Add("@Unitcost", SqlDbType.Int).Value = txt_cost.Text.Trim();
                        select.ExecuteNonQuery();
                        CustomMessage.Show("Message: Item successfully added!", CustomMessage.Messagetype.Success);
                        pictureBox1.Image = null;
                        cbox_supplier.Items.Clear();
                        txt_code.Clear();
                        txt_item.Clear();
                        txt_quantity.Clear();
                        txt_cost.Clear();
                        _view.btn_update.Enabled = false;
                        _view.AddingProduct();
                        this.Close();
                    }
                }                
            }                
        }
    }
    private void pictureBox1_Click(object sender, EventArgs e)
    {
        using (OpenFileDialog ofd = new OpenFileDialog())
        {
            ofd.Filter = "Image Files (*.jpg;*.jpeg;.*.png;)|*.jpg;*.jpeg;.*.png;";
            ofd.FilterIndex = 1;
            ofd.Multiselect = false;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Image = Image.FromFile(ofd.FileName);

            }
        }
    }
 }

【问题讨论】:

  • 这是一个非常糟糕的设计,用于在数据库中搜索二进制文件以确保二进制内容是否存在于表中。当您将图像插入数据库中的单独字段时,存储二进制 blob 的哈希(如 MD5),然后当您要检查图像是否存在时,只需获取图像的 MD5 哈希并查询数据库即可。
  • 抛开 SQL 问题:你会发现像这样进行二进制比较只有在图像确实相同的情况下才有效。重新编码 jpg,添加标签等将使它们不同。然而,完美的解决方案并非易事..
  • @我仍然对此感到困惑,但谢谢
  • 这不是小事。远非微不足道。即使内容大体相同。比较编码的二进制数据根本没有意义。看看这里stackoverflow.com/questions/5730631/image-similarity-comparison。无论如何,您可以开始尝试特征匹配方法:www.emgu.com/wiki/index.php/FAST_feature_detector_in_CSharp
  • 我们今天早些时候回答了这个问题,你得到了同样的答案

标签: c# winforms sql-server-2008


【解决方案1】:

您可以为文件生成一些唯一值,例如哈希或校验和,并将其与字节流一起存储在数据库中,可用于检查文件是否存在。通常,这些机制不用于此目的。这仅适用于文件内容完全相同的情况。即使是最轻微的变化也无法识别匹配。

你可以找到类似字符串散列的散列:

using(SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
{
    hash = Convert.ToBase64String(sha1.ComputeHash(byteArray));
}

或者,您也可以决定像我们通常那样存储一些备用信息。像文件名或基于用户的验证来检查文件是否存在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    相关资源
    最近更新 更多