i am trying to create a bitmap in mfc dll and i am writting
CDC *pDC = GetDC();
CDC memDC ;
memDC.CreateCompatibleDC(pDC);
CBitmap newbmp;
newbmp.CreateCompatibleBitmap(pDC, 300, 300);
ReleaseDC(pDC);
CBitmap *pOldBitmap = memDC.SelectObject(&newbmp);
memDC.FillSolidRect(0,0,300,300,RGB(255,255,255));
CBrush brush;
CBrush *pOldBrush = memDC.SelectObject(&brush);
memDC.Rectangle(10, 10, 80, 80);
memDC.Ellipse(60,60,220,220);
memDC.SelectObject(pOldBrush);
memDC.SelectObject(pOldBitmap);
CImage img;
img.Attach((HBITMAP)newbmp.Detach());
img.Save(_T("C:\\atest.bmp"),Gdiplus::ImageFormatBMP);
although this works properly in a Cwindows application i cannot make it works in a console application how can i get a Device conext in a console application ? i cannot get getDC to work
在调用GetDC()之前,必须具有CWnd的句柄或指针。但是在控制台应用程序中,不能调用此函数。
This works for me:
CDC *pDC;
pDC = CDC::FromHandle(::GetDC(NULL));
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap newbmp;
newbmp.CreateCompatibleBitmap(pDC, 300, 300);
CBitmap *pOldBitmap = memDC.SelectObject(&newbmp);
memDC.FillSolidRect(0,0,300,300,RGB(255,255,255));
CBrush brush;
CBrush *pOldBrush = memDC.SelectObject(&brush);
memDC.Rectangle(10, 10, 80, 80);
memDC.Ellipse(60,60,220,220);
memDC.SelectObject(pOldBrush);
memDC.SelectObject(pOldBitmap);
CImage img;
img.Attach((HBITMAP)newbmp.Detach());
img.Save(_T("C:\\atest.bmp"),Gdiplus::ImageFormatBMP);
::ReleaseDC( NULL,
*pDC);
::ReleaseDC( NULL,
memDC);
Of course to use CDC and CBitmap you need to use MFC as a shared DLL and add some headers to the stdafx.h:
#include <afxwin.h>
#include <atlimage.h>
Hope it helps,
Javier
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.