Click here to Skip to main content
15,923,006 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
QuestionConversion to MC++??? Pin
ydasari25-Aug-02 7:14
ydasari25-Aug-02 7:14 
AnswerRe: Conversion to MC++??? Pin
Nick Hodapp25-Aug-02 11:40
sitebuilderNick Hodapp25-Aug-02 11:40 
GeneralRe: Conversion to MC++??? Pin
Anonymous25-Aug-02 11:52
Anonymous25-Aug-02 11:52 
GeneralRe: Conversion to MC++??? Pin
Nick Hodapp25-Aug-02 18:08
sitebuilderNick Hodapp25-Aug-02 18:08 
Questionremove this ambiguity? Pin
imran_rafique25-Aug-02 1:49
imran_rafique25-Aug-02 1:49 
AnswerRe: remove this ambiguity? Pin
Nick Hodapp25-Aug-02 2:53
sitebuilderNick Hodapp25-Aug-02 2:53 
QuestionProcess related question? Pin
Anonymous25-Aug-02 1:33
Anonymous25-Aug-02 1:33 
General32 Bitmaps -> 24 Dibs -> .bmp File Pin
Swinefeaster24-Aug-02 20:48
Swinefeaster24-Aug-02 20:48 
Grrr... I have done so much bitmap work that I feel embarassed even asking this! Smile | :)

But I'm stuck!

I create a ddb on a 32 bits per pixel display. Now I want to save this as a 24 bit bitmap --- rather than a 32 bit bitmap (which I can do just fine).

I'm using the dib api functions provided by microsoft (slighty modified), namely this function:

<br />
HDIB WINAPI CreateDibFromBitmap(HBITMAP hBitmap, HPALETTE hPal, <br />
 int bitsPerPixel)<br />
{<br />
   HDIB hDIB = NULL;<br />
<br />
   BITMAP bm;                   // bitmap structure<br />
   BITMAPINFOHEADER bi;         // bitmap header<br />
   BITMAPINFOHEADER FAR *lpbi = NULL;  // pointer to BITMAPINFOHEADER<br />
   DWORD dwLen = 0;                 // size of memory block<br />
   HDIB h = NULL;              // handle to DIB, temp handle<br />
   HDC hDC = NULL;                     // handle to DC<br />
   WORD biBits = 0;                 // bits per pixel<br />
<br />
   /* check if bitmap handle is valid */<br />
<br />
   if (!hBitmap)<br />
      return NULL;<br />
<br />
   /* fill in BITMAP structure, return NULL if it didn't work */<br />
   if (!GetObject(hBitmap, sizeof(bm), (LPSTR)&bm))<br />
      return NULL;<br />
<br />
   /* if no palette is specified, use default palette */<br />
   if (hPal == NULL)<br />
      hPal = (HPALETTE)::GetStockObject(DEFAULT_PALETTE);<br />
<br />
   if(bitsPerPixel == 0)<br />
   {<br />
      /* calculate bits per pixel */<br />
      biBits = bm.bmPlanes * bm.bmBitsPixel;<br />
   }<br />
   else<br />
   {<br />
      // Use specified bits per pixel.<br />
      biBits = bitsPerPixel;<br />
   }<br />
<br />
   /* make sure bits per pixel is valid */<br />
   if (biBits <= 1)<br />
      biBits = 1;<br />
   else if (biBits <= 4)<br />
      biBits = 4;<br />
   else if (biBits <= 8)<br />
      biBits = 8;<br />
   else /* if greater than 8-bit, force to 24-bit */<br />
      biBits = 24;<br />
<br />
   /* initialize BITMAPINFOHEADER */<br />
   bi.biSize = sizeof(BITMAPINFOHEADER);<br />
   bi.biWidth = bm.bmWidth;<br />
   bi.biHeight = bm.bmHeight;<br />
   bi.biPlanes = 1;<br />
   bi.biBitCount = biBits;<br />
   bi.biCompression = BI_RGB;<br />
   bi.biSizeImage = 0;<br />
   bi.biXPelsPerMeter = 0;<br />
   bi.biYPelsPerMeter = 0;<br />
   bi.biClrUsed = 0;<br />
   bi.biClrImportant = 0;<br />
<br />
   /* calculate size of memory block required to store BITMAPINFO */<br />
   dwLen = bi.biSize + PaletteSize((LPSTR)&bi);<br />
<br />
   /* get a DC */<br />
   hDC = GetDC(NULL);<br />
<br />
   /* select and realize our palette */<br />
   hPal = SelectPalette(hDC, hPal, FALSE);<br />
   RealizePalette(hDC);<br />
<br />
   /* alloc memory block to store our bitmap */<br />
   hDIB = (HDIB)GlobalAlloc(GHND, dwLen);<br />
<br />
   /* if we couldn't get memory block */<br />
   if (!hDIB)<br />
   {<br />
      /* clean up and return NULL */<br />
      SelectPalette(hDC, hPal, TRUE);<br />
      RealizePalette(hDC);<br />
      ReleaseDC(NULL, hDC);<br />
      return NULL;<br />
   }<br />
<br />
   /* lock memory and get pointer to it */<br />
   lpbi = (BITMAPINFOHEADER*)GlobalLock(hDIB);<br />
<br />
   /* use our bitmap info. to fill BITMAPINFOHEADER */<br />
   *lpbi = bi;<br />
<br />
   /*  call GetDIBits with a NULL lpBits param, so it will calculate the<br />
    *  biSizeImage field for us<br />
    */<br />
   GetDIBits(hDC, hBitmap, 0, (WORD)bi.biHeight, NULL, (LPBITMAPINFO)lpbi,<br />
         DIB_RGB_COLORS);<br />
<br />
   /* get the info. returned by GetDIBits and unlock memory block */<br />
   bi = *lpbi;<br />
   GlobalUnlock(hDIB);<br />
<br />
   /* if the driver did not fill in the biSizeImage field, make one up */<br />
   if (bi.biSizeImage == 0)<br />
      bi.biSizeImage = WIDTHBYTES((DWORD)bm.bmWidth * biBits) * bm.bmHeight;<br />
<br />
   /* realloc the buffer big enough to hold all the bits */<br />
   dwLen = bi.biSize + PaletteSize((LPSTR)&bi) + bi.biSizeImage;<br />
   if (h = (HDIB)GlobalReAlloc(hDIB, dwLen, 0))<br />
      hDIB = h;<br />
   else<br />
   {<br />
      /* clean up and return NULL */<br />
      GlobalFree(hDIB);<br />
      hDIB = NULL;<br />
      SelectPalette(hDC, hPal, TRUE);<br />
      RealizePalette(hDC);<br />
      ReleaseDC(NULL, hDC);<br />
      return NULL;<br />
   }<br />
<br />
   /* lock memory block and get pointer to it */<br />
   lpbi = (BITMAPINFOHEADER*)GlobalLock(hDIB);<br />
<br />
   /*  call GetDIBits with a NON-NULL lpBits param, and actualy get the<br />
    *  bits this time<br />
    */<br />
   int result = GetDIBits(hDC, hBitmap, 0, (WORD)bi.biHeight,<br />
    (LPSTR)lpbi + (WORD)lpbi->biSize + PaletteSize((LPSTR)lpbi), <br />
    (LPBITMAPINFO)lpbi, DIB_RGB_COLORS);<br />
<br />
   if(result == 0)<br />
   {<br />
      /* clean up and return NULL */<br />
      GlobalUnlock(hDIB);<br />
      hDIB = NULL;<br />
      SelectPalette(hDC, hPal, TRUE);<br />
      RealizePalette(hDC);<br />
      ReleaseDC(NULL, hDC);<br />
      return NULL;<br />
   }<br />
   bi = *lpbi;<br />
<br />
   /* clean up */<br />
   GlobalUnlock(hDIB);<br />
   SelectPalette(hDC, hPal, TRUE);<br />
   RealizePalette(hDC);<br />
   ReleaseDC(NULL, hDC);<br />
<br />
   /* return handle to the DIB */<br />
   return hDIB;<br />
} <br />


I pass in the bitmap handle, NULL for the palette, and then 24 for the bitsPerPixel. But the second call to GetDIBits() fails with a return result of 0.

Does anyone know why?

Thanks for your time.

Cheers,

swine

Check out Aephid Photokeeper, the powerful digital
photo album solution at www.aephid.com.
Generalmoving labels Pin
smooth24-Aug-02 20:44
smooth24-Aug-02 20:44 
GeneralRemoting in VC++ Pin
Harvail24-Aug-02 1:49
Harvail24-Aug-02 1:49 
Generalplz see this error Pin
imran_rafique23-Aug-02 9:12
imran_rafique23-Aug-02 9:12 
QuestionWhy an error?? Pin
ydasari22-Aug-02 14:45
ydasari22-Aug-02 14:45 
AnswerRe: Why an error?? Pin
Nish Nishant22-Aug-02 15:45
sitebuilderNish Nishant22-Aug-02 15:45 
Questioncom? Pin
imran_rafique22-Aug-02 11:31
imran_rafique22-Aug-02 11:31 
Questionhow to convert intptr into hwnd Pin
imran_rafique22-Aug-02 0:28
imran_rafique22-Aug-02 0:28 
AnswerRe: how to convert intptr into hwnd Pin
Firoz22-Aug-02 6:58
Firoz22-Aug-02 6:58 
Generalwaiting for ur reply Pin
imran_rafique21-Aug-02 23:02
imran_rafique21-Aug-02 23:02 
GeneralVC++ .NET BOOK ! Pin
Hadi Rezaee20-Aug-02 12:11
Hadi Rezaee20-Aug-02 12:11 
GeneralRe: VC++ .NET BOOK ! Pin
TigerNinja_3-Sep-02 8:11
TigerNinja_3-Sep-02 8:11 
GeneralRe: VC++ .NET BOOK ! Pin
Hadi Rezaee3-Sep-02 8:24
Hadi Rezaee3-Sep-02 8:24 
Generalexecuting activeX control app in .Net framework Pin
ydasari20-Aug-02 7:22
ydasari20-Aug-02 7:22 
GeneralRe: executing activeX control app in .Net framework Pin
Nish Nishant20-Aug-02 17:48
sitebuilderNish Nishant20-Aug-02 17:48 
GeneralProffesional vs Enterprise !!! Pin
Hadi Rezaee20-Aug-02 5:11
Hadi Rezaee20-Aug-02 5:11 
GeneralRe: Proffesional vs Enterprise !!! Pin
David Cunningham20-Aug-02 6:18
cofounderDavid Cunningham20-Aug-02 6:18 
GeneralRe: Proffesional vs Enterprise !!! Pin
Hadi Rezaee20-Aug-02 8:41
Hadi Rezaee20-Aug-02 8:41 

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.