Loading D2D1Bitmap from raw image file in memory

Started by
2 comments, last by Nik02 9 years, 6 months ago
Hello, I'm new in the forums, so pleased to meet you!

I've been working the last weeks on a library for game development to help me with simple and common functions. One of them is compression and decompression of game resources, such as sprites and textures. I extract them on demand into memory. But I got myself stuck with this, I can't find a way to convert it to a D2D1Bitmap, the methods I find need to pass a URI, and I don't have it, I only have the binary data of a PNG encoded image.

Is there a way to use the data directly from memory instead of loading it from hdd?
Advertisement

The bitmap creation function has a parameter "pSrcData", which you can use to initialize the bitmap contents from memory.

http://msdn.microsoft.com/en-us/library/windows/desktop/dd371800(v=vs.85).aspx

If you already have a bitmap allocated and want to set its memory, use the following:

http://msdn.microsoft.com/en-us/library/windows/desktop/dd371155(v=vs.85).aspx

Note that the creation is potentially expensive, so you will want to use the CopyFromMemory method if you update the bitmap often.

EDIT: You will need to decode the PNG to raw pixels before doing these. For this, you may want to use the IWICBitmapDecoder implementations:

http://msdn.microsoft.com/en-us/library/windows/desktop/ee690086(v=vs.85).aspx

That said, you can use any decoder, as long as you get the raw pixel array in the end.

Niko Suni

Thanks,
Correct me if I'm wrong, but a pseudo code for that would be something like this :

Create a stream from my stored image
Initialize a decoder passing the stream
Get Frame 0 from decode

Pass to bitmap creation (I can guess that the decoder can produce the dimensions) and keep bitmap pointer to use it to draw

Use this guy: http://msdn.microsoft.com/en-us/library/windows/desktop/dd371797%28v=vs.85%29.aspx


Is That it?

Again: thank you for the help.

Yea. Note that if you get the raw pixel data from the decoded bitmap, you can use it with any rendering API, not just D2D :)

Niko Suni

This topic is closed to new replies.

Advertisement