我这里有一个用C#编写的PCX解码器,它被设计成返回一个IntPtr,它指向一个未压缩字节数组(PCX文件使用RLE压缩,但是我的解码器应该能够处理这个问题)。我已经从文件中读取了宽度、尺寸和调色板,该图像将仅将文件呈现为大多数图像的位图,但有些不能正确呈现。图像在那里,颜色也在那里,但是实际的位图看起来像是被对角切割了4或5次,并重新排列。我检查了图像中的飞机数量,bpp也没问题。
我想是我的代码出了问题,所以如果有人看到错误,请告诉我好吗?
编辑2:
正如古法所指出的,我没有处理任何填充物。有人能为我指出正确的方向吗?
代码(对不起,这里有很多代码,但它是实际的像素处理器):
IntPtr pBits;
Boolean bRepeat;
Int32 RepeatCount;
Byte ReadByte;
Int32 Row = 0;
Int32 Col = 0;
Byte[] PCXData = new Byte[BytesPerScanline * ScanLines]; //BytesPerScanline * ScanLines);
BinaryReader r = new BinaryReader(file);
r.BaseStream.Seek(128, SeekOrigin.Begin);
while (Row < ScanLines)
{
ReadByte = r.ReadByte();
bRepeat = (0xc0 == (ReadByte & 0xC0));
RepeatCount = (ReadByte & 0x3f);
if (!(Col >= BytesPerScanline))
{
if (bRepeat)
{
ReadByte = r.ReadByte();
while (RepeatCount > 0)
{
PCXData[(Row * BytesPerScanline) + Col] = ReadByte;
RepeatCount -= 1;
Col += 1;
}
}
else
{
PCXData[(Row * BytesPerScanline) + Col] = ReadByte;
Col += 1;
}
}
if (Col >= BytesPerScanline)
{
Col = 0;
Row += 1;
}
}
pBits = System.Runtime.InteropServices.Marshal.AllocHGlobal(PCXData.Length);
System.Runtime.InteropServices.Marshal.Copy(PCXData, 0, pBits, PCXData.Length);
return pBits;发布于 2011-04-15 17:00:05
首先,您没有正确地处理您的非托管资源(例如BinaryReader)。或者在使用完r.Dispose()之后调用它,或者将它封装在一个使用块中,如下所示:
using(BinaryReader r = new BinaryReader(file))
{
...
}并且始终对实现IDisposable的任何对象执行此操作。
https://stackoverflow.com/questions/5679845
复制相似问题