Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Is there any way to obtain a HBITMAP or HICON from a ID2D1Bitmap * using Direct2D? I am using this function to load the bitmap.

The reason I ask is because I am creating my level editor tool and would like to draw a PNG image on a standard button control.

I know that you can do this using GDI+:

HBITMAP hBitmap;

Gdiplus::Bitmap b(L"a.png");
b.GetHBITMAP(NULL, &hBitmap);

SendMessage(GetDlgItem(hDlg, IDC_BUTTON1), BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap);

Is there any equivalent, simple solution using Direct2D? If possible, I would like to render multiple PNG files (some with transparency) on a single button.

share|improve this question

1 Answer 1

Direct2D bitmaps are hardware device dependent resources and not generally easily accessible from the CPU side of the system. They don't even have lock or map methods. This makes it difficult to get at the pixels of a bitmap; it's not really designed to be used that way.

If you only have the D2D bitmap object available, you can render it to a HWND-backed render target, and from there you can get the HWND's device context and with that you can create a compatible bitmap and blit from the render target DC to your new bitmap's DC. This is obviously exceedingly clunky.

However, in your link you indicate that you are passing through the WIC bitmap source object to load your pixel data. If you cache that object for your level editor, you can more easily obtain the pixels, since IWICBitmapSource has CopyPixels method you can use to dump the bitmap data into a raw byte array (although the Lock method may be better if you want to avoid a lot of copies). From there you can create an HBITMAP or HICON from the raw bytes in the traditional fashion.

(If your bitmap ended up backed by a D3D texture or DXGI surface instead, you could also use the pixel access methods of those interfaces.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.