• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 678
  • Last Modified:

Can someone explain the BitmapData.Scan0 property?

This property is a strange one, I made two copies of a bitmap file, included both files as resources, and loaded the resources into bitmap and bitmapdata objects as follows:

        Dim b As New Bitmap(My.Resources.pic1)     ' from bmp file pic1
        Dim b2 As New Bitmap(My.Resources.pic2)   ' from exact copy of pic1 picture file

        Dim r As New Rectangle(0, 0, b.Width, b.Height)

        Dim bd As BitmapData
        bd = b.LockBits(r, ImageLockMode.ReadOnly, b.PixelFormat)
        Dim bd2 As BitmapData
        bd2 = b2.LockBits(r, ImageLockMode.ReadOnly, b2.PixelFormat)

I was trying to cheat and do an Equals on the two bitmapdata objects to see if they were the same picture, but they turn out to be unequal.  Putting a breakpoint after the above, code,I see that the only difference is the Scan0 property, for which one has a value of 62935008 and the other has a value of 62944688.  I guess I don't really understand what this property is, and how they ended up different.  MSDN says Scan0:

"Gets or sets the address of the first pixel data in the bitmap. This can also be thought of as the first scan line in the bitmap."

I read this and assumed that it was an address relative to the first byte of the object, maybe a header length or something.  It would seem weird if it was its own actual location in memory... does anyone know for sure?

2 Solutions
Mike TomlinsonMiddle School Assistant TeacherCommented:
The Scan0 property returns the actual location in memory of the first pixel so it makes sense that those two would be different.

If you need a copy then why not just use the Clone() method of your first Bitmap "b"?
In VB you cannot access directly memory returned by LockBits. You can use this method in C# or C++ which can work with unmanaged pointers. The following article shows how to do this:

Of course, pointers to pixel data in two different bitmaps are different, even if image is the same. Every bitmap keeps it's own copy of pixel data.

You can use Marshal.Copy methods to copy between unmanaged bitmap bits and Byte() array.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now