怎样把多个位图存入一个新文件中去,这些位图原本不属于一个文件,我现在已经通过链表把它们同时显示在窗体中,我开始试着把这些位图一个一个写入新文件中去,但结果该文件中只有第一个写入的位图,还众位前辈、兄台不吝赐教,小弟这厢有礼了!

解决方案 »

  1.   

    我的目的就是把显示在一个窗体中的多个位图合并成一幅,然后写入一个新建的BMP文件中去。再次打开此新文件,应把保存前的位图显示出来。如哪位兄台有办法,还望多多指教,在下不胜感激!
      

  2.   

    我也正在写一个类似的程序,我觉得可以先把这些位图画到一个大的位图中去,然后把它写入一个bmp文件,我现在已有把一个位图写如bmp文件的程序,关键是怎样把多个BMP图像写入一个BMP图像,如有哪位高手知道,还望告诉在下!我将时时关注此题!
      

  3.   

    我现在在WriteDIBList 函数中使用setDIBits 函数得到了一个HBITMAP对象,由于WriteDIBList 是全局函数,我不能看到效果,但我使用WriteDIB(把位图保存到文件中去),画不出图来,代码如下:
        // WriteDIB - Writes a DIB to file
    // Returns - TRUE on success
    // szFile - Name of file to write to
    // hDIB - Handle of the DIB
    BOOL WriteDIB(  HGLOBAL hDIB)
    {
    BITMAPFILEHEADER hdr;
    LPBITMAPINFOHEADER lpbi;
    HANDLE hFile;
    DWORD dwBytesWritten; if (!hDIB)
    return FALSE; if(n == 0)
    {
    n = saveFileName.Replace("\\", "\\\\");
    } // Open the existing file. 
     
    hFile = CreateFile(   saveFileName,               // open the BitMap file
                           GENERIC_WRITE,          //open for writing
                               0,                      //  not share 
                               NULL,                   // no security 
                               CREATE_ALWAYS,          // Creates a new file 
                               FILE_ATTRIBUTE_NORMAL,  // normal file 
                               NULL);                  // no attr. template 
       //fail to open file
        if (hFile == INVALID_HANDLE_VALUE) 

    return false; 
    }  lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB); if(!lpbi)
    {
    CloseHandle(hFile);
    return false;
    } int nColors = 1 << lpbi->biBitCount; // Fill in the fields of the file header
    hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
    hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );
    hdr.bfReserved1 = 0;
    hdr.bfReserved2 = 0;
    hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize +
    nColors * sizeof(RGBQUAD));

    // Write the file header
    WriteFile(hFile, &hdr, sizeof(hdr), 
                &dwBytesWritten, NULL); 
            
    // Write the DIB header and the bits
    WriteFile(hFile, lpbi, GlobalSize(hDIB), 
                &dwBytesWritten, NULL); 
        
        GlobalUnlock(hDIB);
    CloseHandle(hFile);
    return TRUE;
    }
    //save the diblist to a Bmp file
    void writeDIBList(linklist L)
    {
    HGLOBAL hdib;
    LPVOID lpv;
    int startScan = 0;
    int nTotalLines = 0;
    int nScanLines;
    HDC hSrcdc, hMemDC;
        HBITMAP hbmp, holdbmp; hSrcdc = ::CreateDC("DISPLAY", NULL, NULL, NULL);
    hbmp = ::CreateCompatibleBitmap(hSrcdc, width, height + maxheight);

    while (L != NULL)
    {
    if(L->hdib == NULL)
    {
    return;
    }        BITMAPINFO &bmInfo = *(LPBITMAPINFO)L->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 )
        lpv = (LPVOID)((LPDWORD)(bmInfo.bmiColors +
                bmInfo.bmiHeader.biClrUsed) +
    ((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));

        //Other color
        else
    lpv = (LPVOID)(bmInfo.bmiColors + nColors);        startScan = startScan + nTotalLines;

    nScanLines = ::SetDIBits(hSrcdc,
                       hbmp,
       startScan,
       bmInfo.bmiHeader.biHeight,
       lpv,
       &bmInfo,
       DIB_RGB_COLORS); nTotalLines = nTotalLines + nScanLines;     L = L->next;
    } hMemDC = ::CreateCompatibleDC(hSrcdc);
    holdbmp = (HBITMAP)::SelectObject(hMemDC, hbmp);
    //L = NULL; if(!::BitBlt(hMemDC, 0, 0, width, height + maxheight, hSrcdc, 0, 0, SRCCOPY))
    return; hbmp = (HBITMAP)::SelectObject(hMemDC, holdbmp);

    ::DeleteDC(hSrcdc);
    ::DeleteDC(hMemDC);    hdib = (HGLOBAL) hbmp;
    WriteDIB(hdib);

    }
     还望众位大虾多多指教!
      

  4.   

    思路:将多幅图像画到一个DC上面,然后从CDC生成一个DIB,然后保存就可以了。
      

  5.   

    多谢二位,我可以实现将多幅图像画到一个DC上面,可我不知道怎样从CDC生成一个DIB,这已经成了本题的瓶颈,还望众位兄台多多指教,在下洗耳恭听!!
      

  6.   

    我觉得当画到DC上后就成了DDB了,也就是如何将DDB转为DIB然后保存的问题。你可以看看vc关于图象处理方面的书,上面应该都有这种程序的。
      

  7.   

    我的代码:
    //save the diblist to a Bmp file
    //save the diblist to a Bmp file
    void writeDIBList(linklist L, CDC *pdc)
    {
    HGLOBAL hdib;
    CDC memdc;
    HBITMAP hbmp; 
    CRect rect(0, 0, width, height + maxheight);

        memdc.CreateCompatibleDC(pdc);
        DrawDIBList(rect, &memdc, L );//将保存在L中的位图画到memdc中去
    hbmp = ::CreateCompatibleBitmap(memdc.m_hDC, width, height + maxheight);//获得memdc中位图的句柄
    ::DeleteDC(memdc);
    hdib = BitmapToDIB(hbmp, NULL);//将位图转换为DIB格式
    WriteDIB(hdib);//将位图写入文件}上叙代码可能是从内存中获得hbmp有问题,当我再次打开它时,没有显示,还望众位大虾能提点在下,为盼!