浅析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是索引格式,其调色板并不是灰度的而是伪彩,因此需要我们对其加以修改。

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

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

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

 
 
  1.   static void Main(string[] args)  
  2.   {  
  3.   byte[] bytes = new byte[10000];  
  4.   int k = 0;  
  5.  
  6.   for (int i = 0; i < 100; i++)  
  7.   {  
  8. for (int j = 0; j < 100; j++)  
  9. {  
  10. bytes[k++] = (byte)(i + j);  
  11. }  
  12.   }  
  13.  
  14.   Bitmap bmp = ToGrayBitmap(bytes, 100, 100);  
  15.  
  16.   bmp.Save(@"d:\test.png",   
  17.  
  18. System.Drawing.Imaging.ImageFormat.Png);  
  19.   }  

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

C#byte数组转化成图像 

如果没有修改过调色板,则会显示出下面的色彩斑斓的图像:

C#byte数组转化成图像 

C#byte数组转化成图像的相关内容就向你介绍到这里,希望对你了解和学习C#byte数组转化成图像有所帮助。


转自:http://developer.51cto.com/art/200908/147763.htm

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C#中,可以使用以下方法将字节数组转换为字符串: 1. 使用Encoding类的GetString方法: byte[] bytes = { 97, 98, 99, 100, 101, 102 }; string str = Encoding.ASCII.GetString(bytes); [2] 2. 使用BitConverter和Split方法进行转换: byte[] bytes = { 97, 98, 99, 100, 101, 102 }; string str = BitConverter.ToString(bytes); string[] tempArr = str.Split('-'); byte[] decBytes = new byte[tempArr.Length]; for (int i = 0; i < tempArr.Length; i++) { decBytes[i = Convert.ToByte(tempArr[i], 16); } [1] 无论使用哪种方法,都可以将C#的字节数组转换为字符串。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C# byte[]数组和string的互相转化 (四种方法)](https://blog.csdn.net/pretty_h/article/details/85297935)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C#字节数组转换成字符串浅析](https://blog.csdn.net/Joetao/article/details/4600223)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值