在VC 6.0中,写一个读取bmp位图文件的程序,输出文件的高和宽,同时在位图中留下你修改过的痕迹—如在图中加入一条白色的线段等。

解决方案 »

  1.   

    HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, "c:\\test.bmp", LR_LOADFROMFILE, IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
    BIMTAP bmp;
    GetObject(hBitmap, sizeof(BITMAP), &bmp);// Get the width and height of the bitmap
    int nWidth = bmp.bmWidth;
    int nHeight = bmp.bmHeight;// Add a line in the center of the bitmap
    HDC hdc = GetDC(NULL);
    HDC memdc = CreateCompatibleDC(hdc);
    HBITMAP hOldBitmap = (HBITMAP)SelectObject(memdc, hBitmap);
    SelectObject(memdc, hOldBitmap);HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255,255,255));
    HPEN hOldPen = (HPEN)SelectObject(memdc, hPen);
    MoveTo(memdc, 0, nHeight/2);
    LineTo(memdc, nWidth, nHeight/2);
    SelectObject(memdc, hOldPen);ReleaseDC(NULL, hdc);
    DeleteDC(memdc);
      

  2.   

    If you want to save the modified bitmap, using the code to create a bitmap info header as below:
    Note: insert the code between SelectObject(memdc, hOldPen) and ReleaseDC(NULL, hdc);
    =============================================================== PBITMAPINFO pbmi;
    WORD nClrBits; nClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel); if (nClrBits < 24)
    {
    pbmi = (PBITMAPINFO)::LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER) + 
    sizeof(RGBQUAD) * (1<<nClrBits));
    }
    else
    {
    pbmi = (PBITMAPINFO)::LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER));
    } pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    pbmi->bmiHeader.biWidth = bmp.bmWidth;
    pbmi->bmiHeader.biHeight = bmp.bmHeight;
    pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
    pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
    if (nClrBits < 24)
    pbmi->bmiHeader.biClrUsed = (1 << nClrBits); pbmi->bmiHeader.biCompression = BI_RGB; pbmi->bmiHeader.biSizeImage = ( (bmp.bmWidth * nClrBits + 31) & ~31 ) / 8 * bmp.bmHeight;
    pbmi->bmiHeader.biClrImportant = 0; HANDLE hf; // file handle
    BITMAPFILEHEADER hdr; // bitmap file-header
    PBITMAPINFOHEADER pbih; // bitmap info-header LPBYTE lpBits; // memory pointer
    DWORD dwTmp; pbih = (PBITMAPINFOHEADER)pbmi;
    lpBits = (LPBYTE)::GlobalAlloc(GMEM_FIXED, pbih->biSizeImage); if (!lpBits)
    {
    TRACE("Memory space is not enough to create bitmap!\n");
    return;
    } if (!GetDIBits(memdc, hBitmap, 0, pbih->biHeight, lpBits, pbmi, DIB_RGB_COLORS))
    {
    TRACE("Fail to retrieve the bits of the specified bitmap!\n");
    return;
    }// pszFile is file name what you want to save
    hf = ::CreateFile(pszFile, GENERIC_READ | GENERIC_WRITE, 0, NULL, 
    CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL);
    hdr.bfType = 0x4d42; // 0x4d = 'M', 0x42 = 'B'
    hdr.bfSize = sizeof(BITMAPFILEHEADER) + pbih->biSizeImage + pbih->biClrUsed * sizeof(RGBQUAD);
    hdr.bfOffBits = sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof(RGBQUAD);
    // bitmap file-header
    WriteFile(hf, &hdr, sizeof(BITMAPFILEHEADER), &dwTmp, NULL);
    // bitmap info-header
    WriteFile(hf, pbih, pbih->biSize + pbih->biClrUsed * sizeof(RGBQUAD), &dwTmp, NULL);
    // the array of color indicates into .BMP file.
    WriteFile(hf, lpBits, pbih->biSizeImage, &dwTmp, NULL); ::CloseHandle(hf); // Free memory
    ::GlobalFree((HGLOBAL)lpBits);
      

  3.   

    Thank you very much.You are smart!
      

  4.   

    I am not good at C++,can you give me a whole program which includ the Class and Head
    file,ok?  I will thanks again.
      

  5.   

    程序已打包,下载地址:
    http://www.geekclaw.com/changebitmap.rar
      

  6.   

    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images。目前我知道的,开发源码中最好的图像处理工具。download:
    http://code.knowsky.com/down/5626.html
      

  7.   

    yjgx007:您好,非常谢谢您提供的答案.但是我发现运行的时候还是有错误,是怎么回事呢?
    错误如下:1.c:\documents and settings\owner\my documents\my qq files\changebitmap.cpp(100) : error C2664: 'DialogBoxParamA' : cannot convert parameter 4 from 'long (struct HWND__ *,unsigned int,unsigned int,long)' to 'int (__stdcall *)(struct HWND__ *,unsigned 
           2.c:\documents and settings\administrator\桌面\新建文件夹\changebitmap1.cpp(5) : fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory
    执行 cl.exe 时出错.