日期:2014-05-17 浏览次数:20933 次
        int centerx = -1;
        int centery = -1;
        Bitmap bp;
        public Form1()
        {
            InitializeComponent();
        }
        unsafe private void button1_Click_1(object sender, EventArgs e)
        {
            bp = (Bitmap)Bitmap.FromFile("e:\\test.bmp");
            BitmapData data = bp.LockBits(new Rectangle(0, 0, bp.Width, bp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            Dictionary<uint, List<Point>> colorpoints = new Dictionary<uint, List<Point>>();
            uint* p = (uint*)data.Scan0;
            for (int i = 0; i < bp.Width; i++)
            {
                for (int j = 0; j < bp.Height; j++)
                {
                    uint color = p[j * bp.Width + i];
                    if (!colorpoints.Keys.Contains(color))
                        colorpoints.Add(color, new List<Point>());
                    else
                        colorpoints[color].Add(new Point(i, j));
                }
            }
            var cross = colorpoints.OrderByDescending(x => x.Value.Count).Select(y => y.Key).ToArray()[1];
            List<Point> list = colorpoints[cross];
            int leftx = list.Min(x => x.X);
            int rightx = list.Max(x => x.X);
            int topy = list.Min(x => x.Y);
            int bottomy = list.Max(x => x.Y);
            centerx = (leftx + rightx) / 2;
            centery = (topy + bottomy) / 2;
            for (int i = centerx - 10; i < centerx + 11; i++)
            {
                for (int j = centery - 10; j < centery + 11; j++)
                {
                    p[j * bp.Width + i] = 0xffffffff;
                }
            }
            bp.UnlockBits(data);
            Invalidate();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            if(centerx == -1)
                return;
            e.Graphics.DrawImage(bp, new Point(0, 0));
        }
------解决方案--------------------
结合10、11楼的方法,基本可以排除杂斑。。
------解决方案--------------------