[英]c# hex string to byte image and filtering
我需要一些幫助將十六進制字符串轉換為圖像
做一些研究,我想到了這段代碼:
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;
}
問題在於生成的圖像幾乎全是黑色,我想我需要應用一些濾鏡以更好地轉換灰度(因為原始圖像僅是灰度的)
誰能幫我?
非常感謝
您不需要應用任何過濾器。 我猜您作為輸入傳遞的hexString
變量只是一個黑色圖像。 以下對我有用:
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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.