使用LockBits()函数查找偶数和奇数列和行的位图

时间:2017-07-11 10:40:02

标签: c# bitmap lockbits

我使用第二个块代码来查找位图的偶数和奇数列,以便在位图中构建一个方格页面,我使用了我的IsEven()和IsOdd()函数

我想用lockbits做这个工作:

public unsafe void DrawCheckeredPage()
    {
        Bitmap bmpShati = null;
        bmpShati = new Bitmap(@"\ColorReductedBMP.bmp");
        Color GrayOne = Color.FromArgb(153, 153, 153);
        Color WhiteOne = Color.FromArgb(255, 255, 255);

        for (int x = 0; x < bmpShati.Width; x++)
        {
            for (int y = 0; y < bmpShati.Height; y++)
            {
                Color PreCol = bmpShati.GetPixel(x, y);
                if (OmitedPictureBoxes.Exists(item => (Color)item.Tag == PreCol))
                {
                    if (IsEven(x) && IsEven(y))
                    {
                        bmpShati.SetPixel(x, y, GrayOne);
                    }
                    else if (IsOdd(x) && IsOdd(y))
                    {
                        bmpShati.SetPixel(x, y, GrayOne);
                    }
                    else if (IsEven(x) && IsOdd(y))
                    {
                        bmpShati.SetPixel(x, y, WhiteOne);
                    }
                    else if (IsOdd(x) && IsEven(y))
                    {
                        bmpShati.SetPixel(x, y, WhiteOne);
                    }
                }
            }

        }
        bmpShati = resizeBitmap2(bmpShati, newW, newH);
        pbMain.Invoke((Action)(() => pbMain.Image = bmpShati));
    }

但是有一个问题是Checkered页面绘制不正确我用GetPixel()和SetPixel做了但我必须使用LockBits,你能帮我吗?

这是我的代码:

public unsafe void DrawCheckeredPage()
        {
            Bitmap bmpShati = null;
            bmpShati = new Bitmap(@"\ColorReductedBMP.bmp");
            Rectangle rect = new Rectangle(0, 0, bmpShati.Width, bmpShati.Height);
            var _bdata = bmpShati.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
            var _ptr = (byte*)_bdata.Scan0;
            int _numBytes = _bdata.Stride * _bdata.Height, row, col;
            for (int i = 0; i < _numBytes; i += 4)
            {
                Color PreCol = Color.FromArgb(
                    (*(_ptr + i + 2)),
                    (*(_ptr + i + 1)),
                    (*(_ptr + i))
                    );
                if (OmitedPictureBoxes.Exists(item => (Color)item.Tag == PreCol))
                {
                    row = i / _bdata.Stride;
                    col = i % _bdata.Stride;
                    if (IsEven(row) && IsEven(col))
                    {

                        (*(_ptr + i + 2)) = Convert.ToByte(153);
                        (*(_ptr + i + 1)) = Convert.ToByte(153);
                        (*(_ptr + i)) = Convert.ToByte(153);

                    }
                    else if (IsOdd(row) && IsOdd(col))
                    {
                        (*(_ptr + i + 2)) = Convert.ToByte(153);
                        (*(_ptr + i + 1)) = Convert.ToByte(153);
                        (*(_ptr + i)) = Convert.ToByte(153);

                    }
                    else if (IsEven(row) && IsOdd(col))
                    {
                        (*(_ptr + i + 2)) = Convert.ToByte(255);
                        (*(_ptr + i + 1)) = Convert.ToByte(255);
                        (*(_ptr + i)) = Convert.ToByte(255);
                    }
                    else if (IsOdd(row) && IsEven(col))
                    {
                        (*(_ptr + i + 2)) = Convert.ToByte(255);
                        (*(_ptr + i + 1)) = Convert.ToByte(255);
                        (*(_ptr + i)) = Convert.ToByte(255);
                    }
                }
            }
            bmpShati.UnlockBits(_bdata);
            bmpShati = resizeBitmap2(bmpShati, newW, newH);
            pbMain.Invoke((Action)(() => pbMain.Image = bmpShati));
        }
        public bool IsOdd(int value)
    {
        return value % 2 != 0;
    }
    public static bool IsEven(int value)
    {
        return value % 2 == 0;
    }

0 个答案:

没有答案