private void memory_Click(object sender, EventArgs e)
        {
            if (curBitmap != null)
            {
                myTimer.ClearTimer();
                myTimer.Start();                Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
                System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat);
                IntPtr ptr = bmpData.Scan0;
                int bytes = curBitmap.Width * curBitmap.Height * 3;
                byte[] rgbValues = new byte[bytes];
                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
                double colorTemp = 0;
                for (int i = 0; i < rgbValues.Length; i += 3)
                {
                    colorTemp = rgbValues[i + 2] * 0.299 + rgbValues[i + 1] * 0.587 + rgbValues[i] * 0.114;
                    rgbValues[i] = rgbValues[i + 1] = rgbValues[i + 2] = (byte)colorTemp;
                }
                System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
                curBitmap.UnlockBits(bmpData);                myTimer.Stop();
                timeBox.Text = myTimer.Duration.ToString("####.##") + " 毫秒"; 
                Invalidate();
            }
        }
根本不能灰度化啊,问题出在哪?

解决方案 »

  1.   

    rgbValues 做了运算,但是没有设置到Bitmap上。
      

  2.   

    public static Bitmap GrayImage(Image src)
    {
        Bitmap currentBitmap = new Bitmap(src);
        BitmapData bmData = currentBitmap.LockBits(new Rectangle(0, 0, currentBitmap.Width, currentBitmap.Height),
            ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
        int stride = bmData.Stride;            
        System.IntPtr Scan0 = bmData.Scan0;
        unsafe
        {
            byte* p = (byte*)(void*)Scan0;
            int nOffset = stride - currentBitmap.Width * 3;
            byte red, green, blue;
            for (int y = 0; y < currentBitmap.Height; ++y)
            {
                for (int x = 0; x < currentBitmap.Width; ++x)
                {
                    blue = p[0];
                    green = p[1];
                    red = p[2];
                    p[0] = p[1] = p[2] = (byte)(.299 * red + .587 * green + .114 * blue);
                    p += 3;
                }
                p += nOffset;
            }
        }
        currentBitmap.UnlockBits(bmData);
        return currentBitmap;
    }测试通过
      

  3.   

    你测试过么?
    这样无法得到灰度图?
    GrayImage(Image.FromFile("图片")).Save("文件");
      

  4.   


                        Graphics g = Graphics.FromImage(bmp);
                        ImageAttributes ia = new ImageAttributes();                    float[][] colorMatrix =   {new   float[]   {0.3f,   0.3f,   0.3f,   0,   0},
                                                  new   float[]   {0.59f,   0.59f,   0.59f,   0,   0},
                                                  new   float[]   {0.11f,   0.11f,   0.11f,   0,   0},
                                                  new   float[]   {0,   0,   0,   1,   0},
                                                  new   float[]   {0,   0,   0,   0,   1}
                                              };
                        ColorMatrix cm = new ColorMatrix(colorMatrix);
                        ia.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                        g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, ia);
    在网上找了段新方式的代码可以用,不是我使用的问题.
      

  5.   

    我2楼给你代码中方法和你的类似,你没考虑我写的nOffset那块。所以你那个出来尺寸不对。
      

  6.   

    搞个灰度不需要这么复杂,直接对Bitmap的像素进行操作即可,不用跨平台调用