2014-10-29 104 views
-1

我一直在考慮包含十六進制數據,我知道形式JPEG圖像的文本文件。以下是格式示例:如何轉換十六進制的文本文件到jpeg

FF D8 FF E0 00 10 4A 46 49 46 00 01 02 00 00 64 00 64 00 00 FF E1 00 B8 45 78 69 00 00 4D 

這只是一個片段,但您明白了。

有誰知道我可以轉換這回原始JPEG?

回答

0

從十六進制字符串轉換爲使用Convert.ToByte用鹼16參數字節。

字節數組轉換爲Bitmap你把它放在一個流,並使用Bitmap(Stream)構造:

using System.IO; 
//.. 

string hexstring = File.ReadAllText(yourInputFile); 
byte[] bytes = new byte[hexstring.Length/2]; 
for (int i = 0; i < hexstring.Length; i += 2) 
    bytes[i/2] = Convert.ToByte(hexstring.Substring(i, 2), 16); 
using (MemoryStream ms = new MemoryStream(bytes)) 
{ 
    Bitmap bmp = new Bitmap(ms); 
    // now you can do this: 
    bmp.Save(yourOutputfile, System.Drawing.Imaging.ImageFormat.Jpeg); 
    bmp.Dispose(); // dispose of the Bitmap when you are done with it! 
    // or that: 
    pictureBox1.Image = bmp; // Don't dispose as long as the PictureBox needs it! 
} 

我想有更多的LINQish方式,但只要它的工作原理..