How do i access surface memory using DirectDraw 9?

Posted on 2005-04-28
Medium Priority
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
  • 4
  • 3

Expert Comment

ID: 13888849
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

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

Expert Comment

ID: 13919151
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 msdn.microsoft.com 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.

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 13920423
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

ID: 13921040
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

ID: 13927735
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

davebytes earned 1000 total points
ID: 13928151
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

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


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

840 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