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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:24:34+00:00 2026-06-10T14:24:34+00:00

This is a bit puzzling here. The following code is part of a little

  • 0

This is a bit puzzling here. The following code is part of a little testing application to verify that code changes didn’t introduce a regression. To make it fast we used memcmp which appears to be the fastest way of comparing two images of equal size (unsurprisingly).

However, we have a few test images that exhibit a rather surprising problem: memcmp on the bitmap data tells us that they are not equal, however, a pixel-by-pixel comparison doesn’t find any difference at all. I was under the impression that when using LockBits on a Bitmap you get the actual raw bytes of the image. For a 24 bpp bitmap it’s a bit hard to imagine a condition where the pixels are the same but the underlying pixel data isn’t.

A few surprising things:

  1. The differences are always single bytes that are 00 in one image and FF in the other.
  2. If one changes the PixelFormat for LockBits to Format32bppRgb or Format32bppArgb, the comparison succeeds.
  3. If one passes the BitmapData returned by the first LockBits call as 4th argument to the second one, the comparison succeeds.
  4. As noted above, the pixel-by-pixel comparison succeeds as well.

I’m a bit stumped here because frankly I cannot imagine why this happens.

(Reduced) Code below. Just compile with csc /unsafe and pass a 24bpp PNG image as first argument.

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

class Program
{
    public static void Main(string[] args)
    {
        Bitmap title = new Bitmap(args[0]);
        Console.WriteLine(CompareImageResult(title, new Bitmap(title)));
    }

    private static string CompareImageResult(Bitmap bmp, Bitmap expected)
    {
        string retval = "";

        unsafe
        {
            var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            var resultData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat);
            var expectedData = expected.LockBits(rect, ImageLockMode.ReadOnly, expected.PixelFormat);

            try
            {
                if (memcmp(resultData.Scan0, expectedData.Scan0, resultData.Stride * resultData.Height) != 0)
                    retval += "Bitmap data did not match\n";
            }
            finally
            {
                bmp.UnlockBits(resultData);
                expected.UnlockBits(expectedData);
            }
        }

        for (var x = 0; x < bmp.Width; x++)
            for (var y = 0; y < bmp.Height; y++)
                if (bmp.GetPixel(x, y) != expected.GetPixel(x, y))
                {
                    Console.WriteLine("Pixel diff at {0}, {1}: {2} - {3}", x, y, bmp.GetPixel(x, y), expected.GetPixel(x, y));
                    retval += "pixel fail";
                }

        return retval != "" ? retval : "success";
    }

    [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
    static extern int memcmp(IntPtr b1, IntPtr b2, long count);
}
  • 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-10T14:24:35+00:00Added an answer on June 10, 2026 at 2:24 pm

    Take a look at this, which pictorially illustrates a LockBits buffer – it shows the Rows of Strides and where Padding can appear at the end of the Stride (if it’s needed).

    • https://web.archive.org/web/20141229164101/http://bobpowell.net/lockingbits.aspx

    • http://supercomputingblog.com/graphics/using-lockbits-in-gdi/

    A stride is probably aligned to the 32bit (i.e. word) boundary (for efficiency purposes)…and the extra unused space at the end of the stride is to make the next Stride be aligned.

    So that’s what’s giving you the random behaviour during the comparison…spurious data in the Padding region.

    When you are using Format32bppRgb and Format32bppArgb that’s naturally word aligned, so I guess you don’t have any extra unused bits on the end, which is why it works.

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

Sidebar

Related Questions

This bit of code comes with new classes that are subclasses of UITableViewController... -
How do I change this bit of code so that I only allow pdf
I've got this bit of code working so that it toggles a show/hide and
Alright, this one's been puzzling me for a bit. the following function encodes a
basically got this bit of code that doesn't work in ie8 & lower. The
I have this bit of code that is being converted from vb6 to vb.net.
I have this bit of code that is receiving a series of messages: byte[]
I have this bit of code that has to iterate trough a enum and
This bit of code is taking almost a half second to execute. Could somebody
To this bit of code I pass the string kellogs special k and I

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.