• Advertisement
Sign in to follow this  

copying gdi texture into directx texture

This topic is 1773 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I recently came along this thread: http://www.gamedev.net/topic/563327-directx-9-customs-fonts/ while looking for a method to use custom font options in directx.


I'm currently at a loss though in how I'd go about copying a gdi+ bitmap into a directx9 texture. The thread I've referenced above states that a "simple memcpy will do the trick", but setting the source as the Bitmap and copying straight over doesn't seem to work. 


Any ideas? 


--Code Attempt--


void attemptCopy()
static IDirect3DTexture9* mTexture;
Gdiplus::GdiplusStartupInput m_gdiplusStartupInput;
ULONG_PTR m_gdiplusToken;

Gdiplus::GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);

HDC hdc;
UINT mWidth = 1024;
UINT mHeight = 1024;

//hdc = BeginPaint(g_engine->getWindowHandle(), &ps);
using namespace Gdiplus;

Bitmap bitmap(1024, 1024, PixelFormat32bppARGB);
Graphics graphics(&bitmap);

Color transparent = Color(0, 255, 255, 255);


FontFamily fontFamily(L"Arial");
StringFormat strformat;
wchar_t pszbuf[] = L"Text Designer";

GraphicsPath path;
path.AddString(pszbuf, wcslen(pszbuf), &fontFamily,
FontStyleRegular, 48, Gdiplus::Point(10,10), &strformat );
Pen pen(Color(234,137,6), 6);
graphics.DrawPath(&pen, &path);
SolidBrush brush(Color(128,0,255));
graphics.FillPath(&brush, &path);

//save bitmap for comparison
CLSID pngClsid;
GetEncoderClsid(L"image/png", &pngClsid);
bitmap.Save(L"test_bit.png", &pngClsid, NULL);


D3DLOCKED_RECT lockedRect;
mTexture->LockRect(0, &lockedRect,0, 0);

unsigned char* TexData = (unsigned char*)lockedRect.pBits;

memcpy(TexData, &bitmap, sizeof(bitmap) );


D3DXSaveTextureToFileA("test.png",D3DXIFF_PNG, mTexture, 0);

//EndPaint(g_engine->getWindowHandle(), &ps);


--Attempt Result--


test.png (directx-texture): http://i1280.photobucket.com/albums/a500/daegon123/test_zps09f12c7f.png

test_bit.png (Bitmap.save): http://i1280.photobucket.com/albums/a500/daegon123/test_bit_zpse8be6cd7.png


The Bitmap as you can see comes out fine, but the directx texture remains blank, hence I'm assuming something's

wrong in my copy method.

Edited by dhk

Share this post

Link to post
Share on other sites

No, you can't directly access a Bitmap's internal pixel data. You need to use the Bitmap::LockBits method to retrieve a BitmapData object which contains a pointer to a temporary buffer filled with pixel data. You can then memcpy the contents of this buffer into the D3D texture. Also keep the scanline padding in both the D3D and GDI objects in mind. You may have to copy data line by line based on the Stride values of the respective Locking-structs.

Share this post

Link to post
Share on other sites

Do you use D3DX API?

  1. In this case you would write the source image into a stream instead of file.
  2. Load it into the memory from the stream.
  3. calling D3DXCreateTextureFromFileInMemoryEx() API.

A second solution, without D3DX.

You would create a kind of DDS texture from such bitmap (these two types are different and you can not pass PNG or BMP into locked rectangle).

It is more complex.


Please consider reading blog:


Edited by kubera

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement