用C#实现:从文件中读取二进制数据,将1标记为黑色,将0标记为白色,并打印出图像。请教实现的方法

来源:百度知道 编辑:UC知道 时间:2024/06/06 05:12:06

FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "data.txt",FileMode.OpenOrCreate,FileAccess.Read);

Bitmap bmp = new Bitmap(1, (int)fs.Length * 8);

int h = 0;

for (int i = 0; i < fs.Length; i++)
{
int b = fs.ReadByte();

int c = 1;

for (int j = 0; j < 8; j++)
{
if ((b & c) != 0)
{
bmp.SetPixel(0, h, Color.Black);
}
else
{
bmp.SetPixel(0, h, Color.White);
}
c <<= 1;
h++;
}
}

this.pictureBox1.Image = (Image)bmp;