Click here to Skip to main content
15,886,774 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
how to i scan for a particular 'pattern' of a picture file using c#?
I am able to scan for the header and trailer of the file using the binaryReader.
when the header has been extracted, I will compare it with the header code in the local database, and it will return the designated file extension.

Now i would like to scan for the particular pattern of the picture file's hex code.
example: '41 70 70 6c 65' will indicating the source of the picture is from an Apple Iphone.

C#
private void sourceDatabase(BinaryReader reader)
        {
            byte[] buffer = new byte[5];
            reader.BaseStream.Seek(134, SeekOrigin.Begin);
            reader.Read(buffer, 0, 5);

            string source = "";
            foreach (var item in buffer)
            {
                string temp = "0";
                if (item.ToString("X").Length == 1)
                    source += temp + item.ToString("X") + " ";
                else
                    source += item.ToString("X") + " ";
            }
            if (source.Length != 0)
                source = source.Substring(0, source.Length - 1);
            MessageBox.Show("File Source : " + source, "Result");

            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["databaseConnectionString"].ConnectionString);
            connection.Open();
            SqlCommand command = null;
            string ori_source = source;
            //int flag = 1;
            while (true)
            {
                string sql = "SELECT source FROM sourceDatabase WHERE Hexadecimal like '" + source + "%'";
                command = new SqlCommand(sql, connection);
                if (source.Length != 0 && source.Length > 6)
                {
                    source = source.Substring(0, source.Length - 1);
                    if (command.ExecuteScalar() != null)
                    {
                        string scr = System.Convert.ToString(command.ExecuteScalar().ToString());
                        //string desc = command.ExecuteScalar().ToString();
                        MessageBox.Show("Source : " + scr, "Result");
                        //flag = 2;
                        break;
                    }
                }
                else break;
            }
        }
Posted
Comments
Philippe Mori 8-Jun-15 12:42pm    
As already mentionned, the whole approach is wrong. You have to start with the file specifications or get the information from an image library that make those informations available.

1 solution

I have no idea what makes you thinking that you can identify the source of some image by capturing some specific pattern. I don't think so. And of course the expression "hex pattern" is absurd: "decimal" or "hexadecimal" is just the artifact of string representation of some numeric data in positional notation using one or another base; none of them really exist in file data, which is always binary. I also cannot believe that hexadecimal can help you to identify the source of the image. Maybe you are talking about some signature part of the metadata portion of the image file, but then it has nothing to do with "patterns".

Anyway, if you need to capture the bits of the image itself, not metadata, you are doing it wrong. You start from the position 134 of the beginning of the file, but who told you that this is where the image part starts? If you really need to image bits, you can use, for example, System.Drawing.Bitmap and System.Drawing.Bitmap.LockBits:
https://msdn.microsoft.com/en-us/library/system.drawing.bitmap%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
Comments
JasonLai1991 8-Jun-15 12:48pm    
i know it is wrong because i was just testing on the ability of the code passing to the database to compare. the example image i used has the 'pattern', in which i get from different kind of file from the same source (iPhone 5s), has the code '40 70 70 6c 65 .. .. ..' which translate to 'Apple IPhone .
is there a way to read the whole hexadecimal and scan for the 'pattern' like '40 70 70 6c 65' ?
Sergey Alexandrovich Kryukov 8-Jun-15 13:07pm    
Well, I gave you an idea, you can try it out. You still did not explain why would you think that this sequence is a pattern, but you can give it a try.
—SA
Ralf Meier 8-Jun-15 15:50pm    
The code-sequence translated to ASCII means '@pple'.
Did you realized that.
Why don't you read it as a string if there where ASCII-Chars in your "pattern" ?
Sergey Alexandrovich Kryukov 8-Jun-15 17:07pm    
:-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900