Reputation: 11
I want to compare an uploaded pic with all images in database , if it is matching then should display data with image , the compare is simple by converting each image to binary see this
I am using this code but its not working:
protected void CompareImages(object sender, EventArgs e)
{
if (this.FileUpload1.HasFile)
{
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
string sqlQuery = "SELECT * from [MostWanted] WHERE Photo = @FileName";
using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
{
cmd.Parameters.AddWithValue("@FileName", Path.GetFileName(this.FileUpload1.PostedFile.FileName));
conn.Open();
Byte[] bytes2 = (Byte[])cmd.ExecuteScalar();
conn.Close();
if (bytes2 != null)
{
if (bytes.Length == bytes2.Length)
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Equal Images')", true);
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Images are not equal')", true);
}
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('File Not found in the table')", true);
}
}
}
}
}
Upvotes: 1
Views: 748
Reputation: 43
data varbinary(max)
to store the image fileUpvotes: 0
Reputation: 9824
Unless those images are literally copies of the same file, a simple binary comparision will not work. Even one bit difference or a different file format will utterly break it. And that that point recognising them as the same goes into Artificial Intelligence Research.
If those files are the same down to the last byte, you could relevantly speed up the check by storing a hashvalue for every image. Compare the hash of the image you try to add with all existing image hashes to pre-filter wich ones can not be the same. You still should do a binary comparision, as even with hash values you can have wrong positives/colissions (two different inputs having the same output) happen.
As you are also planning to store a lot of images, I think the filestream atribute might help: https://www.simple-talk.com/sql/learn-sql-server/an-introduction-to-sql-server-filestream/
Upvotes: 1