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

  • SEARCH
  • Home
  • 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 8765725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:21:38+00:00 2026-06-13T16:21:38+00:00

I have an issue while changing some code from Bitmap.GetPixel to using a direct

  • 0

I have an issue while changing some code from Bitmap.GetPixel to using a direct pixel buffer returned by LockBits. It seems that the returned data by LockBits does give me different Color values compared to GetPixel.

That is unfortunate since this change does produce different colors which would break automated unit tests. I have a png file of 29*30 pixel which I do load a Format32bppArgb into a bitmap. Can this really be that the data returend by LockBits and GetPixel are different. How can I get around this?

Here is some code to repro it with a loaded bitmap

public unsafe static Bitmap Convert(Bitmap originalBitmap)
{
    Bitmap converted = new Bitmap(originalBitmap);
    Rectangle rect = new Rectangle(0, 0, converted.Width, converted.Height);
    var locked = converted.LockBits(rect, ImageLockMode.ReadWrite, originalBitmap.PixelFormat);
    byte* pData = (byte*)locked.Scan0;

    // bytes per pixel
    var bpp = ((int)converted.PixelFormat >> 11) & 31;

    byte* r;
    byte* g;
    byte* b;
    byte* a;

    for (int y = 0; y < locked.Height; y++)
    {
        var row = pData + (y * locked.Stride);

        for (int x = 0; x < locked.Width; x++)
        {
            a = row + x * bpp + 3;
            r = row + x * bpp + 2;
            g = row + x * bpp + 1;
            b = row + x * bpp + 0;
            var col = Color.FromArgb(*a, *r, *g, *b);
            var origCol = originalBitmap.GetPixel(x, y);
            if (origCol != col)
            {
                Debug.Print("Orig: {0} Pixel {1}", origCol, col);
            }

        }
    }
    converted.UnlockBits(locked);

    return converted;
}

Orig: Color [A=128, R=128, G=128, B=255] Pixel Color [A=128, R=127, G=127, B=255]
Orig: Color [A=0, R=128, G=128, B=255]   Pixel Color [A=0, R=0, G=0, B=0]

Orig: Color [A=45, R=128, G=128, B=255]  Pixel Color [A=45, R=130, G=130, B=254]
                                                       ok     -2      -2     +1

Most of the time but there seems to be some rounding and conversion to be going on. Can I force LockBits to return data as it would be returned by GetPixel?

  • 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-13T16:21:40+00:00Added an answer on June 13, 2026 at 4:21 pm

    To the best of my knowledge, PNGs can contain colour profiles and gamma correction information and whatever else which can affect the final colour of pixels vs. their raw representation.

    Even if we disregard specific knowledge about PNGs, in general GetPixel can return different values than is expected.

    Bitmap.GetPixel is implemented thus:

    public Color GetPixel(int x, int y)
    {
    
        int color = 0;
    
        if (x < 0 || x >= Width)
        {
            throw new ArgumentOutOfRangeException("x", SR.GetString(SR.ValidRangeX));
        }
    
        if (y < 0 || y >= Height)
        {
            throw new ArgumentOutOfRangeException("y", SR.GetString(SR.ValidRangeY));
        }
    
        int status = SafeNativeMethods.Gdip.GdipBitmapGetPixel(new HandleRef(this, nativeImage), x, y, out color);
    
        if (status != SafeNativeMethods.Gdip.Ok)
            throw SafeNativeMethods.Gdip.StatusException(status);
    
        return Color.FromArgb(color);
    }
    

    The definition of SafeNativeMethods.Gdip.GdipBitmapGetPixel is:

    [DllImport(ExternDll.Gdiplus, SetLastError=true, ExactSpelling=true, CharSet=System.Runtime.InteropServices.CharSet.Unicode)] // 3 = Unicode 
    [ResourceExposure(ResourceScope.None)]
    internal static extern int GdipBitmapGetPixel(HandleRef bitmap, int x, int y, out int argb);
    

    Which we learn from here is Gdiplus::Bitmap::GetPixel. The documentation for that function says:

    Remarks

    Depending on the format of the bitmap, Bitmap::GetPixel might not return the same value as was set by Bitmap::SetPixel. For example, if you call Bitmap::SetPixel on a Bitmap object whose pixel format is 32bppPARGB, the pixel’s RGB components are premultiplied. A subsequent call to Bitmap::GetPixel might return a different value because of rounding. Also, if you call Bitmap::SetPixel on a Bitmap object whose color depth is 16 bits per pixel, information could be lost during the conversion from 32 to 16 bits, and a subsequent call to Bitmap::GetPixel might return a different value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue while getting the file name with out extension from a
I have an issue while storing some special character values into db. For e.g.
I have an odd issue while using FlashCS4. I have a textfield that, when
First of all: I'm at Scala 2.8 I have a slight issue while using
I am experimenting with changing some stylesheets from javascript I have come across a
I have ran into an interesting issue while trying to create a more usable
We have implemented webservice which generates xml response. I am facing issue while invoking
I have an issue with log4net which has been bugging me for a while
I've been struggling with this issue for a while now. I have OpenCart 1.5.2.1
I have a somehow funny issue. While trying to understand why a certain website

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.