C# lockbits遍历图像

  通过图像遍历,将pictureBox 中的 灰度bitmap变量转变为一维数组,方便进一步的图像处理。

    public static byte[] ChangeByte(Bitmap tp)  
    {  
        int w = tp.Width;   // 图像实际宽度   
        int h = tp.Height;  // 图像实际高度  
      
        BitmapData srcdata = tp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly,  
          PixelFormat.Format24bppRgb);     // 将图像锁入内存,只读,图像格式为rgb格式  
      
        byte[] pixeData = new byte[h * w]; // 声明一个与图像等大的数组,因为每个像素点的(灰度)值  
                                           // 范围为 0 - 255  所以使用byte数组  
        int sride = srcdata.Stride;    // 获取图像的系统宽度(字节数)  
        unsafe    // C#使用指针时需要在unsafe中使用,  
        {    // C#默认不支持unsafe,使用时在项目 -》选项  -》生成中设置  
            byte* temp = (byte*)srcdata.Scan0.ToPointer(); // 获取图像锁入内存的首地址  
      
            for (int i = 0; i < h; i++)  
            {  
                for (int j = 0; j < w; j++)  
                {  
                    pixeData[i * w + j] = temp[0];  // 我们只获取第一个色彩的值,对于灰度图像  
                                                    // red blue green 像素值相等  
                    temp += 3;                      // 跳过其余两色  
                }  
                temp += sride - w * 3;              // 加上系统对齐的宽度   
            }  
        }  
      
        tp.UnlockBits(srcdata);                     // 解锁  
        return pixeData;  
      
    }  

bitmage lockbits方法介绍

C# lockbits操作指针处理图像

来源:http://blog.csdn.net/onlyou930/article/details/5361086

加支付宝好友偷能量挖...


评论(0)网络
阅读(135)喜欢(0)Asp.Net/C#/WCF