One feature of GDI is that all images are drawn through DC. DC can communicate with each other.
The graphs in the two DC use DC ---> DC to transmit information to each other.
Regardless
Bitmap ---> Screen
Bitmap ---> bitmap
Screen ----> bitmap
Screen -----> Screen
They all use the transfer between DC> DC.
The key point is:
How to obtain their respective DC
For screens:
You can directly use getdc () releasedc ()
: Getdc () returns the CDC *
For images:
Cbitmap does not support DC
Cimage to obtain DC
CImage img;img.Load(imageFilePath);CDC *pDC;pDC=CDC::FromHandle(img.GetDC());// use pDC hereimg.ReleaseDC();
Note:
: Getdc () returns the CDC *
While cimage: HDC returned by getdc ()
Operate Images
: Getdc () obtains the DC of the screen. With this DC, you can plot the screen.
If we want to redraw a bitmap, we cannot simply use: getdc () because it is only responsible for drawing on the screen.
What should we do?
Since all the drawings are drawn on the DC, We must select this bitmap into the DC. In this way, the bitmap is directly operated on the DC.
PDC-> SelectObject (& BMP ):
// Use PDC to draw orthers In the BMP
Memory DC Construction
Generally, in order to avoid flickering and other phenomena, we need to construct a memory DC, and then draw it on the DC. After the painting is completed, the image will be drawn to the screen through the transmission between DC.
CDC memdc; only a CDC object has been created and DC resources have not been created.
Memdc. createcompatibledc (PDC); is the true creation of DC resources.
Creating compatible DC is the key. The key to creating a compatible DC is to be compatible with the existing DC.
Because memory DC is only an intermediary, it must pass the images in its DC to other DC (Target DC) to reflect its value.
The premise that information can be transmitted between DC and DC is that the two DC are compatible.
We can see that:The memory DC must be compatible with the Target DC
For example:
CBitmap bmp;bmp.LoadBitmap(IDI_BITMAP);CDC memDC;memDC.CreateCompatibleDC(pDC);memDC.SelectObject(&bmp);pDC->BitBlt(0,0,nWidth,nHeight,&memDC,0,0,SRCCOPY);
DC ---> transmission of DC
The following are examples for various situations:
1 bitmap ---> Screen
// Bitmap to the screen cbitmap BMP; bitmap bm; CDC memdc; CDC * PDC = getdc (); // load the image to obtain the image information BMP. loadbitmap (idb_bitmap); BMP. getbitmap (& BM); // create a screen-compatible DC and select the bitmap memdc. createcompatibledc (PDC); cbitmap * poldbmp = (cbitmap *) memdc. selectObject (& BMP); // draw a bitmap on the screen ---> screen PDC-> setstretchbltmode (coloroncolor); PDC-> stretchblt (100,100, & memdc, BM. bmwidth, BM. bmheight, srccopy); memdc. selectObject (poldbmp); // release the resource releasedc (PDC );
2
Bitmap in place Figure 1 ----- both are cbitmap objects
// Bitmap in place graph // because the destination is a bitmap, create a blank bitmap first // because it is the transfer between the bitmap and the bitmap, so how can I use two memory DC to achieve the compatibility of the two DC? As long as each DC is compatible with the screen DC, the two DC will be compatible with cbitmap destbmp; cbitmap sourcebmp; bitmap bm; // define the source DC and the Target DC object CDC sourcedc; CDC destdc; // obtain compatible screen dccdc * PDC = getdc (); // load source image sourcebmp. loadbitmap (idb_bitmap); sourcebmp. getbitmap (& BM); // create the source DC resource sourcedc. createcompatibledc (PDC); sourcedc. selectObject (& sourcebmp); // create DEST bitmap resource destbmp. createcompatiblebitmap (PDC, BM. bmwidth, BM. bmheight); // create the destdc resource destdc. createcompatibledc (PDC); destdc. selectObject (& destbmp); // pass destdc to the bitmap in place. setstretchbltmode (halftone); destdc. stretchblt (0, 0, BM. bmwidth, BM. bmheight, & sourcedc, 0, 0, 100,100, srccopy); releasedc (PDC );
Bitmap in place Figure 2 --- source bitmap is a cimage class
// Bitmap in place 2 // The Source bitmap is a cimage object, and the cimage sourceimage, cbitmap destbmp, CDC destdc, and sourceimage are directly transmitted using the member functions of the cimage object. load (imagefile); // obtain the dccdc * PDC = CDC: fromhandle (sourceimage. getdc (); // create DEST bitmap resource destdcdestdc. createcompatibledc (PDC); destdc. selectObject (& destbmp);: setstretchbltmode (destdc. m_hdc, halftone);: setbrushorgex (destdc. m_hdc, 0, 0, null); // directly use the cimage member function for transfer. In fact, cimage encapsulates the transfer between DC and sourceimage. stretchblt (& destdc, crect (100,100, 100,100), crect (,), srccopy); // release the DC resource sourceimage. releasedc ();
3. Screen Position Chart
// Screen position map cbitmap destbmp; CDC destdc; // obtain compatible screen dccdc * PDC = getdc (); // create DEST bitmap resource destbmp. createcompatiblebitmap (PDC, 100,100); // create destdc resource destdc. createcompatibledc (PDC); destdc. selectObject (& destbmp); // transfer destdc to the screen position chart. setstretchbltmode (halftone); destdc. stretchblt (100,100, 100,100, PDC, srccopy); releasedc (PDC );