Display two 256 color images in one view.

My problem is as follows. I am trying to display two gif images onto the same view. The problems arise when my graphics adapter is at 256 colors.

Inserting one image onto the view is o.k. The gif seems fine. When I add another image to the view the palette of image 1 get trampled over by the palette of image 2.

I can have a number of gifs onscreen and display each of them in turn by calling the following function :

BOOL CGifObject::DisplayGif(CDC* pCDC,int x,int y)
      if (!GIFLoaded) return FALSE;

      CPalette *oldpal;
          oldpal = pCDC->SelectPalette(&pal,TRUE);

      if (!hBMP)
            hBMP = CreateDIBitmap(pCDC->m_hDC,(LPBITMAPINFOHEADER)&bmi->bmiHeader,CBM_INIT,
      if (Transparent)
            DrawTransparentBitmap(pCDC->m_hDC, hBMP, x,y,
               CDC hdcTemp;
               HBITMAP oldbmp = (HBITMAP)hdcTemp.SelectObject(hBMP);
               // destructor will delete DC

      // 1. pCDC->RealizePalette();
      return TRUE;

At point 1. I tried to realize the old palette again but that didn't work. Even if only one image is displayed all the colors are screwed up.

I have encountered the same problem with bmp's. Here is the code for the DisplayBMP function :

BOOL CGifObject::DisplayBMP(CDC *pDC, int x, int y)
      BITMAP                   bm;
      HBITMAP             hbmp;
      CBitmap                  *oldBmp;                        
      CDC                    memDC;
      //Test to see if image to display
      if (!BMPLoaded || pDC == NULL || m_hbits==NULL || m_hhdr==NULL)
            return FALSE ;
      CPalette *oldPal = NULL;

      if (m_cPal)            // we have a palette wrt the image
            oldPal = pDC->SelectPalette(m_cPal,TRUE);

      m_pbits =  (char *)GlobalLock(m_hbits);    
      m_pHdr  =  (LPSTR)GlobalLock(m_hhdr);
      m_bmi   =  (BITMAPINFO*) m_pHdr;                   //Create a pointer to a  BITMAP info structure
      m_bmih  =  (BITMAPINFOHEADER *) m_pHdr;     //Create a pointer to a BITMAPINFOHEADER structure
      //Create a handle to the BMP
      hbmp = CreateDIBitmap(pDC->m_hDC, m_bmih, CBM_INIT, m_pbits, m_bmi, DIB_RGB_COLORS);                                 

      CBitmap *bmp = CBitmap::FromHandle(hbmp);
      GetObject(hbmp, sizeof(BITMAP), (LPVOID)&bm);
      oldBmp = (CBitmap*)memDC.SelectObject(bmp);
      //Draw bitmap on screen
      pDC->BitBlt(x,y,cx,cy,&memDC,xSrcOff,ySrcOff,SRCCOPY) ;
      //Reset device
      //Delete device
      //Reset palette
      if (oldPal)

      return 1;      

Is this the same problem???

Can somebody help me with the palette manager before I tear my hair out!!!

Thanx, Gordon.
Who is Participating?
chensuConnect With a Mentor Commented:
Since each individual image has its own palette, the quality of the images have to be compromised. You may create a halftone palette (CPalette::CreateHalftonePalette) and use it for all the images.

Check out the following GDI Technical Articles in the MSDN Library.

- Palette Awareness
- The Palette Manager: How and Why
- Using DIBs with Palettes
jeffreygAuthor Commented:
Thanks, the HalftonePalette worked. I now can display multiple images with slight, but satisfactory degradation.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.