首页 > 代码库 > 以Lockbits的方式访问bitmap

以Lockbits的方式访问bitmap

用Bitmap.GetPixel和Bitmap.SetPixel访问像素点实在是太慢了,必须要用LockBits的方式访问内存才能改善,这里贴一个快速访问Bitmap每个像素点的包装类,是国外一个老外写的,感觉很好用。

    public class LockBitmap    {        Bitmap source = null;        IntPtr Iptr = IntPtr.Zero;        BitmapData bitmapData = http://www.mamicode.com/null;"Only 8, 24 and 32 bpp images are supported.");                }                // Lock bitmap and return bitmap data                bitmapData = http://www.mamicode.com/source.LockBits(rect, ImageLockMode.ReadWrite,"x"></param>        /// <param name="y"></param>        /// <returns></returns>        public Color GetPixel(int x, int y)        {            Color clr = Color.Empty;            // Get color components count            int cCount = Depth / 8;            // Get start index of the specified pixel            int i = ((y * Width) + x) * cCount;            if (i > Pixels.Length - cCount)                throw new IndexOutOfRangeException();            if (Depth == 32) // For 32 bpp get Red, Green, Blue and Alpha            {                byte b = Pixels[i];                byte g = Pixels[i + 1];                byte r = Pixels[i + 2];                byte a = Pixels[i + 3]; // a                clr = Color.FromArgb(a, r, g, b);            }            if (Depth == 24) // For 24 bpp get Red, Green and Blue            {                byte b = Pixels[i];                byte g = Pixels[i + 1];                byte r = Pixels[i + 2];                clr = Color.FromArgb(r, g, b);            }            if (Depth == 8)            // For 8 bpp get color value (Red, Green and Blue values are the same)            {                byte c = Pixels[i];                clr = Color.FromArgb(c, c, c);            }            return clr;        }        /// <summary>        /// Set the color of the specified pixel        /// </summary>        /// <param name="x"></param>        /// <param name="y"></param>        /// <param name="color"></param>        public void SetPixel(int x, int y, Color color)        {            // Get color components count            int cCount = Depth / 8;            // Get start index of the specified pixel            int i = ((y * Width) + x) * cCount;            if (Depth == 32) // For 32 bpp set Red, Green, Blue and Alpha            {                Pixels[i] = color.B;                Pixels[i + 1] = color.G;                Pixels[i + 2] = color.R;                Pixels[i + 3] = color.A;            }            if (Depth == 24) // For 24 bpp set Red, Green and Blue            {                Pixels[i] = color.B;                Pixels[i + 1] = color.G;                Pixels[i + 2] = color.R;            }            if (Depth == 8)            // For 8 bpp set color value (Red, Green and Blue values are the same)            {                Pixels[i] = color.B;            }        }        public bool IsValidCoordinate(int x, int y)        {            return x >= 0 && x < this.Width && y > 0 && y < this.Height;        }    }
还有一个时间测算的类,可以计算代码的时间差,跟Stopwatch差不多
    public class Benchmark    {        private static DateTime startDate = DateTime.MinValue;        private static DateTime endDate = DateTime.MinValue;        public static TimeSpan Span { get { return endDate.Subtract(startDate); } }        public static void Start() { startDate = DateTime.Now; }        public static void End() { endDate = DateTime.Now; }        public static double GetSeconds()        {            if (endDate == DateTime.MinValue) return 0.0;            else return Span.TotalSeconds;        }    }

以Lockbits的方式访问bitmap