BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
                ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            unsafe
            {
                int* colorData = (int*)bmData.Scan0.ToPointer();
                #region 转为特征码
                for (int i = 1; i < w; i++)
                {
                    for (int j = 1; j < h; j++)
                    {
                        int index = j * b.Width + i;
                        if (colorData[index] < yansheqj)
                        {
                           icolor=colorData[index];
                        }
                    }
                }
                #endregion
            }
            b.UnlockBits(bmData);icolor的值也就是颜色.ToArgb()的值了,问题是:
icolor得到的是一个负整数,如何根据icolor得出该颜色的红色red分量呢?

解决方案 »

  1.   

    Color someColor = Color.FromArgb(icolor);
    byte  Red=someColor.R;
      

  2.   

    icolor应该转换为无符号整形
    那么 
    R=(byte)(icolor);
    G=(byte)(((word)(icolor)) >> 8)
    B=(byte)((icolor)>>16)
      

  3.   

    感谢alangsos但怎么转换成无符号整形啊??
     icolor = Convert.ToUInt32(colorData[index]);
    有异常,值对于uint32太大或者太小了
      

  4.   

    参考下面的代码private void LockUnlockBitsExample(PaintEventArgs e)
            {            // Create a new bitmap.
                Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");            // Lock the bitmap's bits.  
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                System.Drawing.Imaging.BitmapData bmpData =
                    bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                    bmp.PixelFormat);            // Get the address of the first line.
                IntPtr ptr = bmpData.Scan0;            // Declare an array to hold the bytes of the bitmap.
                int bytes  = Math.Abs(bmpData.Stride) * bmp.Height;
    ���         byte[] rgbValues = new byte[bytes];            // Copy the RGB values into the array.
                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);            // Set every third value to 255. A 24bpp bitmap will look red.  
                for (int counter = 2; counter < rgbValues.Length; counter += 3)
                    rgbValues[counter] = 255;            // Copy the RGB values back to the bitmap
                System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);            // Unlock the bits.
                bmp.UnlockBits(bmpData);            // Draw the modified image.
                e.Graphics.DrawImage(bmp, 0, 150);        }
    来自:
    http://msdn.microsoft.com/en-us/library/system.drawing.imaging.bitmapdata.scan0.aspx
      

  5.   

    或者你试一试int* colorData = (int*)bmData.Scan0.ToPointer();到uint* colorData = (uint*)bmData.Scan0.ToPointer();