c#-X位图处理器不起作用

| 我最近一直在研究图像处理器,并决定查看旧的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;
    
已邀请:
我没有尝试您的代码,但这是我踢出的一个工作示例。它不解析文件,而是采用字节数组(来自Wikipedia)。它还使用普通的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);
        }
    

要回复问题请先登录注册