3

I need some help in converting a hex string into an image

doing some researches i came up to this code:

private byte[] HexString2Bytes(string hexString)
{
    int bytesCount = (hexString.Length) / 2;
    byte[] bytes = new byte[bytesCount];
    for (int x = 0; x < bytesCount; ++x)
    {
        bytes[x] = Convert.ToByte(hexString.Substring(x*2, 2),16);
    }

    return bytes;
}


public bool ByteArrayToFile(string _FileName, byte[] _ByteArray)
{
    try
    {
            System.IO.FileStream _FileStream = new  System.IO.FileStream(_FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
            _FileStream.Write(_ByteArray, 0, _ByteArray.Length);
            _FileStream.Close();
            return true;
     }
     catch (Exception _Exception)
     {
         MessageBox.Show(_Exception.Message);
     }

        return false;
 }

The problem is that the resulting image is almost all black and i guess i need to apply some filters to better translate the gray-scale (since the original image is in gray-scale only)

Can anyone help me?

Many thanks

ilcaste
  • 45
  • 1
  • 6
  • Wait - is the resulting binary file ok? I mean, did you detect any problem with the function you posted? – André Chalella May 22 '12 at 08:08
  • But the method you're showing just converts the string into a byte array. What did you do next, to create the image? A [Marshal.Copy](http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.copy.aspx)? – Nadir Sampaoli May 22 '12 at 08:08

1 Answers1

4

You don't need to apply any filters. I guess that the hexString variable that you are passing as input is simply a black image. The following works great for me:

class Program
{
    static void Main()
    {
        byte[] image = File.ReadAllBytes(@"c:\work\someimage.png");
        string hex = Bytes2HexString(image);

        image = HexString2Bytes(hex);
        File.WriteAllBytes("visio.png", image);
        Process.Start("visio.png");
    }

    private static byte[] HexString2Bytes(string hexString)
    {
        int bytesCount = (hexString.Length) / 2;
        byte[] bytes = new byte[bytesCount];
        for (int x = 0; x < bytesCount; ++x)
        {
            bytes[x] = Convert.ToByte(hexString.Substring(x * 2, 2), 16);
        }

        return bytes;
    }

    private static string Bytes2HexString(byte[] buffer)
    {
        var hex = new StringBuilder(buffer.Length * 2);
        foreach (byte b in buffer)
        {
            hex.AppendFormat("{0:x2}", b);
        }
        return hex.ToString();
    }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • it is not a black image since i can see something but in very low brightness and almost all black – ilcaste May 22 '12 at 08:27
  • Well, I don't know, there must be something else then, with some other part of your code that you haven't shown. The code I have posted works here 100%. I have tested it with an input image and it generated exactly the same output image. I don't see how I can help you further. – Darin Dimitrov May 22 '12 at 08:38
  • that code works for all "normal" colored images, but mine is in gray-scale only and the result is almost all black instead of many grades of grey – ilcaste May 22 '12 at 08:38
  • I see. Could you upload somewhere a sample gray-scale image so that I can reproduce the problem? – Darin Dimitrov May 22 '12 at 08:39
  • Yes, but i dont have the original image, i start from the hex code here [link](http://www.mediafire.com/?picjp59dobo35n8) and the resulting image is this [link](http://www.mediafire.com/?z4u0k088z0tsw3a) – ilcaste May 22 '12 at 08:45
  • If you don't have the original image how do you know that the original image is not corrupted or that it is not all black? How do you know that this text file that you have given doesn't actually contains an all-black image? – Darin Dimitrov May 22 '12 at 08:47
  • i know it cause this image comes directly from a database where the images are stored and if you open the management software, you can see the image perfectly clear, but i need to export these images so i need to open the database(where the images are saved in hex) and translate them – ilcaste May 22 '12 at 08:51
  • What is the original image type? Jpeg, Gif, Png, ...? It looks like a Tiff to me by looking at the headers. – Darin Dimitrov May 22 '12 at 08:58
  • It has the Tiff header signature at the beginning: `49 49 2A`. – Darin Dimitrov May 22 '12 at 09:02
  • i'm sorry, i'm not much of an image expert :) – ilcaste May 22 '12 at 09:07