I have a problem with my code. After read wbmp data, get width & height, I setPixel according to the data converted into the biarray. But my final png file contains a picture that looks like a supposed picture but some pixels are not properly places. (Please assume the width is always multiples of 8, ie exact x byte, so the padding zeroes can be disregarded)
My guess is my code is wrong or some error occurred when I read/convert the data.
I have spent hours to find the cause but couldnt do so. You guys might help me out.
Followings are my code:
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);
}
}