Hello,

I am working on a project which aims to compare an image to from a database of images and find out similar looking images.

I am working with a DLL file which returns unique hash for each image.

The hash is an array of 768 bytes. (byte[])

My problem is how can I store this byte[] in database and later retrive it back as a byte[]?

I have tried many methods like storing it in a varbinary and varchar format but when I look in the database, its just a long number (might be string) and there is no information about which element goes where in array if that makes sense??

SqlCommand cmd1 = new SqlCommand("INSERT INTO SIMILAR (ImageHash, ID) VALUES ((@hash), 26)", con);

byte[] hash1 = GetHash(path1, 1);
byte[] hash2 = GetHash(path2, 1);

cmd1.Parameters.AddWithValue("@hash", hash1);

Any help is greatly appreciated.

Ash

Oh and the reason why I need the values back as byte array is because the DLL has another function which takes two byte arrays to compare them.

Thanks.

An easy approach would be to separate the values in the array by a comma before putting it into the database as a comma delimited string. Then when you bring it back from the database, just do a split on the comma. I'm sure there's better/more elegant solutions, but that seems like it would work.

Hi bcasp,
Thank you very much for your quick reply.

I did thought of saving the array as Comma Seperated values and checking for commas when retrieveing, but I am sure there are more neater ways of doing this. I mean it just doesn't feel like the best way of doing this?

Is there any way I can save values in the memory location in database and retrieve as it is? Like saving whatever is in the memory, save it in database?

Hello,

Why don't you try to make that byte array to image then save it, and retrive it like and image then convert the image to a byte array.

What do you think??

As danielernesto said, store it to a column type that supports byte arrays such as Image or VarBinary(max).

// Jerry

Saving the byte[] as VarBinary(MAX) is ok. But, the problem comes when retrieving it as byte array.

I will be working with hundreds of pictures so I can't store each and every single hash as image or external file.

Is there a method to read the VarBinary values from a database as byte[]? Could you please give an example code?

SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=c:\\Test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"); 

SqlDataReader rdr = null;

SqlCommand cmd = new SqlCommand("Select ImageHash from SIMILAR WHERE ID=26", con); 

            try
            { 
                con.Open(); 
                rdr = cmd.ExecuteReader(); 
                rdr.Read(); 

                //      And there is something here?? 
                //      While reader is reading, get the values as Bytes? 

                //      byte[] temp = rdr.GetBytes(); (I couldn't figure out what should I put in the arguments)
                                                      "No overload for method 'GetBytes' takes '0' arguments"

            }

Thank you for all your help :)

You do not need to use the GetBytes. It is available if you need to do special things with that byte array, however you want the entire array as you originally placed it into the database.

SqlCommand cmd = new SqlCommand("Select ImageHash from SIMILAR WHERE ID=26", con); 
            con.Open(); 
            rdr = cmd.ExecuteReader(); 
            rdr.Read();
            byte[] temp = (byte[])rdr["ImageHash"];

// Jerry

It worked! :)
Thank you so much for your time. Everybody.
And specially Jerry, you are just awsome.

Much appreciated.

Ash

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.