您的足迹:首页 > .NET >检测图片是否是空白页,GetPixel高效处理

检测图片是否是空白页,GetPixel高效处理


处理判断图片是否空白页:


  /// <summary>
        /// 检查图片是否空白图片
        /// </summary>
        /// <param name="img">Image</param>
        /// <returns>是否空白图片</returns>
        public static bool CheckTransparentImg(Bitmap bitMap)
        {
            try
            {
                bool blnIsTransparent = false;
                //图片总像素
                int intAll = bitMap.Height * bitMap.Width;
                int intBrightnessCount = 0;

                PointBitmap lockbmp = new PointBitmap(bitMap);
                lockbmp.LockBits();
                //按像素遍历
                for (int intY = 0; intY < bitMap.Height; intY++)
                {
                    for (int intX = 0; intX < bitMap.Width; intX++)
                    {
                        //获取颜色
                        Color color = lockbmp.GetPixel(intX, intY);
                        //Color color = bitMap.GetPixel(intX, intY);
                        float dBrightness = color.GetBrightness();

                        //亮度小于0.2认为不透明
                        if (dBrightness < 0.2)
                        {
                            intBrightnessCount++;
                        }
                    }
                }
                lockbmp.UnlockBits();
                //释放资源
                //bitMap.Dispose();
                //只需要把数量为200以下的,基本能把噪点去掉
                if (intBrightnessCount <= 200)
                {
                    blnIsTransparent = true;
                }

                return blnIsTransparent;
            }
            catch (Exception ex)
            {
                log4netHelper.Error("---报错方法--CheckTransparentImg");
                log4netHelper.Error(ex.Message);
                log4netHelper.Error(ex.StackTrace);
                log4netHelper.Error("---end---");
            }
            return false;
        }



非安全代码获取GetPixel,处理一张A4纸图片的效率大约是0.5S


    public class PointBitmap
    {
        Bitmap source = null;
        IntPtr Iptr = IntPtr.Zero;
        BitmapData bitmapData = null;

        public int Depth { get; private set; }
        public int Width { get; private set; }
        public int Height { get; private set; }

        public PointBitmap(Bitmap source)
        {
            this.source = source;
        }

        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
                Rectangle rect = new 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);

                //得到首地址
                unsafe
                {
                    Iptr = bitmapData.Scan0;
                    //二维图像循环

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UnlockBits()
        {
            try
            {
                source.UnlockBits(bitmapData);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Color GetPixel(int x, int y)
        {
            unsafe
            {
                byte* ptr = (byte*)Iptr;
                ptr = ptr + bitmapData.Stride * y;
                ptr += Depth * x / 8;
                Color c = Color.Empty;
                if (Depth == 32)
                {
                    int a = ptr[3];
                    int r = ptr[2];
                    int g = ptr[1];
                    int b = ptr[0];
                    c = Color.FromArgb(a, r, g, b);
                }
                else if (Depth == 24)
                {
                    int r = ptr[2];
                    int g = ptr[1];
                    int b = ptr[0];
                    c = Color.FromArgb(r, g, b);
                }
                else if (Depth == 8)
                {
                    int r = ptr[0];
                    c = Color.FromArgb(r, r, r);
                }
                return c;
            }
        }

        public void SetPixel(int x, int y, Color c)
        {
            unsafe
            {
                byte* ptr = (byte*)Iptr;
                ptr = ptr + bitmapData.Stride * y;
                ptr += Depth * x / 8;
                if (Depth == 32)
                {
                    ptr[3] = c.A;
                    ptr[2] = c.R;
                    ptr[1] = c.G;
                    ptr[0] = c.B;
                }
                else if (Depth == 24)
                {
                    ptr[2] = c.R;
                    ptr[1] = c.G;
                    ptr[0] = c.B;
                }
                else if (Depth == 8)
                {
                    ptr[2] = c.R;
                    ptr[1] = c.G;
                    ptr[0] = c.B;
                }
            }
        }
    }



本博客所有文章如无特别注明均为原创。作者:cc复制或转载请以超链接形式注明转自 .NET程序员之路
原文地址《检测图片是否是空白页,GetPixel高效处理

相关推荐

发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(0)