YOU CAN CODE!

 

With The Case Of UCanCode.net  Release The Power OF  Visual C++ !   HomeProducts | PurchaseSupport | Downloads  
Download Free Trial
Pricing & Purchase?
E-XD++Visual C++/ MFC Products
ActiveX COM Products
Technical Support

Links

Get Ready to Unleash the Power of UCanCode .NET


UCanCode Software focuses on general application software development. We provide complete solution for developers. No matter you want to develop a simple database workflow application, or an large flow/diagram based system, our product will provide a complete solution for you. Our product had been used by hundreds of top companies around the world!

"100% source code provided! Free you from not daring to use components because of unable to master the key technology of components!"


MFC Example: CreateFile and WriteFile with save memory dc to bitmap file

 
 

Code Snippet

Introduction

A more easy to save your memory dc to a (DIB) bitmap file(*.bmp), sometimes we don't need to bitblt our map to windows to display. this is a new way you can try.

Using the code

Only a function we needed here.It's very hard to post whole project file here to discuss. so i suppose the reader have some basic ideal about GDI programming. Pass two parameters into the function , you may need CreateCompatibleBitmap to init your second parameter.

Collapse Copy Code
HBITMAP CreateCompatibleBitmap(
  HDC hdc,        // handle to device context
  int nWidth,     // width of bitmap, in pixels
  int nHeight     // height of bitmap, in pixels
);

After you call this function sucessfully, it will generate a new bitmap named "temp.bmp" under your partation C root folder.

Collapse Copy Code
SaveMemDCToBitmapNoPalete(HDC p_hMemDC,HBITMAP p_hBitmap)
{
 HDC hMemDC = p_hMemDC;
 HBITMAP hBitmap = p_hBitmap;
 int   wBitCount = 32;
 
 BITMAP   Bitmap;  
 BITMAPFILEHEADER   bmfHdr;    
 BITMAPINFOHEADER   bi;
 
 GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap); 
 
 bi.biSize = sizeof(BITMAPINFOHEADER);    
 bi.biWidth = Bitmap.bmWidth;    
 bi.biHeight = Bitmap.bmHeight;  
 bi.biPlanes = 1;    
 bi.biBitCount = wBitCount;    
 bi.biCompression = BI_RGB;    
 bi.biSizeImage = 0;  
 bi.biXPelsPerMeter = 0;    
 bi.biYPelsPerMeter = 0;    
 bi.biClrUsed = 0;    
 bi.biClrImportant = 0; 
 
 DWORD dwBmBitsSize = ((Bitmap.bmWidth * wBitCount + 31) / 32) * 4 * Bitmap.bmHeight; 
 
 HANDLE hDib = GlobalAlloc(GHND,dwBmBitsSize+sizeof(BITMAPINFOHEADER)); 
 LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);    
 *lpbi = bi;
 
 GetDIBits(hMemDC, hBitmap, 0, (UINT)Bitmap.bmHeight,  
  (LPSTR)lpbi + sizeof(BITMAPINFOHEADER), (BITMAPINFO *)lpbi, DIB_RGB_COLORS);    
 
 HANDLE fh = CreateFile("c:\\temp.bmp",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,
  FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);   
 ASSERT(fh != INVALID_HANDLE_VALUE);
 
 DWORD dwDIBSize   =   sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwBmBitsSize;
 
 bmfHdr.bfType = 0x4D42;    
 bmfHdr.bfSize = dwDIBSize;  
 bmfHdr.bfReserved1 = 0;    
 bmfHdr.bfReserved2 = 0;    
 bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);    
 
 DWORD dwWritten;
 
 WriteFile(fh,   (LPSTR)&bmfHdr,   sizeof(BITMAPFILEHEADER),   &dwWritten,   NULL);    
 WriteFile(fh, (LPSTR)lpbi, dwDIBSize, &dwWritten, NULL);    
 GlobalUnlock(hDib);    
 GlobalFree(hDib);    
 CloseHandle(fh);
 return TRUE;
}

 

 

 

 

Copyright ?1998-2009 UCanCode.Net Software , all rights reserved.
Other product and company names herein may be the trademarks of their respective owners.

Please direct your questions or comments to webmaster@ucancode.net