How do i access surface memory using DirectDraw 9?

Posted on 2005-04-28
Last Modified: 2013-12-08
I need an example of how to access the R,G, B, and A pixels in a surface after locking the surface. I need to be able to read and write them as quickly as possible.

I have tried

I have also tried
Dim Pic As LockedData = surfaceOld.Lock(LockFlags.Wait)

but I do not know how to get the data from it.  I can get the following INFORMATION but it does not seem to be of any use:
        'Dim A As String = Pic.ToString()
        'Dim B As String = Pic.Height.ToString
        'Dim C As String = Pic.Pitch.ToString
        'Dim D As String = Pic.RGBBitCount.ToString
        'Dim E As String = Pic.Width.ToString

If I could get a 1-dimensional array of bytes, I know how to use pitch to get to a new scan line.
I also need to know how to find the pixelformat so I can manipulate the colors.

Question by:glover110697
    LVL 8

    Expert Comment

    You might luck out and get a answer here, but I think you'd get better ones if you asked an admin to move the question here:

    Author Comment

    Thank you for your help.
    I have asked in Community Support that the question be moved as you advised.
    LVL 17

    Expert Comment

    DirectDraw has been deprecated.  If you really want to use is, there is, I believe, a Data field that is the graphics stream.  Have you looked at the deprecated DX9 DirectDraw docs on the website?  That's my reference I always go back to...

    I'm not positive what is being exposed to VB these days, but with C++ if you have a IDirect3DSurface9 object, you can use the LockRect method to lock the surface and UnlockRect to unlock it.

    LockRect returns a D3DLOCKED_RECT structure, which contains the pitch, and the pointer to the data bits.  Which is more like the old-style DirectDraw Lock let you do.

    They're hiding DirectDraw functions away under D3D bit by bit, then finding people need them more accessible so they 'bubble up' to the top again...

    Let me know if you need more of a 'push' than that.


    Author Comment

    I have checked the deprecated docs for DirectDraw.  They do not give any information other than that it has been deprecated.  Using VS Visual Basic I tried to find out what methods are available for a surface. LocKRectangle is not one of them.  There is a .Lock but it seems to return something called a "GraphicsStream".  I do not know how to use a GraphicsStream to access the bytes that make up each pixel, then get them back onto the surface.

    I do not know how to do 2D using Direct3D.  I have tried isome experiments but it was extremely slow.  I would really like to use DirectDraw.

    LVL 17

    Expert Comment

    The GraphicsStream class should have a simple Read method that returns a bytestream.  That should be easy.  Did you try looking at it in VS to find the methods available?

    DirectDraw was basically phased out almost two years ago, and is 'dying off'.  "DirectX Graphics" is the merging of DDraw and D3D into one interface.

    Also, are you running the latest DX SDK (I think it's the Feb 05 update)?  The newer versions may have more (or less!) functionality...

    LockRect is the method off the IDirect3DSurface9 object, so if you are trying to access via a different type of surface, that'd be why you didn't find it.  Unfortunately, LockRect with DXG gives the closest thing to old DX7 or earlier surface locks.


    Author Comment

    I am running October 2004 SDK.
    Referrig back to my earlier code snippet:

    Dim Pic As LockedData = surfaceOld.Lock(LockFlags.Wait)

    It tells me that Pic.Data is a GraphicsStream.

    How can I:
    Get the data from it into a byte array
    Modify the byte array (I know I need pitch and pixelformat)
    Put the data back to the surface so I can flip it to the primary.

    I do not understand GraphicsStream or bytestream enough to know how to manupulate them.
    LVL 17

    Accepted Solution

    Off the top of my head (I haven't used DDraw methods in YEARS!), surfaceOld should have a method to retrieve a description structure (GetSurfaceDesc?, takes a DDSURFACEDESC2 or something like that).  That should get you all the surface information you might need.

    The new GraphicsStream class doesn't seem to have ANY real samples out there, which was why I suggested you update to the Feb05 SDK and see if there are newer samples, or newer methods like I mentioned.

    The little I HAVE seen shows there is stream.Read and stream.Write methods, which look like they just take a byte array of the proper size and block-copy the entire thing.  I don't know if it skips over the stride/padding (which would make sense, as this is being abstracted away), but your first test of .Read you should create an overly-large buffer.  Generally, if you are creating in say 32-bit with 16-byte alignment anyway, there isn't likely extra padding.

    Here's the info off the page I found on MSDN.  Note that there is NO final docs on this stuff, and it could HUGELY depend on the exact version of the SDK you have...

    GraphicsStream.Read Method
    Note: This documentation is preliminary and is subject to change.

    Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.


        Visual Basic .NET      Overrides Public Function Read( _
            ByVal buffer() As Byte, _
            ByVal offset As Integer, _
            ByVal count As Integer _
        ) As Integer


        buffer      System.Byte[]. Reference to a Byte Leave Site array that is used as the read buffer.
        offset      System.Int32. Value that specifies the offset, from the start of the buffer, of the data read from buffer.
        count      System.Int32. Value that specifies the number of bytes to read.

    Return Value

        System.Int32. Integer that represents the number of bytes read into buffer from the graphics stream.

    (I'll reiterate that in the long run, learning a little about doing 2D under DirectX Graphics and 3d/textures will go a long way to making your life easier!)


    Author Comment

    Thanks for your help.  I guess that I will just have to experiment with it until I find out how to use the GraphicsStream.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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

    What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
    As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now