We help IT Professionals succeed at work.

Can someone explain the BitmapData.Scan0 property?

riceman0
riceman0 asked
on
Medium Priority
741 Views
Last Modified: 2008-03-17

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?


Comment
Watch Question

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
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"?

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Commented:
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:
http://www.codeproject.com/cs/media/csharpgraphicfilters11.asp

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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.