Using C# to restore hex binary data to image?

Is there a way to restore hex binary data to original .BMP image

Thanks.



Answer this question

Using C# to restore hex binary data to image?

  • Siebe Tolsma

    There is a large hex binary digits stored in the XML file, I'm trying to find out the way to restore the image by using those digits. Any idea
  • Tamizhan

    Um.... A BMP is hex binary data to start with. What has happened to your data that you need undone

  • IanG

    Hi, James

    Thank you for your solution. But I'm wondaring how to convert the array of byte to actual image or there is no way to do it by using C#


  • katokay


    byte[] byBitmap = HexStringToBytes(str);
    MemoryStream ms = new MemoryStream(byBitmap);
    Bitmap bmp = new Bitmap(ms);

    Of course, you could compress that down to one line:

    Bitmap bmp = new Bitmap(new MemoryStream(HexStringToBytes(str)));



  • LAPM

    First of all, a number can be represented as Hex digits, or as Binary digits, but it cannot be both at the same time.

    Basically, what you have is a string containing hex digits.

    I don't know if this is the best way, but it should work:


    byte[] HexStringToBytes(string hex)
    {
    byte[] data = new byte[hex.Length /2];
    int j = 0;
    for (int i = 0; i < hex.Length; i+=2)
    {
    data[ j ] = Convert.ToByte(hex.Substring(x, 2), 16);
    ++j;
    }
    return data;
    }



  • Using C# to restore hex binary data to image?