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

How do i access surface memory using DirectDraw 9?

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.

  • 4
  • 3
1 Solution
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:
glover110697Author Commented:
Thank you for your help.
I have asked in Community Support that the question be moved as you advised.
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.

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

glover110697Author Commented:
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.

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.

glover110697Author Commented:
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.
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!)

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

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

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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