StealthRT
StealthRT

Reputation: 10542

c# Image to byte array then to hex in the format of 0x?

Hey all I am trying to take my image file and convert it to HEX like this site does.

However, when I use this code:

private void btnImg2ByteArray_Click(object sender, EventArgs e)
    {
        OpenFileDialog dialog = new OpenFileDialog();

        dialog.Filter = "Select Valid Image(*.png)|*.png";
        dialog.Multiselect = true;

        if (dialog.ShowDialog() == DialogResult.OK)
        {
            foreach (string fileName in dialog.FileNames)
            {
                using (var ms = new MemoryStream())
                {
                    Image imageIn = new Bitmap(fileName);
                    imageIn.Save(ms, imageIn.RawFormat); //System.Drawing.Imaging.ImageFormat.Png
                    byte[] b = ms.ToArray();
                    txtImage2byte.Text += "[" + Path.GetFileName(fileName) + "]" + Environment.NewLine;
                    StringBuilder hex = new StringBuilder(b.Length * 2);

                    foreach (byte _b in b)
                    {
                        hex.AppendFormat("{0:x2}", _b);
                    }

                    txtImage2byte.Text += hex.ToString();
                }
            }
        }
    }

I get an output like this:

89504e470d0a1a0a0000000d49484452000000180000001e0806000000362edee50000000467414d410000b18f0bfc61050000000970485973000001b1000001b1016198280e0000044949444154484bb5564928757d18bfc810ca3c44a6b0a124436241e6154916a605998a9524c5c2860529a16c88905bc6125d3296a94b1642126183cc3391e1f9eeeff9eeb9ef75cee17debfdbe5f3ddde73ee339ff67387f8506761a52999b9b93e6f73f216d2c9586ec1419191914161646b5b5b534393949d3d3d37f458881589ae06a0d2915bebebed4d8d848696969747d7d4d7f0bc4402cc4446c85979717f5f5f5d1d5d595d6448a8787073a3838a08d8d0d26f0907d07c4424cc4d62510e3f3f393363737a9a8a888fcfcfcc8d1d191dcdcdc98c043061d6c602bc66f1334343490b7b737858787535353132d2d2dd1cece0e1378c8a0830d6cc5f831417e7e3e595b5b534b4b0bbdbdbdd1fcfc3c959797537c7c3c0d0d0d69ad883e3e3ed806b6f0d1c7b709d001464646343e3e4e27272794949444c6c6c6fca4292929b4babaaab5fc05d8c207be026413a07830ece9e9a1adad2dee029cf5fafa3aeb7f027ce08b18806c82ecec6c4a4848a0d7d7578a8e8ea6a8a828babbbb63ddfbfb3b9d9e9e32811703c7055fc40024096e6f6f7902552a154d4d4d91b3b3330f0d80b6abababe3a282c0cbb5357c1103b12409e6e6e6c8d0d0909e9e9e7850525353d9092d585050c0eda9542a99c043266e4ff82206624912c011fd0d04060652757535f3171717e4e1e1c1fa95951526f09041270662402f49d0dbdb4b2e2e2e6c54515141cbcbcbcca393209f9898a091911126f090412706e4ddddddd2040b0b0b646262c2c5d207e6202626866263633920083c64d0e903be8881b69524383a3ae2552bd7e77b7b7b141919a95bc7e0211303bed02396240180e2e6e5e5c9ee16b4aeb0ecc08b011ff8c6c5c5f17fd9046ab59a5f1185fc0e586e380231e003df999919fe2f9b004f919999c9853a3c3c6499009cefd8d818ef9ddcdc5cadf45fc0163ec9c9c9bab7ff92a0b9b999eeefef59f1f8f8c83b27202080464747d9018353595949666666e4efef4fc7c7c76c0b1d6c609b9898483737372c07be2440ef666565e9bae2ecec8c0a0b0b79a08a8b8b39a1a9a9294f298ee2f9f999161717a9a4a484271e9f5df8e8e34b024ca5bbbb3be5e4e47c69d3b6b636323030d0758f959515f9f8f8f05963b1858686d2ececaca4b501490db077e08837c1821b1c1c240707070a0909215b5b5beefbd2d2521ec2f6f676d936d5872401b0bbbb4baeaeae1cd8c2c282cacacab8e73158e7e7e76cf3a7904d80a22198a7a7270d0c0c505555157f6c6a6a6a78d2b7b7b7d90eeb1adb143543615f5e5ee8f2f29275026413c009678de3e9e8e8202727270a0e0ee6fa585a5a92bdbd3ddbedefeff3b6c5dca06e586ee9e9e9ac132049203c119e1e329c3b362a64389eaeae2e4e0eacadad918d8d0df5f7f7f3a71475c1af00c4d225105fbcf074adadad5c74bcba00dcda840438aaa0a0209e5a141f1ffe888808d6492e5e1a2835a4feddd5b1bebe9e1380c7970b9d84c9eeececa4e1e1616e69e884ab23aea3980f24f81f2fbf0abb7f009773da6a104473a90000000049454e44ae426082

But what I need/want it to format as is like this:

0x00, 0x00, 0x00, 0x3f, 0xff, 0xfc, 0x5f, 0xff, 0xfa, 0x7f, 0xff, 0xfe, 0x7f, 0x00, 0xfe, 0x7e, 0x7e, 0x7e, 0x79, 0xcf, 0x9e, 0x7b, 0x0f, 0xde, 0x76, 0x7f, 0xee, 0x64, 0xff, 0xe6, 0x6d, 0xe7, 0xf6, 0x69, 0x99, 0xf6, 0x69, 0xbd, 0xf6, 0x6b, 0xbd, 0xf6, 0x6f, 0x99, 0xf6, 0x6f, 0xc3, 0xf6, 0x67, 0xff, 0xe6, 0x77, 0xff, 0xee, 0x73, 0xf3, 0xce, 0x79, 0xc7, 0x9e, 0x1c, 0x0e, 0x3e, 0x0f, 0x00, 0xfe, 0x66, 0x07, 0xfe, 0x70, 0x3f, 0xfe, 0x72, 0x7f, 0xfe, 0x76, 0x0d, 0x5e, 0x70, 0xed, 0x1e, 0x5f, 0xed, 0x5a, 0x3f, 0xef, 0xfc, 0x00, 0x00, 0x00

Not sure what I am doing incorrectly?

UPDATE 1

With the suggestions of both Suryateja KONDLA and Remove this is what it comes out to be:

0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x1e, 0x08, 0x06, 0x00, 0x00, 0x00, 0x36, 0x2e, 0xde, 0xe5, 0x00, 0x00, 0x00, 0x04, 0x67, 0x41, 0x4d, 0x41, 0x00, 0x00, 0xb1, 0x8f, 0x0b, 0xfc, 0x61, 0x05, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x01, 0xb1, 0x00, 0x00, 0x01, 0xb1, 0x01, 0x61, 0x98, 0x28, 0x0e, 0x00, 0x00, 0x04, 0x49, 0x49, 0x44, 0x41, 0x54, 0x48, 0x4b, 0xb5, 0x56, 0x49, 0x28, 0x75, 0x7d, 0x18, 0xbf, 0xc8, 0x10, 0xca, 0x3c, 0x44,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0x27, 0x27, 0x27, 0x0a, 0x0e, 0x0e, 0xe6, 0xfa, 0x58, 0x5a, 0x5a, 0x92, 0xbd, 0xbd, 0x3d, 0xdb, 0xed, 0xef, 0xef, 0xf3, 0xb6, 0xc5, 0xdc, 0xa0, 0x6e, 0x58, 0x6e, 0xe9, 0xe9, 0xe9, 0xac, 0x13, 0x20, 0x49, 0x20, 0x3c, 0x11, 0x9e, 0x1e, 0x32, 0x9c, 0x3b, 0x36, 0x2a, 0x64, 0x38, 0x9e, 0xae, 0xae, 0x2e, 0x4e, 0x0e, 0xac, 0xad, 0xad, 0x91, 0x8d, 0x8d, 0x0d, 0xf5, 0xf7, 0xf7, 0xf3, 0xa7, 0x14, 0x75, 0xc1, 0xaf, 0x00, 0xc4, 0xd2, 0x25, 0x10, 0x5f, 0xbc, 0xf0, 0x74, 0xad, 0xad, 0xad, 0x5c, 0x74, 0xbc, 0xba, 0x00, 0xdc, 0xda, 0x84, 0x04, 0x38, 0xaa, 0xa0, 0xa0, 0x20, 0x9e, 0x5a, 0x14, 0x1f, 0x1f, 0xfe, 0x88, 0x88, 0x08, 0xd6, 0x49, 0x2e, 0x5e, 0x1a, 0x28, 0x35, 0xa4, 0xfe, 0xdd, 0xd5, 0xb1, 0xbe, 0xbe, 0x9e, 0x13, 0x80, 0xc7, 0x97, 0x0b, 0x9d, 0x84, 0xc9, 0xee, 0xec, 0xec, 0xa4, 0xe1, 0xe1, 0x61, 0x6e, 0x69, 0xe8, 0x84, 0xab, 0x23, 0xae, 0xa3, 0x98, 0x0f, 0x24, 0xf8, 0x1f, 0x2f, 0xbf, 0x0a, 0xbb, 0x7f, 0x00, 0x97, 0x73, 0xda, 0x6a, 0x10, 0x44, 0x73, 0xa9, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82

Does not seem to work since its (1) longer than the original one from the website and (2) does not produce a picture when adding it to my arduino code for my oled display.

Upvotes: 0

Views: 355

Answers (3)

Charlieface
Charlieface

Reputation: 71119

As well as changing the format as mentioned by others, you don't need Image if all you want is the hex. All it's going to do is muck around converting it unnecessarily.

You can also move the StringBuilder out of the loop and continually append it, this will be more efficient.

var sb = new StringBuilder();
foreach (string fileName in dialog.FileNames)
{
    byte[] b = File.ReadAllBytes(fileName);
    sb.Append("[").Append(Path.GetFileName(fileName)).AppendLine("]");
    sb.Capacity += b.Length * 6;

    foreach (byte _b in b)
    {
        hex.AppendFormat("0x{0:x2}, ", _b);
    }
}

txtImage2byte.Text += sb.ToString();

Upvotes: 2

Suryateja KONDLA
Suryateja KONDLA

Reputation: 1576

Change the string format in your Append Format call to "0x{0:x2}, ". This includes the 0x prefix and appends a comma and a space after each byte.

private void btnImg2ByteArray_Click(object sender, EventArgs e)
{
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.Filter = "Select Valid Image(*.png)|*.png";
    dialog.Multiselect = true;

    if (dialog.ShowDialog() == DialogResult.OK)
    {
        foreach (string filename in dialog.FileNames)
        {
            using (var ms = new MemoryStream())
            {
                Image imageIn = new Bitmap(fileName);
                imageIn.Save(ms, imageIn.RawFormat); // Using the image's original format
                byte[] b = ms.ToArray();
                txtImage2byte.Text += "[" + Path.GetFileName(fileName) + "]" + Environment.NewLine;
                StringBuilder hex = new StringBuilder(b.Length * 2);

                foreach (byte _b in b)
                {
                    hex.AppendFormat("0x{0:x2}, ", _b);
                }

                // Remove the last comma and space
                //if (hex. Length > 0)
                //{
                //    hex. Length -= 2; // Remove the last comma and space
                //}

                txtImage2byte.Text += hex.ToString() + Environment.NewLine;
            }
        }
    }
}

you might want to remove the trailing comma from the last byte in the string. You can do this by trimming the string after the loop

Upvotes: 0

Remove
Remove

Reputation: 34

To format a byte into hexadecimal, you should put ‘0x’ before the byte and a comma after the byte, all within the string. The code would look like this:

hex.AppendFormat("0x{0:x2}, ", _b);

This will add the byte, _b, to the hex string in hexadecimal format preceded by ‘0x’ and followed by a comma.

Upvotes: 0

Related Questions