0

I'm working on image detection on screenshot project and i have tried many ways to make my program find image and i have read many posts on many forums non of them is working.

Now I'm trying to make it with converting bitmap to hash and matching with other image hash and if there will be some similarity i would know is there my image on screenshot or not. However I have problems with converting Bitmap into hash. this is my code:

Bitmap ScreenShot = new Bitmap(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
Graphics g = Graphics.FromImage(ScreenShot);
g.CopyFromScreen(Point.Empty, Point.Empty, Screen.PrimaryScreen.WorkingArea.Size);
pictureBox1.Image = ScreenShot;
pictureBox1.Size = Screen.PrimaryScreen.WorkingArea.Size;


System.Drawing.ImageConverter ic = new System.Drawing.ImageConverter();
byte[] SS = new byte[1];
SS = (byte[])ic.ConvertTo(ScreenShot, SS.GetType());


SHA256Managed hash = new SHA256Managed();
byte[] hash1 = hash.ComputeHash(SS);

textBox1.Text = hash1.ToString();

and this is what textbox shows : System.Byte[]

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74

1 Answers1

0

"System.Byte[]" is the default string representation of any byte array. I believe you want to see your byte array as a string either for debugging purposes, easier handling or to do a string comparison.

In reality, bytes have many different ways of being represented as a string. These are called encodings. You can apply them to your byte array like this:

string resultstring = System.Text.Encoding.Default.GetString(hash1);

If you don't want to deal with encodings you can just present each byte as a char.

char[] chars = new char[hash1.Length/sizeof(char)];
System.Buffer.BlockCopy(hash1, 0, chars, 0, hash1.Length);
string resultstring = new string(chars);

However byte operations are faster than string operations, so if you are doing lots of comparisons, try keeping things in bytes (see Comparing two byte arrays in .NET)

Community
  • 1
  • 1
Ro Mathew
  • 16
  • 3