C# 锁定位无法检测像素

C# 锁定位无法检测像素,c#,.net,bitmap,lockbits,C#,.net,Bitmap,Lockbits,我正在创建一个程序,扫描图像的所有像素,每当它找到一个包含粉红色的像素时。它使像素变黑。但当图像上有两个粉红色像素时,它似乎找不到一个粉红色像素。我不知道我是否正确地使用了锁位,也许我用错了。有人能帮我解决这个问题吗?我将不胜感激 代码如下: Bitmap bitmap = pictureBox1.Image as Bitmap; System.Drawing.Imaging.BitmapData d = bitmap.LockBits(new R

我正在创建一个程序,扫描图像的所有像素,每当它找到一个包含粉红色的像素时。它使像素变黑。但当图像上有两个粉红色像素时,它似乎找不到一个粉红色像素。我不知道我是否正确地使用了锁位,也许我用错了。有人能帮我解决这个问题吗?我将不胜感激

代码如下:

            Bitmap bitmap = pictureBox1.Image as Bitmap;
            System.Drawing.Imaging.BitmapData d = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
            IntPtr ptr = d.Scan0;
            byte[] rgbs = new byte[Math.Abs(d.Stride) * bitmap.Height];
            Marshal.Copy(ptr, rgbs, 0, rgbs.Length);
            Graphics g = pictureBox1.CreateGraphics();
            for (int index = 2; index < rgbs.Length; index += 3)
            {


                if (rgbs[index] == 255 &&  rgbs[index - 1] == 0 && rgbs[index - 2] == 255) // If color = RGB(255, 0, 255) Then ...
                {
                     // This never gets executed!
                     rgbs[index] = 0;
                     rgbs[index - 1] = 0;
                     rgbs[index - 2] = 0;

                }
            }
            Marshal.Copy(rgbs, 0, ptr, rgbs.Length); // Copy rgb values back to the memory location of the bitmap.
            pictureBox1.Image = bitmap;
            bitmap.UnlockBits(d); 
Bitmap Bitmap=pictureBox1.图像作为位图;
System.Drawing.Imaging.BitmapData d=位图.LockBits(新矩形(0,0,位图.Width,位图.Height),System.Drawing.Imaging.ImageLockMode.ReadWrite,位图.PixelFormat);
IntPtr ptr=d.Scan0;
字节[]rgbs=新字节[Math.Abs(d.Stride)*位图.Height];
封送处理副本(ptr,rgbs,0,rgbs.Length);
Graphics g=pictureBox1.CreateGraphics();
对于(int index=2;index
不需要将像素数据复制到数组中。
LockBits
的要点是它可以让您直接(不安全)访问内存。您可以迭代像素并在找到它们时更改它们。您需要知道图像的格式才能成功地执行此操作

  BitmapData bmd=bm.LockBits(new Rectangle(0, 0, 10, 10), 
                       ImageLockMode.ReadOnly, bm.PixelFormat);
  // Blue, Green, Red, Alpha (Format32BppArgb)
  int pixelSize=4;

  for(int y=0; y<bmd.Height; y++)
  {
    byte* row=(byte *)bmd.Scan0+(y*bmd.Stride);
    for(int x=0; x<bmd.Width; x++) 
    {
      int offSet = x*pixelSize;
      // read pixels
      byte blue = row[offSet];
      byte green = row[offSet+1];
      byte red = row[offSet+2];
      byte alpha = row[offSet+3];

      // set blue pixel
      row[x*pixelSize]=255;
    }
  }
  Dim x As Integer
  Dim y As Integer
  ' Blue, Green, Red, Alpha (Format32BppArgb)
  Dim PixelSize As Integer = 4 
  Dim bmd As BitmapData = bm.LockBits(new Rectangle(0, 0, 10, 10),
                                      ImageLockMode.ReadOnly, bm.PixelFormat)

  For y = 0 To bmd.Height - 1
    For x = 0 To bmd.Width - 1
      Dim offSet As Int32 = (bmd.Stride * y) + (4 * x)
      ' read pixel data
      Dim blue As Byte = Marshal.ReadByte(bmd.Scan0, offSet)
      Dim green As Byte = Marshal.ReadByte(bmd.Scan0, offSet + 1)
      Dim red As Byte = Marshal.ReadByte(bmd.Scan0, offSet + 2)
      Dim alpha As Byte = Marshal.ReadByte(bmd.Scan0, offSet + 3)
      ' set blue pixel
      Marshal.WriteByte(bmd.Scan0, offSet , 255)
    Next
  Next