Explain, please. There are HWND windows. From it I receive dc: GetDC (hwnd). Now how can I create a context in memory that is compatible with the DC window, so that BITMAP is in memory context. And so I had a pointer to an array of pixels of this BITMAP. Do not write that in the internet a lot of information about this. She helped me a little.

HBITMAP hbmp; HDC windc; HDC memdc; int bits[10000]; ////////////// windc=GetDC(hwnd); memdc=CreateCompatibleDC(windc); hbmp=СreateBitmap(100,100,1,24,&bits); SelectObject(memdc,hbmp); SetPixel(memdc,1,1,RGB(255,255,0)); //////////////// WM_PAINT: BitBlt(GetDC(hwnd),0,0,100,100, memdc,0,0,SRCCOPY);` 

Here, nothing happens. The window behaves alone as with this code, that without it. bits are pixels. As planned, changing them will change the picture in the window.

  • What did you do and what did not work out? Write. - VladD
  • @VladD, which I just did ... Now I’ll write the last thing I tried - ololo

1 answer 1

You will not succeed, Windows will not give you access to the bytes in memory, because they do not guarantee you the format and the desired representation. That is, you must draw in the picture, and then draw this picture in DC.

Here is a fragment of the working code (I threw out the pieces, I hope not too much):

 void Engine::OnPaint(const HDC hDC, const RECT& winClipRect) { Gdiplus::Graphics graphics(hDC); Gdiplus::Rect clipRect(winClipRect.left, winClipRect.top, winClipRect.right-winClipRect.left, winClipRect.bottom-winClipRect.top); RECT winClientRect; ::GetClientRect(m_hWnd, &winClientRect); Gdiplus::Rect clientRect(winClientRect.left, winClientRect.top, winClientRect.right-winClientRect.left, winClientRect.bottom-winClientRect.top); m_MemBitmap.SetSize(clientRect.Width, clientRect.Height, &graphics); GraphicsContainer cont = m_MemBitmap.GetGraphics()->BeginContainer(); // нужно чистить, у нас была транспарентность m_MemBitmap.GetGraphics()->Clear(0x00000000); m_MemBitmap.GetGraphics()->SetClip(clipRect); // рисуем в картинку pRootObject->Render(m_MemBitmap.GetGraphics(), clientRect, clipRect, false); // а теперь картинку на экран graphics.DrawImage(m_MemBitmap.GetBitmap(), 0, 0); m_MemBitmap.GetGraphics()->EndContainer(cont); } 

Here m_MemBitmap is the instance of the class that encapsulates Gdiplus::Bitmap* and Gdiplus::Graphics* , and implements reallocation, if needed.

  • now add sample code - VladD
  • @vlad, I once did it on Delphi, and this is where the procedure is CreateDIBSection, the last parameter was the pointer - ololo
  • @ololo: updated answer - VladD