C# byte数组转化位Bitmap

来源:互联网 发布:qq音乐网站源码 编辑:程序博客网 时间:2024/06/05 18:00

转自:浅析C#byte数组转化成图像的实现

C# byte数组转换为8bit灰度图像的问题类似的文章在网上可以看到不少,但多多少少都存在一些问题。这两天做实验室的项目用到这个功能,我从头把它整理了一遍。在看代码之前,首先解释几个问题。

1、byte数组存放的是图像每个像素的灰度值,byte类型正好是从0~255,存放8bit灰度图像的时候,一个数组元素就是一个像素的灰度值。仅有这个数组还不足以恢复出原来的图像,还必须事先知道图像的长、宽值;

2、创建Bitmap类的时候必须指定PixelFormat为Format8bppIndexed,这样才最符合图像本身的特性;

3、Bitmap类虽然提供了GetPixel()、SetPixel()这样的方法,但我们绝对不能用这两个方法来进行大规模的像素读写,因为它们的性能实在很囧;

4、托管代码中,能不用unsafe就尽量不用。在.NET 2.0中已经提供了BitmapData类及其LockBits()、UnLockBits()操作,能够安全地进行内存读写;

5、图像的width和它存储时的stride是不一样的。位图的扫描线宽度一定是4的倍数,因此图像在内存中的大小并不是它的显示大小;

6、Format8bppIndexed类型的PixelFormat是索引格式,其调色板并不是灰度的而是伪彩,因此需要我们对其加以修改。

代码如下,解说写在注释里了:

    /// <summary>      /// 将一个字节数组转换为8bit灰度位图      /// </summary>      /// <param name="rawValues">显示字节数组</param>      /// <param name="width">图像宽度</param>      /// <param name="height">图像高度</param>      /// <returns>位图</returns>      public static Bitmap ToGrayBitmap(byte[] rawValues, int width, int height)      {      //// 申请目标位图的变量,并将其内存区域锁定      Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);      BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height),       ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);           //// 获取图像参数      int stride = bmpData.Stride;  // 扫描线的宽度      int offset = stride - width;  // 显示宽度与扫描线宽度的间隙      IntPtr iptr = bmpData.Scan0;  // 获取bmpData的内存起始位置      int scanBytes = stride * height;// 用stride宽度,表示这是内存区域的大小           //// 下面把原始的显示大小字节数组转换为内存中实际存放的字节数组      int posScan = 0, posReal = 0;// 分别设置两个位置指针,指向源数组和目标数组      byte[] pixelValues = new byte[scanBytes];  //为目标数组分配内存             for (int x = 0; x < height; x++)      {       //// 下面的循环节是模拟行扫描       for (int y = 0; y < width; y++)       {       pixelValues[posScan++] = rawValues[posReal++];       }       posScan += offset;  //行扫描结束,要将目标位置指针移过那段“间隙”      }             //// 用Marshal的Copy方法,将刚才得到的内存字节数组复制到BitmapData中      System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, iptr, scanBytes);      bmp.UnlockBits(bmpData);  // 解锁内存区域             //// 下面的代码是为了修改生成位图的索引表,从伪彩修改为灰度      ColorPalette tempPalette;      using (Bitmap tempBmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))      {       tempPalette = tempBmp.Palette;      }      for (int i = 0; i < 256; i++)      {       tempPalette.Entries[i] = Color.FromArgb(i, i, i);      }           bmp.Palette = tempPalette;             //// 算法到此结束,返回结果      return bmp;      }  


下面是我用来测试的代码片段:

 static void Main(string[] args)    {    byte[] bytes = new byte[10000];    int k = 0;     for (int i = 0; i < 100; i++)    {  for (int j = 0; j < 100; j++)  {  bytes[k++] = (byte)(i + j);  }    }     Bitmap bmp = ToGrayBitmap(bytes, 100, 100);     bmp.Save(@"d:\test.png",    System.Drawing.Imaging.ImageFormat.Png);    }