Home > Back-end >  MFC double buffered map PNG problem
MFC double buffered map PNG problem

Time:10-06

Recently doing a photo editing software, I am a PNG figure with the figure of resources, USES the CImage class to display the image, at the time of using double buffer, image color is wrong, said on the Internet to find a lot of resources are the GDI does not support transparent color, need to use GDI +, not quite sure how GDI + realize double buffer?

CodePudding user response:

Same effect with the implementation of GDI, the code is as follows:


/* c + + code */
The RECT rc;
GetClientRect (g_hwnd, & amp; Rc);
Bitmap BMP (int) (rc) right, int (rc) bottom));


Graphics bmpGraphics (& amp; BMP);
BmpGraphics. SetSmoothingMode (SmoothingModeAntiAlias);


/* Drawing on bitmap */
SolidBrush bkBrush (Color (0, 0));
BmpGraphics. FillRectangle (& amp; BkBrush, 0, 0, rc. Right, rc, bottom);


/* Drawing on DC */
Graphics Graphics (HDC);
/* Important! Create a CacheBitmap object for the quick drawing */
CachedBitmap cachedBmp (& amp; BMP, & amp; Graphics);
Graphics. DrawCachedBitmap (& amp; CachedBmp, 0, 0);


Rendering code above the most different from other GDI + realize on the network is, in the final added a CacheBitmap object for fast rendering,


CacheBitmap is a BMP is included all pixels, and in view of the graphics of the associated DC done optimization bitmap object in particular, it can be seen from its structure parameters,


About the realization of the double buffer and is the key, although it does not belong to the core of the double buffer to achieve, if drawing need to redraw background often, you need to own interceptor WM_ERASEBKGND message, and don't do anything in the processing function, namely the message occurs not heavy background, the background of the drawing in the WM_PAINT full control,


For example, the realization of the WM_ERASEBKGND message handling
Void OnEraseBkGnd (HDC HDC)
{
//do nothing
}




Add: GDI double buffer to achieve
The RECT rc;
GetClientRect (HWND, & amp; Rc);
HDC hMemDc=CreateCompatibleDC (HDC);
HBITMAP hBmp=CreateCompatibleBitmap (HDC, rc. Right, rc, bottom);
HBITMAP hOldBmp=(HBITMAP) SelectObject (hMemDc hBmp);
//in this using hMemDc GDI drawing
BitBlt (HDC, 0, 0, rc. Right, rc. Bottom, hMemDc, 0, 0, SRCCOPY);
SelectObject (hMemDc hOldBmp);
DeleteObject (hBmp);
DeleteObject (hMemDc);

CodePudding user response:

The above methods go wrong but I don't know where is wrong?
Void CGdiPlusView: : ontouch pDC (CDC */* */)
{
CGdiPlusDoc * pDoc=GetDocument ();
ASSERT_VALID (pDoc);
if (! PDoc)
return;
CClientDC dc (this);
The RECT rc;
Rc. Right=3000;
Rc. Left=0;
Rc. Top=0;
Rc. Bottom=2000;
Bitmap BMP (int) (rc) right, int (rc) bottom));
Graphics bmpGraphics (& amp; BMP);
BmpGraphics. SetSmoothingMode (SmoothingModeAntiAlias);
SolidBrush bkBrush (Color (0, 0));
BmpGraphics. FillRectangle (& amp; BkBrush, 0, 0, rc. Right, rc, bottom);
Graphics Graphics (dc GetSafeHdc ());
Iamge_bg. The Draw (graphics. GetHDC (), 200200, iamge_bg. GetWidth (), iamge_bg. GetHeight ());
Image. The Draw (graphics. GetHDC (), 200200, the image. The GetWidth (), image, GetHeight ());
CachedBitmap cachedBmp (& amp; BMP, & amp; Graphics);
Graphics. DrawCachedBitmap (& amp; CachedBmp, 0, 0);
}
CImage image;
CImage iamge_bg;

Don't use double buffer can draw two pictures, but now in the second picture when make a mistake, I'm using MFC single document,
  • Related