各位大虾,小妹最近用C#写了个小程序,按行列数循环写入一个数组,数组每个值是从0-255,得到了一个二进制文件,现在我想把这个文件在picturebox里面以灰度图(黑白的)的形式显示出来,不知道该怎么做?麻烦麻烦各位不吝赐教,小妹感激不尽~~

解决方案 »

  1.   

    Bitmap GetGrayBmp(byte[,] buf)

        
        Bitmap bmp = new Bitmap(buf,Width, buf.Length);
        for(int i=0;i<bmp.Width;i++)
            for (j = 0; j < bmp.Height; j++)
            {
                Color c=new Color();
                c.R=c.G=c.B=buf[i,j];
                bmp.SetPixel(i,j,c);
            }
        return bmp;
    }
      

  2.   

    sorry,1楼错误:Bitmap GetGrayBmp(byte[,] buf)

        
        Bitmap bmp = new Bitmap((int)buf.Rank, (int)buf.Length);
        for(int i=0;i<bmp.Width;i++)
            for (int j = 0; j < bmp.Height; j++)
            {
                Color c=Color.FromArgb(buf[i,j],buf[i,j],buf[i,j]);
                bmp.SetPixel(i,j,c);
            }
        return bmp;
    }
      

  3.   


            //一维数组(0-255)显示为灰度图
            private void button1_Click(object sender, EventArgs e)
            {
                //生成数据 
                byte[] bytes = new byte[100 * 100];
                Random rnd = new Random(DateTime.Now.Millisecond);
                for (int i = 0; i < bytes.Length; i++)
                {
                    bytes[i] = Convert.ToByte(rnd.Next(255));
                }            //填充灰度图            int k = 0;
                Bitmap bmp = new Bitmap(100, 100);
                for (int y = 1; y < 99; y++)
                {
                    for (int x = 1; x < 99; x++)
                    {
                        int colorValue = bytes[k++];
                        bmp.SetPixel(x, y, Color.FromArgb(colorValue, colorValue, colorValue));
                    }
                }            pictureBox1.Image = bmp;
            }
      

  4.   

    Bitmap bmp = new Bitmap((int)buf.GetLength(0), (int)buf.Length / (int)buf.GetLength(0));
      

  5.   

    回复mngzilin:谢谢你了,你的意思是先创建一个bitmap吧?创建后是不是把它赋值给picturebox.image呢?还是用画图的方法画进picturebox?之前我没有接触过绘图这方面的,所以不太懂,能说的详细点吗?谢谢啦~~
      

  6.   

    回复skep99:谢谢了,我的文件是用二维数组写进去的...你的代码里面  new byte[100 * 100];是什么意思?是表示一共有10000个元素吗?呵呵,我是新手,很多东西不懂
      

  7.   

     int i;            for (i = 0; i < 8729; i++)
                { 
                   
                    for (int j = 0; j < 10458; j++)
                    {
                        buffer[i, j] = bin.ReadByte();                }
                   
                } 
                if (i == 8729) MessageBox.Show("read done");
                Bitmap bmp = new Bitmap(10458, 8729);
                for (int m = 0; m < bmp.Height; m++)
                    for (int n = 0; n < bmp.Width; n++)
                    {
                        Color c = Color.FromArgb(buffer[m, n], buffer[m, n], buffer[m, n]);
                        bmp.SetPixel(n,m,c);
                    }
                
                        pictureBox1.Image = bmp;
                       
            }
    我用了mngzilin的方法,可是在picturebox里面全是黑色的...不知道怎么回事
      

  8.   


    new byte[100 * 100];//生成大小为1万的一维数组//如果二维数组
    new byte[100 ,100];////填充时用x,y去取
    int colorValue = bytes[x-1,y-1];