从PictureBox获取指定颜色的所有像素

时间:2014-09-19 19:33:36

标签: c# winforms picturebox

我想在C#中获取指定颜色的所有像素(来自ARGB或来自Color类的任何像素)。我的第一个想法是将图片框内容绘制到Bitmap,然后使用for循环,使用b.GetPixel(x, y);迭代像素。但是,我使用的PictureBox是巨大的,因此需要花费数百万/数千万次迭代来检查这一点,这对性能太不友好了。有人有比这更好的解决方案吗?感谢

1 个答案:

答案 0 :(得分:0)

我为System.Drawing.Image编写了扩展方法,迭代图像中的每个像素,并返回一个包含每个x / y坐标的List作为System.Drawing.Point。
您可以搜索rgb值或rgba值 我的方法是使用指针,因此您需要为项目启用不安全的代码 与GetPixel(...)相比,使用指针迭代位图的速度要快许多倍 在此博客条目中,GetPixel(...)声称比基于指针的解决方案慢1000多倍。
http://davidthomasbernal.com/blog/2008/03/14/c-image-processing-performance-unsafe-vs-safe-code-part-ii/
您应该在使用之前正确测试此代码,但即使某些内容不正确,显示的技术也可以帮助您入门!

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;

public static class Pixelcounter
{
    private struct PixeldataARGB
    {
        public byte b;
        public byte g;
        public byte r;
        public byte a;
    }

    private struct PixeldataRGB
    {
        public byte b;
        public byte g;
        public byte r;
    }

    public static IEnumerable<Point> CountOccurences(this Image @this, int r, int g, int b)
    {
        if (r < 0 || g < 0 || b < 0)
            throw new ArgumentException("color values must not be negative");
        if (r > 255 || g > 255 || b > 255)
            throw new ArgumentException("color values must be below 256");

        return CountOccurences(@this, (byte)r, (byte)g, (byte)b);
    }

    public static unsafe IEnumerable<Point> CountOccurences(this Image @this, byte r, byte g, byte b)
    {
        Bitmap bitmap = new Bitmap(@this);
        BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
        PixeldataRGB* pointer = (PixeldataRGB*)bitmapData.Scan0;

        List<Point> pointList = new List<Point>();

        for (int y = 0; y < bitmap.Height; y++)
        {
            for (int x = 0; x < bitmap.Width; x++)
            {
                PixeldataRGB current = *pointer;
                if (current.r == r && current.g == g && current.b == b)
                    pointList.Add(new Point(x, y));
                pointer++;
            }
        }

        bitmap.Dispose();

        return pointList;
    }

    public static IEnumerable<Point> CountOccurences(this Image @this, int r, int g, int b, int a)
    {
        if (r < 0 || g < 0 || b < 0 || a < 0)
            throw new ArgumentException("color and alpha values must not be negative");
        if (r > 255 || g > 255 || b > 255 || a > 255)
            throw new ArgumentException("color and alpha values must be below 256");

        return CountOccurences(@this, (byte)r, (byte)g, (byte)b, (byte)a);
    }

    public static unsafe IEnumerable<Point> CountOccurences(this Image @this, byte r, byte g, byte b, byte a)
    {
        Bitmap bitmap = new Bitmap(@this);
        BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
        PixeldataARGB* pointer = (PixeldataARGB*)bitmapData.Scan0;

        List<Point> pointList = new List<Point>();

        for (int y = 0; y < bitmap.Height; y++)
        {
            for (int x = 0; x < bitmap.Width; x++)
            {
                PixeldataARGB current = *pointer;
                if (current.r == r && current.g == g && current.b == b && current.a == a)
                    pointList.Add(new Point(x, y));
                pointer++;
            }
        }

        bitmap.Dispose();

        return pointList;
    }
}