Click here to Skip to main content
15,910,121 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralBitmaps and DCs Pin
S O S14-Mar-03 7:21
S O S14-Mar-03 7:21 
GeneralRe: Bitmaps and DCs Pin
Chris Losinger14-Mar-03 7:24
professionalChris Losinger14-Mar-03 7:24 
GeneralRe: Bitmaps and DCs Pin
Jason Henderson14-Mar-03 8:53
Jason Henderson14-Mar-03 8:53 
GeneralRe: Bitmaps and DCs Pin
73Zeppelin14-Mar-03 14:21
73Zeppelin14-Mar-03 14:21 
GeneralRe: Bitmaps and DCs Pin
Chris Losinger14-Mar-03 18:09
professionalChris Losinger14-Mar-03 18:09 
GeneralRe: Bitmaps and DCs Pin
Mike Nordell15-Mar-03 3:21
Mike Nordell15-Mar-03 3:21 
GeneralRe: Bitmaps and DCs Pin
S O S14-Mar-03 8:53
S O S14-Mar-03 8:53 
GeneralRe: Bitmaps and DCs Pin
TomKat14-Mar-03 14:00
TomKat14-Mar-03 14:00 
HDC hdcScreen,hdcCompatible;
HBITMAP hbmScreen;
hdcScreen = ::CreateDC("DISPLAY", NULL, NULL, NULL);
hdcCompatible = ::CreateCompatibleDC(hdcScreen);
hbmScreen = ::CreateCompatibleBitmap(hdcScreen,width,height);
::SelectObject(hdcCompatible, hbmScreen);

Now draw in the hdcCompatible !
/**/
PBITMAPINFO CreateBitmapInfoStruct(HBITMAP hBmp)
{
BITMAP bmp;
PBITMAPINFO pbmi;
WORD cClrBits;
GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp);
cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
if (cClrBits == 1) cClrBits = 1;
else
if(cClrBits <= 4) cClrBits = 4;
else
if (cClrBits <= 8) cClrBits = 8;
else
if (cClrBits <= 16)cClrBits = 16;
else
if (cClrBits <= 24)cClrBits = 24;
else cClrBits = 32;
if (cClrBits != 24) pbmi = (PBITMAPINFO) LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD) * (2^cClrBits));
else pbmi = (PBITMAPINFO) LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER));
pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = bmp.bmWidth;
pbmi->bmiHeader.biHeight = bmp.bmHeight;
pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
if (cClrBits < 24)
pbmi->bmiHeader.biClrUsed = 2^cClrBits;
pbmi->bmiHeader.biCompression = BI_RGB;
pbmi->bmiHeader.biSizeImage = (pbmi->bmiHeader.biWidth + 7) /8 * pbmi->bmiHeader.biHeight * cClrBits;
pbmi->bmiHeader.biClrImportant = 0;
return pbmi;
}

void CreateBMPFile(LPTSTR pszFile, PBITMAPINFO pbi,HBITMAP hBMP, HDC hDC)
{
HANDLE hf;
BITMAPFILEHEADER hdr;
PBITMAPINFOHEADER pbih;
LPBYTE lpBits;
DWORD dwTotal;
DWORD cb;
BYTE *hp;
DWORD dwTmp;
pbih = (PBITMAPINFOHEADER) pbi;
lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight,lpBits, pbi, DIB_RGB_COLORS);
hf = CreateFile(pszFile,GENERIC_READ | GENERIC_WRITE,(DWORD) 0, (LPSECURITY_ATTRIBUTES) NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);
if(hf == INVALID_HANDLE_VALUE)
return;
hdr.bfType = 0x4d42;
hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) +pbih->biSize + pbih->biClrUsed * sizeof(RGBQUAD) + pbih->biSizeImage);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof (RGBQUAD);
WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), (LPDWORD) &dwTmp, (LPOVERLAPPED) NULL);
WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER)+ pbih->biClrUsed * sizeof (RGBQUAD), (LPDWORD) &dwTmp, (LPOVERLAPPED) NULL);
dwTotal = cb = pbih->biSizeImage;
hp = lpBits;
while (cb > sizeof(hp)) {
WriteFile(hf, (LPSTR) hp, (int) sizeof(hp),(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL);
cb-= sizeof(hp);
hp += sizeof(hp);
}
WriteFile(hf, (LPSTR) hp, (int) cb,(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL);
CloseHandle(hf);
GlobalFree((HGLOBAL)lpBits);
}
/*have fun*/
To save use this :
PBITMAPINFO pbi=CreateBitmapInfoStruct((HBITMAP)hbmScreen);
CreateBMPFile(d.m_ofn.lpstrFile,pbi,(HBITMAP)hbmScreen,(HDC)hdcCompatible);
DeleteObject(hbmScreen);
DeleteDC(hdcCompatible);
DeleteDC(hdcScreen);


I am the mighty keeper of the book on knowledge . Contact me to get your copy .
GeneralRe: Bitmaps and DCs Pin
S O S14-Mar-03 19:35
S O S14-Mar-03 19:35 
GeneralChange Text font on CListCtrl Pin
MyEden14-Mar-03 6:53
MyEden14-Mar-03 6:53 
GeneralRe: Easy fonts with no code Pin
Bill Gates Antimatter Particle14-Mar-03 7:12
Bill Gates Antimatter Particle14-Mar-03 7:12 
GeneralRe: Change Text font on CListCtrl Pin
valikac14-Mar-03 12:33
valikac14-Mar-03 12:33 
Questionwhy do I need to import msxml.dll in order to use IHTMLDocument2Ptr m_pHTMLDoc;? Pin
Joan M14-Mar-03 6:51
professionalJoan M14-Mar-03 6:51 
AnswerRe: why do I need to import msxml.dll in order to use IHTMLDocument2Ptr m_pHTMLDoc;? Pin
TomKat14-Mar-03 14:23
TomKat14-Mar-03 14:23 
GeneralRe: why do I need to import msxml.dll in order to use IHTMLDocument2Ptr m_pHTMLDoc;? Pin
Joan M16-Mar-03 21:34
professionalJoan M16-Mar-03 21:34 
GeneralConnect an Access Database.. Pin
Dennis L14-Mar-03 6:37
Dennis L14-Mar-03 6:37 
GeneralRe: Connect an Access Database.. Pin
Steve Schaneville14-Mar-03 9:48
professionalSteve Schaneville14-Mar-03 9:48 
GeneralRe: Connect an Access Database.. Pin
Amit Dey14-Mar-03 12:08
Amit Dey14-Mar-03 12:08 
GeneralMapping a drive in a program Pin
Anonymous14-Mar-03 6:15
Anonymous14-Mar-03 6:15 
GeneralRe: Mapping a drive in a program Pin
Mike Nordell14-Mar-03 17:54
Mike Nordell14-Mar-03 17:54 
GeneralDynamic creation of MFC controls & message handling Pin
Mandar D. Sahasrabuddhe14-Mar-03 6:11
professionalMandar D. Sahasrabuddhe14-Mar-03 6:11 
GeneralRe: Dynamic creation of MFC controls & message handling Pin
valikac14-Mar-03 12:42
valikac14-Mar-03 12:42 
GeneralDialogs within TabCtrl Pin
GWENJi14-Mar-03 3:40
GWENJi14-Mar-03 3:40 
GeneralRe: Dialogs within TabCtrl Pin
GWENJi14-Mar-03 4:30
GWENJi14-Mar-03 4:30 
GeneralRe: Dialogs within TabCtrl Pin
TomKat14-Mar-03 14:18
TomKat14-Mar-03 14:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.