コードに問題があります。wbmpデータを読み取り、幅と高さを取得した後、バイアレイに変換されたデータに従ってPixelを設定します。しかし、私の最終的なpngファイルには、想定された画像のように見える画像が含まれていますが、一部のピクセルが適切に配置されていません。(幅は常に8の倍数、つまり正確なxバイトであると想定してください。したがって、パディングゼロは無視できます)
私の推測では、データの読み取り/変換時にコードが間違っているか、エラーが発生しました。
私は原因を見つけるために何時間も費やしましたが、そうすることができませんでした。あなたたちは私を助けるかもしれません。
以下は私のコードです:
using System;
using System.Drawing;
using System.IO;
using System.Collections;
class Program
{
static void Main(string[] args)
{
BinaryReader binReader = new BinaryReader(File.Open(args[0], FileMode.Open));
byte[] aa = new byte[1000];
int width, height;
try
{
//read the bytes until the end of stream
for (int i = 0; i<aa.Length; i++)
{
aa[i] = binReader.ReadByte();
}
}
catch (EndOfStreamException)
{
Console.WriteLine("End of the Stream");
}
//once end is reached,
finally
{
width = aa[2];
height = aa[3];
Console.Write("Width: " + width);
Console.Write(" Height: " + height + "\n");
//Conversion
BitArray bits = new BitArray(aa); //convert bytes array to bit array
//Conversion Ends
Bitmap image = new Bitmap(width, height);
int index = 32;
for (int r = 0; r < height; r++)
{
for (int c = 0; c < width; c++)
{
if (bits[index] == false)
{
image.SetPixel(c, r, Color.Black);
index++;
}
else
{
image.SetPixel(c, r, Color.White);
index++;
}
}
}
String n = "aa";
image.Save(n + ".png", System.Drawing.Imaging.ImageFormat.Png);
}
Console.Write("\nPress any key to continue . . .");
Console.ReadKey(true);
}
}