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 8437749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:31:12+00:00 2026-06-10T07:31:12+00:00

As the subject says, I have a .bmp image and I need to write

  • 0

As the subject says, I have a .bmp image and I need to write a code which will be able to get the color of any pixel of the image. It is a 1bpp (indexed) image, so the colour will be either black or white. Here is the code I currently have:

    //This method locks the bits of line of pixels
    private BitmapData LockLine(Bitmap bmp, int y)
    {
        Rectangle lineRect = new Rectangle(0, y, bmp.Width, 1);
        BitmapData line = bmp.LockBits(lineRect,
                                       ImageLockMode.ReadWrite,
                                       bmp.PixelFormat);
        return line;
    }
    //This method takes the BitmapData of a line of pixels
    //and returns the color of one which has the needed x coordinate
    private Color GetPixelColor(BitmapData data, int x)
    {
        //I am not sure if this line is correct
        IntPtr pPixel = data.Scan0 + x; 
        //The following code works for the 24bpp image:
        byte[] rgbValues = new byte[3];
        System.Runtime.InteropServices.Marshal.Copy(pPixel, rgbValues, 0, 3);
        return Color.FromArgb(rgbValues[2], rgbValues[1], rgbValues[0]);
    }

But how can I make it work for a 1bpp image? If I read only one byte from the pointer it always has the 255 value, so I assume, I am doing something wrong.
Please, do not suggest to use the System.Drawing.Bitmap.GetPixel method, because it works too slow and I want the code to work as fast as possible.
Thanks in advance.

EDIT:
Here is the code that works fine, just in case someone needs this:

    private Color GetPixelColor(BitmapData data, int x)
    {
        int byteIndex = x / 8;
        int bitIndex = x % 8;
        IntPtr pFirstPixel = data.Scan0+byteIndex;
        byte[] color = new byte[1];
        System.Runtime.InteropServices.Marshal.Copy(pFirstPixel, color, 0, 1);
        BitArray bits = new BitArray(color);
        return bits.Get(bitIndex) ? Color.Black : Color.White;
    }
  • 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-06-10T07:31:14+00:00Added an answer on June 10, 2026 at 7:31 am

    Ok, got it! You need to read the bits from the BitmapData and apply a mask to the bit you want to extract the colour:

    var bm = new Bitmap...
    
    //lock all image bits
    var bitmapData = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
    
    // this  will return the pixel index in the color pallete
    // since is 1bpp it will return 0 or 1
    int pixelColorIndex = GetIndexedPixel(50, 30, bitmapData);
    
    // read the color from pallete
    Color pixelColor = bm.Pallete.Entries[pixelColorIndex];
    

    And here is the method:

    // x, y relative to the locked area
    private int GetIndexedPixel(int x, int y, BitmapData bitmapData)
    {
        var index = y * bitmapData.Stride + (x >> 3);
        var chunk = Marshal.ReadByte(bitmapData.Scan0, index);
    
        var mask = (byte)(0x80 >> (x & 0x7));
        return (chunk & mask) == mask ? 1 : 0;
    }
    

    The pixel position is calculated in 2 rounds:

    1. Find the byte where pixel in ‘x’ is (x / 8): each byte holds 8 pixels, to find the byte divide x/8 rounding down: 58 >> 3 = 7, the pixel is on the byte 7 of the current row (stride)

    2. Find the bit on the current byte (x % 8): Do x & 0x7 to get only the 3 leftmost bits (x % 8)

    Example:

    x = 58 
    // x / 8 - the pixel is on byte 7
    byte = 58 >> 3 = 58 / 8 = 7 
    
    // x % 8 - byte 7, bit 2
    bitPosition = 58 & 0x7 = 2 
    
    // the pixels are read from left to right, so we start with 0x80 and then shift right. 
    mask = 0x80 >> bitPosition = 1000 0000b >> 2 =  0010 0000b 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, the subject pretty much says it all. I have code to unzip and
As the question subject says. I have a console application in Delphi, which contains
Well, the subject says it all but I will explain a little further. I
As the subject says, I have a project based on Apple's TabController template. The
Subject says it all. I want to modify the POCO generation adapter (at http://code.msdn.microsoft.com/EFPocoAdapter
The subject basically says it all. XmlBeans' XmlError.getLine() always returns -1. Is there any
Like the subject says I have a listview, and I wanted to add the
Subject line basically says it all. I have a static method I want to
The subject of the post says it all: I want to be able to
Basically the subject says it all: We have a couple of components running on

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.