本帖最后由 u012160576 于 2013-09-24 22:04:24 编辑

解决方案 »

  1.   

    代码是这样:Bitmap source = new Bitmap(pictureBox1.Image); BitmapData sourceData = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);IntPtr source_scan = sourceData.Scan0; 
                       
            unsafe
                        { 
                            byte* p = (byte*)source_scan.ToPointer(); 
                           
                    for (int width = 0; width <= source.Width; ++width) 
                       { 
                      for (int height = 0; height <= sourceData.Height; ++height)
                                {                          int r, g, b;
                              r = p[2];
                              g = p[1];
                              b = p[0];                  p[0] = p[1] = p[2] = (byte)(.33 * r + .33 * g + .33 * b);                                p += 3;
                                }
                                p += sourceData.Stride - (sourceData.Width * 3);
                            } 
                        } 
                        source.UnlockBits(sourceData);
                        pictureBox1.Image=source;
      

  2.   

    PixelFormat.Format32bppArgb改为PixelFormat.Format24bpprgb
      

  3.   


    但我写成PixelFormat.Format24bpprgb的时候
     R = P [2];这边就有问题
      

  4.   


    unsafe
                {
                    byte* p = (byte*)source_scan.ToPointer();
                    for (int height = 0; height < sourceData.Height; ++height)      //  <= 改成<
                    {
                        for (int width = 0; width < sourceData.Width; ++width)      //  <= 改成<
                        {
                            int r, g, b;
                            r = p[2];
                            g = p[1];
                            b = p[0];                        p[0] = p[1] = p[2] = (byte)(.33 * r + .33 * g + .33 * b);                        p += 3;
                        }
                        p += sourceData.Stride - (sourceData.Width * 3);
                    }
                } 两个for循环调换位置, for循环中的判断没有=号 你这代码看的我伤心。 
      

  5.   

     //  <= 改成<    你没看到吗
      

  6.   

    32bppArgbfor (int height = 0; height < sourceData.Height; height++)
    {
        for (int width = 0; width < source.Width; width++)
        {
             int r, g, b;
             r = p[2];
             g = p[1];
             b = p[0];
             p[0] = p[1] = p[2] = (byte)(.33 * r + .33 * g + .33 * b);
             p += 4;
        }
        p += sourceData.Stride - (sourceData.Width * 4);
    }24bppRgbfor (int height = 0; height < sourceData.Height; height++)
    {
        for (int width = 0; width < source.Width; width++)
        {
             int r, g, b;
             r = p[2];
             g = p[1];
             b = p[0];
             p[0] = p[1] = p[2] = (byte)(.33 * r + .33 * g + .33 * b);
             p += 3;
        }
        p += sourceData.Stride - (sourceData.Width * 3);
    }
      

  7.   

    这样也可以Bitmap source = new Bitmap(pictureBox1.Image);
    BitmapData sourceData = source.LockBits(new Rectangle(0, 0, source.Width,
    source.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
    IntPtr ptr = sourceData.Scan0;
    byte[] buffer = new byte[sourceData.Stride * sourceData.Height];
    System.Runtime.InteropServices.Marshal.Copy(ptr, buffer, 0, buffer.Length);
    //32位+4  24位+3
    for (int i = 0; i < buffer.Length; i += 4)
    {
        byte b = buffer[i+0];
        byte g = buffer[i+1];
        byte r = buffer[i+2];
        buffer[i+0] = buffer[i+1] = buffer[i+2] = (byte)(0.33 * b + 0.33 * g + 0.33 * r);
    }
    System.Runtime.InteropServices.Marshal.Copy(buffer, 0, ptr, buffer.Length);
    source.UnlockBits(sourceData);
    pictureBox1.Image = source;