C#中将byte数组转换为8bit灰度图像

本文详细介绍了如何使用C#将给定的字节数组转换为8位灰度位图,并通过修改位图的索引表将其从伪彩色转换为灰度。文中提供了完整的代码实现,包括如何正确处理内存读写、调整图像尺寸和转换颜色格式等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

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

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

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

代码如下,解说写在注释里了:
  1. 1        /// <summary>
  2. 2        /// 将一个字节数组转换为8bit灰度位图
  3. 3        /// </summary>
  4. 4        /// <param name="rawValues">显示字节数组</param>
  5. 5        /// <param name="width">图像宽度</param>
  6. 6        /// <param name="height">图像高度</param>
  7. 7        /// <returns>位图</returns>
  8. 8        public static Bitmap ToGrayBitmap(byte[] rawValues, int width, int height)
  9. 9        {
  10. 10            //// 申请目标位图的变量,并将其内存区域锁定
  11. 11            Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
  12. 12            BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height),
  13. 13                ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
  14. 14 
  15. 15            //// 获取图像参数
  16. 16            int stride = bmpData.Stride;  // 扫描线的宽度
  17. 17            int offset = stride - width;  // 显示宽度与扫描线宽度的间隙
  18. 18            IntPtr iptr = bmpData.Scan0;  // 获取bmpData的内存起始位置
  19. 19            int scanBytes = stride * height;  // 用stride宽度,表示这是内存区域的大小
  20. 20 
  21. 21            //// 下面把原始的显示大小字节数组转换为内存中实际存放的字节数组
  22. 22            int posScan = 0, posReal = 0;  // 分别设置两个位置指针,指向源数组和目标数组
  23. 23            byte[] pixelValues = new byte[scanBytes];  //为目标数组分配内存
  24. 24 
  25. 25            for (int x = 0; x < height; x++)
  26. 26            {
  27. 27                //// 下面的循环节是模拟行扫描
  28. 28                for (int y = 0; y < width; y++)
  29. 29                {
  30. 30                    pixelValues[posScan++] = rawValues[posReal++];
  31. 31                }
  32. 32                posScan += offset;  //行扫描结束,要将目标位置指针移过那段“间隙”
  33. 33            }
  34. 34 
  35. 35            //// 用Marshal的Copy方法,将刚才得到的内存字节数组复制到BitmapData中
  36. 36            System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, iptr, scanBytes);
  37. 37            bmp.UnlockBits(bmpData);  // 解锁内存区域
  38. 38 
  39. 39            //// 下面的代码是为了修改生成位图的索引表,从伪彩修改为灰度
  40. 40            ColorPalette tempPalette;
  41. 41            using (Bitmap tempBmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
  42. 42            {
  43. 43                tempPalette = tempBmp.Palette;
  44. 44            }
  45. 45            for (int i = 0; i < 256; i++)
  46. 46            {
  47. 47                tempPalette.Entries = Color.FromArgb(i, i, i);
  48. 48            }
  49. 49 
  50. 50            bmp.Palette = tempPalette;
  51. 51 
  52. 52            //// 算法到此结束,返回结果
  53. 53            return bmp;
  54. 54        }
复制代码
下面是我用来 测试 的代码片段:
  1.         static void Main(string[] args)
  2.         {
  3.             byte[] bytes = new byte[10000];
  4.             int k = 0;

  5.             for (int i = 0; i < 100; i++)
  6.             {
  7.                 for (int j = 0; j < 100; j++)
  8.                 {
  9.                     bytes[k++] = (byte)(i + j);
  10.                 }
  11.             }

  12.             Bitmap bmp = ToGrayBitmap(bytes, 100, 100);

  13.             bmp.Save(@"d:\test.png", System.Drawing.Imaging.ImageFormat.Png);
  14.         }
复制代码

结果应该显示成下面的样子:


现在网上的例子多数都是24位图像转24位灰度图的,即使有转8位灰度的,也是通过GetPixel(转换速度太慢)或者指针方式(一来C#并不推荐这种模式,二来我也没有学过c++之类的对指针实在不熟悉)来实现的,而没有c#推荐的数组方式的.

因为bmp在内存中储存的时候,每行都是必须是4的倍数才可以,开始忘记处理这个间隙,结果导致有的图片可以正常转换,有的图片就发生图像扭曲.

处理完这个后,又发现输出的都是一些带颜色的色点组成的图像,不是灰度图,经过查资料才知道输出的是伪彩色,要转成灰度才可以.

全部代码如下

[c-sharp:nogutter]  view plain copy
  1. /// <summary>  
  2. /// 灰度处理(BitmapData类)  
  3. /// </summary>  
  4. /// <returns>输出8位灰度图片</returns>  
  5. public static Bitmap 灰度处理(Bitmap 图像)  
  6. {  
  7.     Bitmap bmp = new Bitmap(图像.Width, 图像.Height, PixelFormat.Format8bppIndexed);  
  8.   
  9.     //设定实例BitmapData相关信息  
  10.     Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);  
  11.   
  12.     BitmapData data = 图像.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);  
  13.     //锁定bmp到系统内存中  
  14.     BitmapData data2 = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);  
  15.   
  16.     //获取位图中第一个像素数据的地址  
  17.     IntPtr ptr = data.Scan0;  
  18.     IntPtr ptr2 = data2.Scan0;  
  19.   
  20.     int numBytes = data.Stride * data.Height;        
  21.     int numBytes2 = data2.Stride * data2.Height;  
  22.   
  23.     int n2 = data2.Stride - bmp.Width; //// 显示宽度与扫描线宽度的间隙  
  24.   
  25.     byte[] rgbValues = new byte[numBytes];  
  26.     byte[] rgbValues2 = new byte[numBytes2];  
  27.     //将bmp数据Copy到申明的数组中  
  28.     Marshal.Copy(ptr, rgbValues, 0, numBytes);  
  29.     Marshal.Copy(ptr2, rgbValues2, 0, numBytes2);  
  30.   
  31.     int n = 0;  
  32.   
  33.     for (int y = 0; y < bmp.Height; y++)  
  34.     {  
  35.         for (int x = 0; x < bmp.Width * 3; x += 3)  
  36.         {  
  37.             int i = data.Stride * y + x;  
  38.   
  39.             double value = rgbValues[i + 2] * 0.299 + rgbValues[i + 1] * 0.587 + rgbValues[i] * 0.114; //计算灰度  
  40.   
  41.             rgbValues2[n] = (byte)value;  
  42.   
  43.             n++;  
  44.         }  
  45.         n += n2; //跳过差值  
  46.     }  
  47.   
  48.     //将数据Copy到内存指针  
  49.     Marshal.Copy(rgbValues, 0, ptr, numBytes);  
  50.     Marshal.Copy(rgbValues2, 0, ptr2, numBytes2);  
  51.   
  52.     //// 下面的代码是为了修改生成位图的索引表,从伪彩修改为灰度  
  53.     ColorPalette tempPalette;  
  54.     using (Bitmap tempBmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))  
  55.     {  
  56.         tempPalette = tempBmp.Palette;  
  57.     }  
  58.     for (int i = 0; i < 256; i++)  
  59.     {  
  60.         tempPalette.Entries[i] = Color.FromArgb(i, i, i);  
  61.     }  
  62.   
  63.     bmp.Palette = tempPalette;  
  64.   
  65.   
  66.     //从系统内存解锁bmp  
  67.     图像.UnlockBits(data);  
  68.     bmp.UnlockBits(data2);  
  69.   
  70.     return bmp;  

http://blog.youkuaiyun.com/slzlren/article/details/4318400
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值