Of course, I would like to use GDI + to completely replace GDI, and then only need to go from GDI objects to GDI + objects. But there are a few projects, but also new technology features, but also want to quickly develop, and will not discard the old version of the existing features. No way, can only sewing, ka bang 碙 啌 ...
One, CDC to graphics as long as this:
Graphics g(pDC->GetSafeHdc());//只要能构造CDC句柄的MFC对象都可以
//CClientDC、CPaintDC,也都可以
Second, CBitmap to graphics need to create a memory bitmap device:
CDC MemDC;
CBitmap MemBitmap;
MemDC.CreateCompatibleDC(pDC);
MemBitmap.CreateCompatibleBitmap(pDC,size.cx, size.cy);
CBitmap *pOldBit=MemDC.SelectObject(&MemBitmap);
Graphics g(MemDC.GetSafeHdc());
///… you can work with GDI+ … ///
or use:
Bitmap::Bitmap(HBITMAP,HPALETTE)。
static?Bitmap?*FromHBITMAP(HBITMAP?hbm,HPALETTE?hpal);
Third, want to use the bitmap to open the picture, but need to use the GDI's drawing function to operate (generally all is the historical legacy problem), needs to convert the bitmap object to the CBitmap, may thus:
Bitmap* pPicture = new Bitmap(L"dotLive.jpg");
HBITMAP hBitmap;
CBitmap* pbm;
pPicture->GetHBITMAP(Color(0,0,0),&hBitmap);
pbm = CBitmap::FromHandle(aaa1);
///… use pbm to do the GDI works ...///
pbm->DeleteObject();//要记得删除GDI对象,否则你会发现你的内存占用 …
Four, save bitmap, open very convenient, save is not so convenient. Bitmap or image opening the image automatically locks the file, and when saved, it produces errors such as the file being used. One compromise is to create a new bitmap object on save, draw the original object onto a new object, and then delete the original object to release the file. Save the new object. If you cannot use clone, which copies the locked file resources together, the unlocked file will have one more release object.
Bitmap* tPic= new Bitmap(L"dotLive.jpg");//锁定文件
Bitmap* pPicture = new Bitmap(tPic->GetWidth()
,tPic->GetHeight()
,tPic->GetPixelFormat());
Graphics g(pPicture);
g.DrawImage(tPic,0,0,m_nWidth,m_nHeight);//复制过来
delete tPic;//解锁
/// … use the pPicture to access "dotLive.jpg" … ///
CLSID tiffClsid;
GetEncoderClsid(L"image/jpeg", &tiffClsid);//这个函数MSDN中有
pPicture->Save(L"dotLive.jpg" ,&tiffClsid);//保存
Five, graphics to HDC, want to use bitmap convenient to open the image, but also to the image to write the text mapping. Unfortunately, because of the legacy of history, these operations are done using GDI. and GDI + does not have a different or drawing mode (to achieve a rubber band effect), no text output background opacity, the string needs to be converted to WCHAR. How to do this, you can turn:
Bitmap* pPicture = new Bitmap(L"dotLive.jpg");//打开图片
Graphics tempG(pPicture );
HDC hdc = tempG.GetHDC ();//获得GDI句柄
CDC* tempDC = CDC::FromHandle(hdc);
/// … hurry up, draw what you want … ///
tempG.ReleaseHDC(hdc); //要记得relese,GDI都需要释放,否则资源会被占用而不接受其 他操作
Welcome to the discussion about GDI +, image processing, and. NET programming.