Can someone explain the BitmapData.Scan0 property?

Posted on 2006-03-31
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?

Question by:riceman0
    LVL 85

    Accepted Solution

    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"?
    LVL 48

    Assisted Solution

    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.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now