Using C# to restore hex binary data to image?  
Author Message
MikeDai





PostPosted: Visual C# General, Using C# to restore hex binary data to image? Top

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

Thanks.



Visual C#7  
 
 
James Curran





PostPosted: Visual C# General, Using C# to restore hex binary data to image? Top

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

 
 
MikeDai





PostPosted: Visual C# General, Using C# to restore hex binary data to image? Top

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
 
 
James Curran





PostPosted: Visual C# General, Using C# to restore hex binary data to image? Top

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;
}



 
 
MikeDai





PostPosted: Visual C# General, Using C# to restore hex binary data to image? Top

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#


 
 
James Curran





PostPosted: Visual C# General, Using C# to restore hex binary data to image? Top


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)));