我现在一个难题,要在一个窗体中同时显示多副图像,且还不是一次打开的,一次只能打开一个,最后将所有图像以256像素或其他格式保存到一个文件。我觉得保存应该不难,关键是把多个不一块打开且数目不确定的文件同时显示,我至今没有什么好办法,我现在可以在不同的位置显示图像,但每当显示下一个图像时,前一个将消失,一次只能显示一个,尽管不会出现位置重复,还望诸位大虾参与,并多多指教!
以下是我的关键代码:/*************************************************************************
 *
 * LoadBMP()
 *
 * Parameter:
 *
 * CPalette* pPal   - pointer to CPalette containing DIB's palette
 *
 * Return Value:
 *
 * HGLOBAL          - handle to global memory with a DIB spec
 *                    in it followed by the DIB bits
 *
 * Description:
 *
 * This function opens a BMP file, and creates a palette from 
 * a DIB by allocating memory for the logical palette,
 * reading and storing the colors from the DIB's color table
 * into the logical palette, creating a palette from this 
 * logical palette,and then returning the handle of global memory,
 * and *pPal specify the palette's handle,This allows the DIB to 
 * be displayed using the best possible colors (important for DIBs 
 * with 256 or more colors).
 *
 ************************************************************************/HGLOBAL CBitMapOpView::LoadBMP( CPalette *pPal )
{

HANDLE hFile;  
    int n;
n=Bmpname.Replace("\\", "\\\\");

    
// Open the existing file. 
 
     hFile = CreateFile(   Bmpname,   // open the BitMap file
                       GENERIC_READ,
                           0,                             //  not share 
                           NULL,                         // no security 
                           OPEN_EXISTING,                // existing file only 
                           FILE_ATTRIBUTE_NORMAL,        // normal file 
                           NULL);                        // no attr. template   //fail to open file
      if (hFile == INVALID_HANDLE_VALUE) 
  { 
  return NULL; 
  } 
     
 BITMAPFILEHEADER bmfHeader;
 DWORD nFileLen,dwBytesRead;
 nFileLen = GetFileSize (hFile, NULL) ; //get the length of the file
      // Read file header
    if(!ReadFile(hFile, (LPSTR)&bmfHeader, sizeof(bmfHeader), &dwBytesRead, NULL) ||
 dwBytesRead != sizeof(bmfHeader))
          return NULL;
      // File type should be 'BM'
     if (bmfHeader.bfType != ((WORD) ('M' << 8) | 'B'))
          return NULL;     HGLOBAL hDIB = ::GlobalAlloc(GMEM_FIXED, nFileLen);
     if (hDIB == 0)
          return NULL;     // Read the remainder of the bitmap file.
     if(!ReadFile(hFile, (LPSTR)hDIB, nFileLen - sizeof(BITMAPFILEHEADER), &dwBytesRead, NULL) ||
 dwBytesRead != nFileLen - sizeof(BITMAPFILEHEADER))
 {
 ::GlobalFree(hDIB);
 return NULL;
 }     BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;  //nColors is the factual color num
     int nColors = bmInfo.bmiHeader.biClrUsed ? 
 bmInfo.bmiHeader.biClrUsed : 1 << bmInfo.bmiHeader.biBitCount;
  // Create the palette  UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
 LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];  pLP->palVersion = 0x300;  //version number
 pLP->palNumEntries = nColors;
           
 //create color matrix
 for( int i=0; i < nColors; i++)
     {
   pLP->palPalEntry[i].peRed = bmInfo.bmiColors[i].rgbRed;
   pLP->palPalEntry[i].peGreen = bmInfo.bmiColors[i].rgbGreen;
   pLP->palPalEntry[i].peBlue = bmInfo.bmiColors[i].rgbBlue;
   pLP->palPalEntry[i].peFlags = 0;
 }  pPal->CreatePalette( pLP );
 delete[] pLP;
 CloseHandle(hFile);// Close the files.
     return hDIB;
}
/*************************************************************************
 *
 * DrawDIB()
 *
 * Parameters:
 *
 * CDC *pDC         - DC to do output to
 *
 * HGLOBAL hDIB     - handle to global memory with a DIB spec
 *                    in it followed by the DIB bits
 *
 * CPalette* pPal   - pointer to CPalette containing DIB's palette
 *
 * Return Value:
 *
 * BOOL             - TRUE if DIB was drawn, FALSE otherwise
 *
 * Description:
 *   Painting routine for a DIB.  Calls StretchDIBits() or
 *   SetDIBitsToDevice() to paint the DIB.  The DIB is
 *   output to the specified DC, at the coordinates given
 *   in lpDCRect.  The area of the DIB to be output is
 *   given by lpDIBRect.
 *
 ************************************************************************/ bool CBitMapOpView::DrawDIB( CDC* pDC, HGLOBAL hDIB, CPalette *pPal )
{
LPVOID lpDIBBits; // Pointer to DIB bits
bool bsuccess = false; //Check hDIB,if hDIB is null,exit the function
if(hDIB == NULL)
return bsuccess; BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;

//nColors is the factual color num
int nColors = bmInfo.bmiHeader.biClrUsed ? 
bmInfo.bmiHeader.biClrUsed :  1 << bmInfo.bmiHeader.biBitCount;
    
//16 bit or 24 bit or 32 bit clearly color
if( bmInfo.bmiHeader.biBitCount > 8 )
lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors +
            bmInfo.bmiHeader.biClrUsed) +
((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));

//Other color
else
lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors);
    
// select the palette into a device context and realize it 
if( pPal && (pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE) )
{
pDC->SelectPalette(pPal, FALSE);
pDC->RealizePalette();
} //draw bmp


::SetDIBitsToDevice(pDC->m_hDC, // hDC
                width, // DestX
                        height, // DestY
                        bmInfo.bmiHeader.biWidth, // nDestWidth
                        bmInfo.bmiHeader.biHeight, // nDestHeight
                        0, // SrcX
                        0, // SrcY
                        0, // nStartScan
                        bmInfo.bmiHeader.biHeight, // nNumScans
                        lpDIBBits, // lpBits
                        (LPBITMAPINFO)hDIB, // lpBitsInfo
                         DIB_RGB_COLORS); // wUsage width = width + bmInfo.bmiHeader.biWidth;
    //height = height + bmInfo.bmiHeader.biHeight;
bsuccess = true;
return bsuccess;

}

解决方案 »

  1.   

    复,还望诸位大虾参与,并多多指教!
    以下是我的关键代码:/*************************************************************************
     *
     * LoadBMP()
     *
     * Parameter:
     *
     * CPalette* pPal   - pointer to CPalette containing DIB's palette
     *
     * Return Value:
     *
     * HGLOBAL          - handle to global memory with a DIB spec
     *                    in it followed by the DIB bits
     *
     * Description:
     *
     * This function opens a BMP file, and creates a palette from 
     * a DIB by allocating memory for the logical palette,
     * reading and storing the colors from the DIB's color table
     * into the logical palette, creating a palette from this 
     * logical palette,and then returning the handle of global memory,
     * and *pPal specify the palette's handle,This allows the DIB to 
     * be displayed using the best possible colors (important for DIBs 
     * with 256 or more colors).
     *
     ************************************************************************/HGLOBAL CBitMapOpView::LoadBMP( CPalette *pPal )
    {

    HANDLE hFile;  
        int n;
    n=Bmpname.Replace("\\", "\\\\");

        
    // Open the existing file. 
     
         hFile = CreateFile(   Bmpname,   // open the BitMap file
                           GENERIC_READ,
                               0,                             //  not share 
                               NULL,                         // no security 
                               OPEN_EXISTING,                // existing file only 
                               FILE_ATTRIBUTE_NORMAL,        // normal file 
                               NULL);                        // no attr. template   //fail to open file
          if (hFile == INVALID_HANDLE_VALUE) 
      { 
      return NULL; 
      } 
         
     BITMAPFILEHEADER bmfHeader;
     DWORD nFileLen,dwBytesRead;
     nFileLen = GetFileSize (hFile, NULL) ; //get the length of the file
          // Read file header
        if(!ReadFile(hFile, (LPSTR)&bmfHeader, sizeof(bmfHeader), &dwBytesRead, NULL) ||
     dwBytesRead != sizeof(bmfHeader))
              return NULL;
          // File type should be 'BM'
         if (bmfHeader.bfType != ((WORD) ('M' << 8) | 'B'))
              return NULL;     HGLOBAL hDIB = ::GlobalAlloc(GMEM_FIXED, nFileLen);
         if (hDIB == 0)
              return NULL;     // Read the remainder of the bitmap file.
         if(!ReadFile(hFile, (LPSTR)hDIB, nFileLen - sizeof(BITMAPFILEHEADER), &dwBytesRead, NULL) ||
     dwBytesRead != nFileLen - sizeof(BITMAPFILEHEADER))
     {
     ::GlobalFree(hDIB);
     return NULL;
     }     BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;  //nColors is the factual color num
         int nColors = bmInfo.bmiHeader.biClrUsed ? 
     bmInfo.bmiHeader.biClrUsed : 1 << bmInfo.bmiHeader.biBitCount;
      // Create the palette  UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
     LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];  pLP->palVersion = 0x300;  //version number
     pLP->palNumEntries = nColors;
               
     //create color matrix
     for( int i=0; i < nColors; i++)
         {
       pLP->palPalEntry[i].peRed = bmInfo.bmiColors[i].rgbRed;
       pLP->palPalEntry[i].peGreen = bmInfo.bmiColors[i].rgbGreen;
       pLP->palPalEntry[i].peBlue = bmInfo.bmiColors[i].rgbBlue;
       pLP->palPalEntry[i].peFlags = 0;
     }  pPal->CreatePalette( pLP );
     delete[] pLP;
     CloseHandle(hFile);// Close the files.
         return hDIB;
    }
    /*************************************************************************
     *
     * DrawDIB()
     *
     * Parameters:
     *
     * CDC *pDC         - DC to do output to
     *
     * HGLOBAL hDIB     - handle to global memory with a DIB spec
     *                    in it followed by the DIB bits
     *
     * CPalette* pPal   - pointer to CPalette containing DIB's palette
     *
     * Return Value:
     *
     * BOOL             - TRUE if DIB was drawn, FALSE otherwise
     *
     * Description:
     *   Painting routine for a DIB.  Calls StretchDIBits() or
     *   SetDIBitsToDevice() to paint the DIB.  The DIB is
     *   output to the specified DC, at the coordinates given
     *   in lpDCRect.  The area of the DIB to be output is
     *   given by lpDIBRect.
     *
     ************************************************************************/ bool CBitMapOpView::DrawDIB( CDC* pDC, HGLOBAL hDIB, CPalette *pPal )
    {
    LPVOID lpDIBBits; // Pointer to DIB bits
    bool bsuccess = false; //Check hDIB,if hDIB is null,exit the function
    if(hDIB == NULL)
    return bsuccess; BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;

    //nColors is the factual color num
    int nColors = bmInfo.bmiHeader.biClrUsed ? 
    bmInfo.bmiHeader.biClrUsed :  1 << bmInfo.bmiHeader.biBitCount;
        
    //16 bit or 24 bit or 32 bit clearly color
    if( bmInfo.bmiHeader.biBitCount > 8 )
    lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors +
                bmInfo.bmiHeader.biClrUsed) +
    ((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));

    //Other color
    else
    lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors);
        
    // select the palette into a device context and realize it 
    if( pPal && (pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE) )
    {
    pDC->SelectPalette(pPal, FALSE);
    pDC->RealizePalette();
    } //draw bmp


    ::SetDIBitsToDevice(pDC->m_hDC, // hDC
                    width, // DestX
                            height, // DestY
                            bmInfo.bmiHeader.biWidth, // nDestWidth
                            bmInfo.bmiHeader.biHeight, // nDestHeight
                            0, // SrcX
                            0, // SrcY
                            0, // nStartScan
                            bmInfo.bmiHeader.biHeight, // nNumScans
                            lpDIBBits, // lpBits
                            (LPBITMAPINFO)hDIB, // lpBitsInfo
                             DIB_RGB_COLORS); // wUsage width = width + bmInfo.bmiHeader.biWidth;
        //height = height + bmInfo.bmiHeader.biHeight;
    bsuccess = true;
    return bsuccess;

    }
      

  2.   

    你先看看DIB头文件,自己重新生成一个新文件。把所有的图片数据写进来。