这个问题已经困惑我很久了,以前发了帖子,都没有解决。希望这次能遇到高手。注意:是256色的bmp图,谢谢大家了~~~

解决方案 »

  1.   

    FileStream fs=File.Creat(.....);
    Bitmap b=new Bitmap(fs);
    b.SetPixel(x,y,color);
    fs.Save();
      

  2.   

    到msdn查查Bitmap的文件结构,里面有个BITMAPINFOHEADER 结构体
    通过设置biBitCount把Bitmap设置成256色256色的bmp图对于超出256色范围的用系统调色板调色
      

  3.   

    呵呵,在C#里画256色的bmp图,用的是Bitmap类,但是要在unsafe的模式下(请在项目中选VS中的项目,选择最下面的项目属性,将配置属性中的允许不安全代码块设为true)
    代码如下:private void button1_Click(object sender, System.EventArgs e)
    {
        unsafe
        {
             //定义一个20×20大小的256色图,在初始的时候就是一幅全黑的(数据段全为0)图。
    Bitmap temp = new Bitmap(20,20,System.Drawing.Imaging.PixelFormat.Format8bppIndexed);         //将其放在内存中
    MemoryStream tempStream = new MemoryStream();
    temp.Save(tempStream,System.Drawing.Imaging.ImageFormat.Bmp);
    tempStream.Flush();         //这是其实256的bmp已经生成了,但我们一般要对其操作所以帮你写了以下代码:
             //这里是找到bmp的数据段,这个不用我解释了吧??自己看256的bmp格式去
    byte[] tempLocation = new byte[2];
    tempStream.Position = 10;
    tempStream.Read(tempLocation,0,2);
             //数据段在流中的位置
    int dataLocation = Convert.ToInt32(tempLocation[0])+Convert.ToInt32(tempLocation[1])*16*16;
             //将流的Position移到数据段
    tempStream.Position = dataLocation;
             //定义一个和原图相同大小的数据段,在这里是整个替换了原来的图,使其变为全
             //白(255)。你如过想对其中的某一个点进行操作可以只write一个byte,即找到Position
             //后替换一个。
    byte[] imageData = new byte[400];
             //几行几列。这里全换成白的了。
    for(int i=0;i<20;i++)
    {
       for(int j=0;j<20;j++)
      {
         imageData[i*20+j] = Convert.ToByte(255);
               }
    }
    tempStream.Write(imageData,0,400);
    tempStream.Flush();         //这里的代码也是可以用来读一个文件流中的256的bmp的,这里我就用MemoryStream了
    temp = new Bitmap(tempStream);
    this.pictureBox1.Image = temp;
             //可以保存成文件,用ACD打开,还是256的bmp。
             temp.Save("C:\\1.bmp",System.Drawing.Imaging.ImageFormat.Bmp);
    }
    }
      

  4.   

    : isaacyh(发现自己啥都不懂回头学C++) 
    写的很好啊,马上试验下,非常感谢,但是你有个地方好像不对
    byte[] tempLocation = new byte[2];
    tempStream.Position = 10;
    tempStream.Read(tempLocation,0,2);
    数据偏移是一个4bytes的吧,你怎么只用2bytes?应该是
    byte[] tempLocation = new byte[4];
    tempStream.Position = 10;
    tempStream.Read(tempLocation,0,4);
    才对吧
      

  5.   

    我直接用的这个,还是比较方便:
    byte[] tempLocation = new byte[4];
    tempStream.Position = 10;
    tempStream.Read(tempLocation,0,4);
    int dataLocation = BitConverter.ToInt32(tempLocation,0);困扰这么久的问题终于解决了,非常感谢 isaacyh(发现自己啥都不懂回头学C++) 的帮助,
    以后大家可以直接利用这个例子来做变化了。揭帖~