主题:[讨论]大家帮我看一下这段代码
我想实现这样的功能:文本中有一些二进制数据,读取这些数据,如果取到1标记为黑色,如果取到0标记为白色,然后在picturebox里显示出图像。下面是一些想法,请各位帮忙看一下
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;
fs.Close();
[em10]
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;
fs.Close();
[em10]