Jump to content


Dx9: HBITMAP to Texture


6 replies to this topic

#1 Ciment

    New Member

  • Members
  • Pip
  • 5 posts

Posted 16 October 2006 - 05:20 PM

Hello,

I need to convert a HBITMAP hBitmap whit a image data to a D3D9Texture for bind onto two triangles ( quad ). How can face up this issue ? :surrender I need some help... all tips, suggestions are wellcome.

Thanx.

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 4979 posts
  • LocationBellevue, WA

Posted 16 October 2006 - 05:27 PM

You'll need to allocate a buffer and call GetDIBits to get access to the raw pixels of the bitmap. Use a screen DC as the reference. Then you can upload the raw pixels to a D3D texture.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 Ciment

    New Member

  • Members
  • Pip
  • 5 posts

Posted 16 October 2006 - 05:48 PM

Reedbeta said:

You'll need to allocate a buffer and call GetDIBits to get access to the raw pixels of the bitmap. Use a screen DC as the reference. Then you can upload the raw pixels to a D3D texture.

:yes:

pseudocode

Setup A
---------

- buff=Malloc ( w*h*3 )
- GetDIBBits (..., buff, )
- Create EmptyTexture
- Lock
- MemCopy ( texture.pBits, buff )
- Unlock
- Free ( buff )
- RenderTexture


Setup B
---------

- Create EmptyTexture
- Lock
- GetDIBBits (..., texture.pBits, )
- Unlock
- RenderTexture

The bitmap is previously get it from a window. ( Using HDC, CreateCompatibleBitmap, SelectObject... GDI functions ). May be i will find problems with image format ? texture pitch ? :ninja:

I forgot to say that i need a good performance, because i need implement this feature in every frame. Maybe a little strange u think.

Best regards.

#4 Nils Pipenbrinck

    Senior Member

  • Members
  • PipPipPipPip
  • 597 posts

Posted 16 October 2006 - 08:43 PM

Hm. per frame sounds like a problem if you need performance.


Why does your image comes as a HBITMAP? maybe there's a way to archive the same thing without going through the not so fast GDI32 functions.

#5 Ciment

    New Member

  • Members
  • Pip
  • 5 posts

Posted 17 October 2006 - 05:38 AM

Nils Pipenbrinck said:

Hm. per frame sounds like a problem if you need performance.


Why does your image comes as a HBITMAP? maybe there's a way to archive the same thing without going through the not so fast GDI32 functions.

Unfortunately, the source is a window, so HDC is inevitable, but Bitmap maybe can i change. Some idea ?

#6 Nils Pipenbrinck

    Senior Member

  • Members
  • PipPipPipPip
  • 597 posts

Posted 17 October 2006 - 08:41 AM

Hm. I'm on thin ice here, but can't you create a compatible DC (memory DC), then create and select a DibSection into it.

The DC can then be passed to the window via a WM_PAINT message.

This way the window will draw itself into the dc, and you have a pointer to the raw bitmap memory (from CreateDibSection). This saves you one Blit (from DC to memory), and you can select a Dib-Format that matches your texture format.

This should be faster.

Oh, and if you can, use dynamic textures. These are a bit faster than ordinary textures. They require some extra work when the context is lost, but you save another image copy inside DirectX since DirectX does not need to create it's own memory copy for the texture.

#7 Ciment

    New Member

  • Members
  • Pip
  • 5 posts

Posted 17 October 2006 - 02:00 PM

Hil,

I found a friendly way. ( tested )


GetClientRect (hFWnd[i],&r);

hDC=GetDC (hFWnd[i]);

		

hr=hQuad[i]->pTexture->GetSurfaceLevel (0,&pSurfaceDst);

hr=pSurfaceDst->GetDC(&hdcDest);

		

BitBlt(hdcDest, 0, 0, r.right,r.bottom, hDC, 0, 0, SRCCOPY);

		

hr=pSurfaceDst->ReleaseDC(hdcDest);

pSurfaceDst->Release();

pSurfaceDst=NULL;

		

ReleaseDC(hFWnd[i], hDC);

hr=pDevice->SetTexture(0, hQuad[i]->pTexture);


But now i have a problem, if the window 'hFWnd[i]' don't draw onto its client area (maybe it's minimized or a piece of window it's outside the screem desktop), i can't get it. arghhh, i try to force to paint with WM_PAINT message and InvalidateRect... but it doesn't work. Any idea ??? Or other method ?

Kind regards.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users