Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6570551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:46:13+00:00 2026-05-25T14:46:13+00:00

I am creating a program that scans all the pixels of an image, and

  • 0

I am creating a program that scans all the pixels of an image, and whenever it finds a pixel that contains the color pink. It makes the pixel black. But it doesn’t seem to find a pink pixel when there is two of them on the image. I do not know if I am using LockBits correctly, maybe I am using it wrong. Can someone please help me solve this I would greatly appreciate it.

Here is the code below:

            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); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:46:14+00:00Added an answer on May 25, 2026 at 2:46 pm

    You don’t need to copy the pixel data into an array. The point of LockBits is it gives you direct (unsafe) access the memory. You can just iterate the pixels and change them as you find them. You will need to know the format of the image to do this successfully.

      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;
        }
      }
    

    It’s a little more tricky in VB than C# as VB has no knowledge of pointers and requires the use of the marshal class to access unmanaged data. Here’s some sample code. (For some reason I originally though this was a VB question).

      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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a program that installs a keyboard hook to capture all keys and
WRITTEN IN JAVA Im creating a program that connects to a proxy and then
I need to generate a pseudo-key column in a MySQL table that's defined as
I am creating a little TV guide for myself and I have a list
I will preface the question by saying that I am somewhat new to the
I'm having trouble creating a port in Unix. This code keeps returning Error creating
I'm fairly new to VB.NET, and I've mainly been doing ASP programming up 'til
Solved: Workable solution: sbi's answer Explanation for what really happens: Hans's answer Explanation for
I am attempting to the read-in a .txt file and create a map to
I'd like to start using unit tests, but I'm having a hard time understanding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.