C#訪問BitMap幾種方法效率比較

http://blog.csdn.net/yunque100/article/details/5839534

           bmpGray = new Bitmap("validCode.bmp");
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < bmpGray.Height; i++)
            {
                for (int j = 0; j < bmpGray.Width; j++)
                {
                    Color color = bmpGray.GetPixel(j, i);
                    int aver = (color.R + color.G + color.B) / 3;
                    bmpGray.SetPixel(j, i, Color.FromArgb(aver, aver, aver));
                }
            }
            stopwatch.Stop();

            Stopwatch stopwatch2 = new Stopwatch();
            stopwatch2.Start();
            bmpGray = new Bitmap("validCode.bmp");
            Rectangle rect = new Rectangle(1, 1, bmpGray.Width - 2, bmpGray.Height - 2);
            BitmapData bmpData = bmpGray.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            IntPtr ptr = bmpData.Scan0;
            for (int i = 0; i < rect.Height; i++)
            {
                for (int j = 0; j < rect.Width; j++)
                {
                    int aver = 0;
                    for (int k = 0; k < 3; k++)
                    {
                        aver += Marshal.ReadByte(ptr, i * bmpData.Stride + j * 3 + k);
                    }
                    aver /= 3;
                    for (int k = 0; k < 3; k++)
                    {
                        Marshal.WriteByte(ptr, i * bmpData.Stride + j * 3 + k, (byte)aver);
                    }
                }
            }
            bmpGray.UnlockBits(bmpData);
            stopwatch2.Stop();

            Stopwatch stopwatch3 = new Stopwatch();
            stopwatch3.Start();
            bmpGray = new Bitmap("validCode.bmp");
            Rectangle rect2 = new Rectangle(1, 1, bmpGray.Width - 2, bmpGray.Height - 2);
            BitmapData bmpData2 = bmpGray.LockBits(rect2, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            IntPtr ptr2 = bmpData2.Scan0;
            byte[] rgbValues = new byte[rect2.Height * rect2.Width * 3];
            Marshal.Copy(ptr2, rgbValues, 0, rgbValues.Length);
            for (int i = 0; i < rgbValues.Length; i += 3)
            {
                int colorTemp = rgbValues[i] + rgbValues[i + 1] + rgbValues[i + 2];
                colorTemp /= 3;
                rgbValues[i] = rgbValues[i + 1] = rgbValues[i + 2] = (byte)colorTemp;
            }
            Marshal.Copy(rgbValues, 0, ptr2, rgbValues.Length);
            bmpGray.UnlockBits(bmpData);
            stopwatch3.Stop();

            Stopwatch stopwatch4 = new Stopwatch();
            stopwatch4.Start();
            bmpGray = new Bitmap("validCode.bmp");
            Rectangle rect4 = new Rectangle(1, 1, bmpGray.Width - 2, bmpGray.Height - 2);
            BitmapData bmpData4 = bmpGray.LockBits(rect4, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            unsafe
            {
                byte* ptr4 = (byte*)(bmpData4.Scan0);
                for (int i = 0; i < rect4.Height; i++)
                {
                    for (int j = 0; j < rect4.Width; j++)
                    {
                        int aver = 0;
                        for (int k = 0; k < 3; k++)
                        {
                            aver += ptr4[i * bmpData4.Stride + j * 3 + k];
                        }
                        aver /= 3;
                        for (int k = 0; k < 3; k++)
                        {
                            ptr4[i * bmpData4.Stride + j * 3 + k] = (byte)aver;
                        }
                    }
                }
            }
            bmpGray.UnlockBits(bmpData4);
            stopwatch4.Stop();


你可能感兴趣的:(C#,byte)