博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
以Lockbits的方式访问bitmap
阅读量:6908 次
发布时间:2019-06-27

本文共 5432 字,大约阅读时间需要 18 分钟。

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

public class LockBitmap    {        Bitmap source = null;        IntPtr Iptr = IntPtr.Zero;        BitmapData bitmapData = null;        public byte[] Pixels { get; set; }        public int Depth { get; private set; }        public int Width { get; private set; }        public int Height { get; private set; }        public LockBitmap(Bitmap source)        {            this.source = source;        }        ///         /// Lock bitmap data        ///         public void LockBits()        {            try            {                // Get width and height of bitmap                Width = source.Width;                Height = source.Height;                // get total locked pixels count                int PixelCount = Width * Height;                // Create rectangle to lock                System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, Width, Height);                // get source bitmap pixel format size                Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);                // Check if bpp (Bits Per Pixel) is 8, 24, or 32                if (Depth != 8 && Depth != 24 && Depth != 32)                {                    throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");                }                // Lock bitmap and return bitmap data                bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,                                             source.PixelFormat);                // create byte array to copy pixel values                int step = Depth / 8;                Pixels = new byte[PixelCount * step];                Iptr = bitmapData.Scan0;                // Copy data from pointer to array                Marshal.Copy(Iptr, Pixels, 0, Pixels.Length);            }            catch (Exception ex)            {                throw ex;            }        }        ///         /// Unlock bitmap data        ///         public void UnlockBits()        {            try            {                // Copy data from byte array to pointer                Marshal.Copy(Pixels, 0, Iptr, Pixels.Length);                // Unlock bitmap data                source.UnlockBits(bitmapData);            }            catch (Exception ex)            {                throw ex;            }        }        ///         /// Get the color of the specified pixel        ///         ///         ///         /// 
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; } /// /// Set the color of the specified pixel /// /// /// /// 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;        }    }

转载于:https://www.cnblogs.com/xiashengwang/p/4225848.html

你可能感兴趣的文章
sublime多行文档注释插件
查看>>
uestc 1425 Another LCIS 线段树 区间合并
查看>>
为Exchange Server 申请有效期超过2年的证书
查看>>
POJ 2686 Traveling by Stagecoach 状态DP
查看>>
HTML meta viewport属性说明(mark)
查看>>
openstack rdo git install icehouse
查看>>
Windows 2008 R2 单网卡搭建***实战设置
查看>>
mongodb3.2复制集和shard集群搭建
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
Java学习笔记1-初始化的顺序
查看>>
开源Xen是如何衰落的?
查看>>
Pivot Table系列之切片器 (Slicer)
查看>>
windows下安装mysql5.6及基本命令
查看>>
jsp的九个内置对象简介
查看>>
用户如何获得***服务---步骤与效果
查看>>
学习沟通技巧--- SOFTEN法则与SOLER法则
查看>>
用户密码重设对EFS的影响
查看>>
基于mdrill的大数据分析
查看>>
我的友情链接
查看>>