我想構建一個更大的工具,它將採用.RTF格式的十六進制字符串並將它們轉儲到文件。這種從內存流寫入文件的嘗試拋出ReadTimeout和WriteTimeout類型爲「System.InvalidOperationException」的異常。我相信我有點過頭了。十六進制值不寫入圖像
,我一起工作的代碼是:
private void button_Click(object sender, RoutedEventArgs e)
{
// Image hex data
string hexImgData = @"FFD8FFE000104A46494600010200006400640000FFFFD9";
// Call function to Convert the hex data to byte array
byte[] newByte = ToByteArray(hexImgData);
MemoryStream memStream = new MemoryStream(newByte);
// Save the memorystream to file
Image image = Image.FromStream(memStream, false, false);
image.Save(@"C:\img.jpg");
memStream.Close();
image.Dispose();
}
// Function converts hex data into byte array
public static byte[] ToByteArray(String HexString)
{
int NumberChars = HexString.Length;
byte[] bytes = new byte[NumberChars/2];
for (int i = 0; i < NumberChars; i += 2)
{
bytes[i/2] = Convert.ToByte(HexString.Substring(i, 2), 16);
}
return bytes;
}
}
任何幫助,將不勝感激
它不是一個有效的JPG圖片... –