Click here to Skip to main content
15,893,190 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: MFC Bitmap Help Pin
Mark Salsbery20-Jun-07 12:04
Mark Salsbery20-Jun-07 12:04 
GeneralRe: MFC Bitmap Help Pin
Hamid_RT20-Jun-07 18:42
Hamid_RT20-Jun-07 18:42 
GeneralRe: MFC Bitmap Help Pin
Mark Salsbery21-Jun-07 5:19
Mark Salsbery21-Jun-07 5:19 
GeneralRe: MFC Bitmap Help Pin
Hamid_RT21-Jun-07 19:43
Hamid_RT21-Jun-07 19:43 
GeneralRe: MFC Bitmap Help Pin
dipen.rana25-Jun-07 5:43
dipen.rana25-Jun-07 5:43 
GeneralRe: MFC Bitmap Help Pin
Mark Salsbery25-Jun-07 7:40
Mark Salsbery25-Jun-07 7:40 
GeneralRe: MFC Bitmap Help Pin
dipen.rana25-Jun-07 8:45
dipen.rana25-Jun-07 8:45 
GeneralRe: MFC Bitmap Help Pin
Mark Salsbery25-Jun-07 13:49
Mark Salsbery25-Jun-07 13:49 
Note that I stated "If you have an HBITMAP (a DDB or a DIBSection)..." you
could use that code. You are using a DIB.

dipen.rana wrote:
here it attaches hBitmap but wat file it saves a test.bmp is it the hBitmap file dat it saves as test.bmp?


// Create uninitialized DIB bitmap.
//.................................
hBitmap = CreateDIBitmap( hDC,
&bi,
0,
0,
(LPBITMAPINFO) lpbi,
DIB_RGB_COLORS);
CImage image;
image.Attach(hBitmap);
image.Save("c:\\test.bmp"); // save as BMP
image.Detach();

Here you're creating a DDB from a DIB and then attaching it to a CImage object to save it.
I'm not sure the creation is succeeding though. Is hBitmap NULL?

This might be a good case to use a DIBsection instead of a DIB since you need both an HBITMAP
and direct access to the pixel bits. Something like this (changes I made to your code are
in red):
// Change Create_DIB() to create a DIBSection instead of a DIB
 
void Create_DIB( HWND hWnd )
{
   HDC hDC; 
   unsigned char i;
   long k,j, NumBytes;
   LPBITMAPINFO info;
   unsigned char *dat;
   DWORD error;
 
   hDC = GetDC (hWnd) ; 
 
   // Intialize BITMAPINFOHEADER data.
   //.................................
   bi.biSize = sizeof (BITMAPINFOHEADER) ;
   bi.biWidth = W; 
   bi.biHeight = -H; // top-down DIB
   bi.biPlanes = 1;
   bi.biBitCount = COLORBITS;
   bi.biCompression = BI_RGB;
   bi.biSizeImage = ( ALIGNLONG( (W * COLORBITS)/8 ) * H);
   bi.biXPelsPerMeter = 0;
   bi.biYPelsPerMeter = 0;
   bi.biClrUsed = 256;
   bi.biClrImportant = 256;
 
<code>   // Allocate memory for BITMAPINFO structure and color table.
   //..........................................
   NumBytes = sizeof(BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD);</code>
   lpbi = (BITMAPINFOHEADER*)GlobalAlloc( GPTR, NumBytes );
 
   if( lpbi == NULL )
   {
      LPVOID lpMsgBuf;
      FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | 
         FORMAT_MESSAGE_FROM_SYSTEM | 
         FORMAT_MESSAGE_IGNORE_INSERTS,
         NULL, GetLastError(),
         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
         (LPTSTR) &lpMsgBuf, 0, NULL );
      // Display the string.
      MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
      // Free the buffer.
      LocalFree( lpMsgBuf );
 
      exit( EXIT_FAILURE ); // Get out now!
   }
 
   // Copy BITMAPINFOHEADER information.
   //...................................
   *lpbi = bi;
 
   // Create The Palette
   info = (LPBITMAPINFO) lpbi;
 
 
   for ( i=0; i<255; i++ )
   {
      info->bmiColors[i].rgbRed = i;
      info->bmiColors[i].rgbBlue = i;
      info->bmiColors[i].rgbGreen = i;
      info->bmiColors[i].rgbReserved = 0; 
   }
 
 
<code>//   // Set pointer to bitmap’s bit data.
//   //..................................
//   lpstBitmap = (LPSTR)lpbi + (WORD)sizeof(BITMAPINFOHEADER) +
//                                       (256 * sizeof(RGBQUAD));
 
   // Create a DIB section
 
   hBitmap = ::CreateDIBSection(hDC, (LPBITMAPINFO) lpbi, DIB_RGB_COLORS, (VOID **)&lpstBitmap, NULL, 0);</code>
 
   dat = (unsigned char*)lpstBitmap;
 
<code>// Note - you haven't allowed for row padding here!! </code>
   for ( k=0; k< H; k++ )
      for ( j = 0; j < W; j++ )
         *dat++ = (unsigned char)k;
 
<code>//   // Create uninitialized DIB bitmap.
//   //.................................
//   hBitmap = CreateDIBitmap( hDC, 
//                              &bi, 
//                              0, 
//                              0, 
//                              (LPBITMAPINFO) lpbi, 
//                              DIB_RGB_COLORS);</code>
 
   CImage image;
   image.Attach(hBitmap);
   image.Save("c:\\test.bmp"); // save as BMP
   image.Detach();
 
   error = GetLastError();
 
   ReleaseDC( hWnd, hDC);
}




"Go that way, really fast. If something gets in your way, turn."

GeneralRe: MFC Bitmap Help Pin
dipen.rana2-Jul-07 10:00
dipen.rana2-Jul-07 10:00 
QuestionOptimizing floating-point calculation performance with 64-bit applications? Pin
Cyrilix20-Jun-07 11:16
Cyrilix20-Jun-07 11:16 
QuestionCInternetSession question Pin
baloneyman20-Jun-07 9:51
baloneyman20-Jun-07 9:51 
QuestionAnyone know which file defines GET_X_LPARAM and GET_Y_LPARAM macros for WM_MOUSEMOVE? Pin
Cyrilix20-Jun-07 9:12
Cyrilix20-Jun-07 9:12 
AnswerRe: Anyone know which file defines GET_X_LPARAM and GET_Y_LPARAM macros for WM_MOUSEMOVE? Pin
Mark Salsbery20-Jun-07 9:30
Mark Salsbery20-Jun-07 9:30 
GeneralRe: Anyone know which file defines GET_X_LPARAM and GET_Y_LPARAM macros for WM_MOUSEMOVE? Pin
Cyrilix20-Jun-07 9:32
Cyrilix20-Jun-07 9:32 
GeneralRe: Anyone know which file defines GET_X_LPARAM and GET_Y_LPARAM macros for WM_MOUSEMOVE? Pin
Mark Salsbery20-Jun-07 9:38
Mark Salsbery20-Jun-07 9:38 
QuestionIt has been 10 years! Pin
mrw_houston20-Jun-07 8:59
mrw_houston20-Jun-07 8:59 
AnswerRe: It has been 10 years! Pin
Cyrilix20-Jun-07 9:21
Cyrilix20-Jun-07 9:21 
AnswerRe: It has been 10 years! [modified] Pin
Mark Salsbery20-Jun-07 9:34
Mark Salsbery20-Jun-07 9:34 
GeneralRe: It has been 10 years! Pin
Stephen Hewitt20-Jun-07 13:55
Stephen Hewitt20-Jun-07 13:55 
GeneralRe: It has been 10 years! Pin
Mark Salsbery20-Jun-07 14:02
Mark Salsbery20-Jun-07 14:02 
QuestionRelease OK,Debug Error! Pin
kingliub20-Jun-07 6:21
kingliub20-Jun-07 6:21 
AnswerRe: Release OK,Debug Error! Pin
shivditya20-Jun-07 6:23
shivditya20-Jun-07 6:23 
GeneralRe: Release OK,Debug Error! Pin
kingliub20-Jun-07 6:29
kingliub20-Jun-07 6:29 
AnswerRe: Release OK,Debug Error! Pin
Cedric Moonen20-Jun-07 7:30
Cedric Moonen20-Jun-07 7:30 
GeneralRe: Release OK,Debug Error! Pin
Mark Salsbery20-Jun-07 8:55
Mark Salsbery20-Jun-07 8:55 

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.