X位图处理器不工作
本文关键字:工作 处理器 位图 | 更新日期: 2023-09-27 17:50:19
我最近一直在研究一个图像处理器,决定研究一下旧的XBitmap文件格式(XBM)。我正在用c#将这些文件中的像素数据复制到位图中。我已经设法设置和算法,似乎在理论上工作,但不能正确显示。对于不了解的人来说,XBM是一个ACSII C文件,它包含一个字节数组并显示单色数据。下面是我的算法,所以如果有人能给我指出正确的方向,我会很高兴。
对不起,这有点逐字,但它是整个解码器:
string input = File.ReadAllText(fname);
using (StreamReader r = new StreamReader(fname))
{
int i = input.IndexOf('{');
string bytes = input.Substring(i+1);
int j = bytes.IndexOf('}');
bytes = bytes.Remove(j-1);
string[] StringArray = bytes.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
byte[] pixels = new byte[StringArray.Length];
for (int k = 0; k < StringArray.Length; k++)
{
byte result;
StringArray[k] = StringArray[k].Replace("0x", "");
StringArray[k] = StringArray[k].Replace("'r", "");
StringArray[k] = StringArray[k].Replace("'n", "");
StringArray[k] = StringArray[k].Trim();
bool result1 = byte.TryParse(StringArray[k], NumberStyles.HexNumber, CultureInfo.CurrentCulture, out result);
if (result1)
pixels[k] = result;
else
throw new Exception();
}
xBits = System.Runtime.InteropServices.Marshal.AllocHGlobal(pixels.Length);
System.Runtime.InteropServices.Marshal.Copy(pixels, 0, xBits, pixels.Length);
}
return xBits;
我没有尝试您的代码,但这里有一个我踢出的工作示例。它不解析文件,而是取一个字节数组(来自维基百科)。它还使用普通的GDI+调用创建位图。
//Sample data from http://en.wikipedia.org/wiki/X_BitMap
int Width = 16;
int Height = 7;
byte[] test_bits = { 0x13, 0x0, 0x15, 0x0, 0x93, 0xcd, 0x55, 0xa5, 0x93, 0xc5, 0x0, 0x80, 0x0, 0x60 };
//Create our bitmap
using (Bitmap B = new Bitmap(Width, Height))
{
//Will hold our byte as a string of bits
string Bits = null;
//Current X,Y of the painting process
int X = 0;
int Y = 0;
//Loop through all of the bits
for (int i = 0; i < test_bits.Length; i++)
{
//Convert the current byte to a string of bits and pad with extra 0's if needed
Bits = Convert.ToString(test_bits[i], 2).PadLeft(8, '0');
//Bits are stored with the first pixel in the least signigicant bit so we need to work the string from right to left
for (int j = 7; j >=0; j--)
{
//Set the pixel's color based on whether the current bit is a 0 or 1
B.SetPixel(X, Y, Bits[j] == '0' ? Color.White : Color.Black);
//Incremement our X position
X += 1;
}
//If we're passed the right boundry, reset the X and move the Y to the next line
if (X >= Width)
{
X = 0;
Y += 1;
}
}
//Output the bitmap to the desktop
B.Save(System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop), "Output.bmp"), System.Drawing.Imaging.ImageFormat.Bmp);
}