Hbitmap/BITMAP转换成.bmp文件

C++: Hbitmap/BITMAP into .bmp file

本文关键字:bmp 文件 转换 BITMAP Hbitmap      更新时间:2023-10-16

好的,整个故事是,我试图使用c++中的Leptonica+Tesseract OCR来截图,将其保存为*.bmp文件,然后将其加载回OCR。我不需要经常这样做,但由于我似乎无法将截图数据直接复制到Leptonica PIX结构中,所以我需要先将其保存到一个文件中……实际上,最好有一个解决方案。

这是我在网上找到的一些代码,试图帮助我。

屏幕帽:

HBITMAP ScreenCapture(){
  int width=100;
  int height=100;
  // get the device context of the screen
  HDC hScreenDC = CreateDC(L"DISPLAY", NULL, NULL, NULL);     
  // and a device context to put it in
  HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
  int x = GetDeviceCaps(hScreenDC, HORZRES);
  int y = GetDeviceCaps(hScreenDC, VERTRES);
  // maybe worth checking these are positive values
  HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, x, y);
  // get a new bitmap
  HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, hBitmap);
  BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);
  hBitmap = (HBITMAP)SelectObject(hMemoryDC, hOldBitmap);
  //GlobalAlloc(GPTR, hBitmap)
  WriteDIB(L"test.bmp", (HGLOBAL)hBitmap);
  // clean up
  DeleteDC(hMemoryDC);
  DeleteDC(hScreenDC);
  return hBitmap;
  // now your image is held in hBitmap. You can save it or do whatever with it
}

尝试写函数:

BOOL WriteDIB( LPTSTR szFile, HANDLE hDIB)
{
  cout<<endl<<"Running save function";
  /*HANDLE hDIB=GlobalAlloc(GPTR, sizeof(hDIBtochange));//this doesn't work, the result is four. Also the HANDLE parameter's name would be changed to hDIBtochange, so that the rest of the function uses the old 'hDIB' throughout
  cout<<endl<<sizeof(hDIBtochange);*/
  BITMAPFILEHEADER  hdr;
  LPBITMAPINFOHEADER    lpbi;
  if (!hDIB)
    return FALSE;
  CFile file;
  if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) )
    return FALSE;
  lpbi = (LPBITMAPINFOHEADER)hDIB;
  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 
  file.Write( &hdr, sizeof(hdr) );
  // Write the DIB header and the bits 
  file.Write( lpbi, GlobalSize(hDIB) );
  return TRUE;
}
多年来无耻地抄袭别人的帖子。好的!我面临的问题是,我似乎无法理解如何将Hbitmap全局分配为全局可访问的句柄,可以转换或使用LPBITMAPINFOHEADER。一旦lpbi被创建,在Visual Studio 2012调试中,它里面的每个字段都是"无法读取内存"错误。虽然已经创建,但是无法访问。

解决方案. .直接从屏幕截图到PIX,在内存里面…找到一种方法保存到位图,并定期创建它们以供阅读。寻找另一种更有意义的方式……

更喜欢第一个,但是,我在这个问题上要求一个解决方案,而不是第二个……或第三个。

如果你需要更多的信息,我可以尽量提供。这主要归结为"我以前从来没有做过这样的代码,我的课上也没有教过,所以我试着边做边学"。

保存HBITMAP到文件的更简单的方法是使用GDI+。这样做的好处是,你可以将图片保存为windows支持的任何格式,同时使你从各种各样的图片格式中解脱出来,甚至不必去理解它们。

在下面的例子中,我只是使用了LoadImage作为一种快速而肮脏的方式来加载预先存在的图像—您可以简单地使用已经捕获的HBITMAP。

下面是加载位图并再次保存它的示例。(我最初使用"image/png"作为输出类型,以及适当的输出文件名)

#include <windows.h>
#include <gdiplus.h>
using namespace Gdiplus;
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
   UINT  num = 0;          // number of image encoders
   UINT  size = 0;         // size of the image encoder array in bytes
   ImageCodecInfo* pImageCodecInfo = NULL;
   GetImageEncodersSize(&num, &size);
   if(size == 0)
      return -1;  // Failure
   pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
   if(pImageCodecInfo == NULL)
      return -1;  // Failure
   GetImageEncoders(num, size, pImageCodecInfo);
   for(UINT j = 0; j < num; ++j)
   {
      if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
      {
         *pClsid = pImageCodecInfo[j].Clsid;
         free(pImageCodecInfo);
         return j;  // Success
      }
   }
   free(pImageCodecInfo);
   return -1;  // Failure
}
int main()
{
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    HBITMAP hBitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL), "babe.bmp", IMAGE_BITMAP, 0,0, LR_LOADFROMFILE);
    Bitmap *image = new Bitmap(hBitmap, NULL);
    CLSID myClsId;
    int retVal = GetEncoderClsid(L"image/bmp", &myClsId);
    image->Save(L"output.bmp", &myClsId, NULL);
    delete image;
    GdiplusShutdown(gdiplusToken);
    return 0;
}

我最近不得不做同样的事情,并成功地使用了GlobalAlloc。
这段代码的基础来自这篇MSDN文章。

看起来你从这里得到了你的示例代码。

MSDN对于win32操作来说是非常可靠的,在我的经验中绝对比其他站点更喜欢它。

似乎发生的是sizeof(hDIBtochange)返回4,所以您只分配了4字节的内存。这不足以容纳pbi结构。

这是我的代码与GlobalAlloc,希望将显示正确的用法。

void
WriteBmpTofile(const bool remote, LPSTR pszFile, PBITMAPINFO pbi, HBITMAP hBmp, HDC hDC)
{
    HANDLE hFile;
    BITMAPFILEHEADER hdr;
    PBITMAPINFOHEADER pbih;
    LPBYTE lpBits;
    DWORD dwTemp;
    pbih = (PBITMAPINFOHEADER)pbi;
    lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
    if(!lpBits)
    {
        return; // could not allocate bitmap
    }
    GetDIBits(hDC, hBmp, 0, (WORD)pbih->biHeight, lpBits, pbi, DIB_RGB_COLORS);
    hFile = CreateFile(pszFile,
                        GENERIC_READ | GENERIC_WRITE,
                        0,
                        NULL,
                        CREATE_ALWAYS,
                        FILE_ATTRIBUTE_NORMAL,
                        NULL);
    if(hFile == INVALID_HANDLE_VALUE)
    {
        return; // Could not open screenshot file
    }
    // type == BM
    hdr.bfType = 0x4d42;
    hdr.bfSize = (sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof(RGBQUAD) + pbih->biSizeImage);
    hdr.bfReserved1 = 0;
    hdr.bfReserved2 = 0;
    hdr.bfOffBits = sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof(RGBQUAD);
    // write the bitmap file header to file
    WriteFile(hFile, (LPVOID)&hdr, sizeof(BITMAPFILEHEADER), &dwTemp, NULL);
    // write the bitmap header to file
    WriteFile(hFile, (LPVOID)pbih, sizeof(BITMAPINFOHEADER) + pbih->biClrUsed * sizeof(RGBQUAD), &dwTemp, NULL);
    // copy the bitmap colour data into the file
    WriteFile(hFile, (LPSTR)lpBits, pbih->biSizeImage, &dwTemp, NULL);
    CloseHandle(hFile);
    GlobalFree((HGLOBAL)lpBits);
}

这是在MSDN文章的顶部函数,如果你需要它(再次由我修改)。

PBITMAPINFO
Print::CreateBitmapInfo(HBITMAP hBmp)
{
    BITMAP bmp;
    PBITMAPINFO pbmi;
    GetObject(hBmp, sizeof(BITMAP), &bmp);
    pbmi = static_cast<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; // we are assuming that there is only one plane
    pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
    // no compression this is an rgb bitmap
    pbmi->bmiHeader.biCompression = BI_RGB;
    // calculate size and align to a DWORD (8bit), we are assuming there is only one plane.
    pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * bmp.bmBitsPixel +31) & -31) * pbmi->bmiHeader.biHeight;
    // all device colours are important
    pbmi->bmiHeader.biClrImportant = 0;
    return pbmi;
}

我猜你从这里得到了你的代码存储图像。前段时间,我不得不修改代码以兼容WinCE 5.0和WinCE 6.0。这是beta样本,虽然有点乱。它没有使用GlobalAlloc。它使用CreateDibSection。

int CreateBMPFile(HWND hwnd, LPCTSTR pszFile, PBITMAPINFO pbi, 
                  HBITMAP hBMP, HDC hDC) 
 { 
    HANDLE hf;                  // file handle  
    BITMAPFILEHEADER hdr;       // bitmap file-header  
    PBITMAPINFOHEADER pbih;     // bitmap info-header  
    //LPBYTE lpBits;            // memory pointer  
    DWORD dwTotal;              // total count of bytes  
    DWORD cb;                   // incremental count of bytes  
    BYTE *hp;                   // byte pointer  
    DWORD dwTmp; 
    int ret = 0;

    pbi = CreateBitmapInfoStruct(NULL, hBMP);
    if(pbi == NULL)
    {
        return ret;
    }
    pbih = (PBITMAPINFOHEADER) pbi; 
    /*
    lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
    if (!lpBits) 
    {
         //errhandler("GlobalAlloc", hwnd); 
        return;
    }
    */
    RGBQUAD *rgbq;
    rgbq = pbi->bmiColors;
    PALETTEENTRY pe[256];
    GetSystemPaletteEntries(hDC, 0, pbih->biClrUsed, pe);
    for(DWORD i = 0; i < pbih->biClrUsed; i++)
    {
        rgbq[i].rgbRed = pe[i].peRed;
        rgbq[i].rgbBlue = pe[i].peBlue;
        rgbq[i].rgbGreen = pe[i].peGreen;
        rgbq[i].rgbReserved = 0;
    }
    // CE5.0 + CE6.0
    HDC tHDC;
    tHDC = CreateCompatibleDC(hDC);
    HBITMAP h = CreateDIBSection(hDC, pbi, DIB_PAL_COLORS, (void **)&hp, NULL, 0);
    if(h == NULL)
    {
        goto close_bmp;
    }
    SelectObject(tHDC, h);
    BitBlt(tHDC, 0, 0, SCREEN_W, SCREEN_H, hDC, 0, 0, SRCCOPY);
    /*
    // Retrieve the color table (RGBQUAD array) and the bits  
    // (array of palette indices) from the DIB.  
    if (!GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi, 
        DIB_RGB_COLORS)) 
    {
        //errhandler("GetDIBits", hwnd); 
        return;
    }
    */
    // Create the .BMP file.  
    hf = CreateFile(pszFile, 
                   GENERIC_READ | GENERIC_WRITE, 
                   (DWORD) 0, 
                    NULL, 
                   CREATE_ALWAYS, 
                   FILE_ATTRIBUTE_NORMAL, 
                   (HANDLE) NULL); 
    if (hf == INVALID_HANDLE_VALUE) 
    {
        //errhandler("CreateFile", hwnd); 
        goto close_bmp;
    }
    hdr.bfType = 0x4d42;        // 0x42 = "B" 0x4d = "M"  
    // Compute the size of the entire file.  
    hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + 
                 pbih->biSize + pbih->biClrUsed 
                 * sizeof(RGBQUAD) + pbih->biSizeImage); 
    hdr.bfReserved1 = 0; 
    hdr.bfReserved2 = 0; 
    // Compute the offset to the array of color indices.  
    hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + 
                    pbih->biSize + pbih->biClrUsed 
                    * sizeof (RGBQUAD); 
    // Copy the BITMAPFILEHEADER into the .BMP file.  
    if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), 
        (LPDWORD) &dwTmp,  NULL)) 
    {
       //errhandler("WriteFile", hwnd); 
        goto close_bmp;
    }
    // Copy the BITMAPINFOHEADER and RGBQUAD array into the file.  
    if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) 
                  + pbih->biClrUsed * sizeof (RGBQUAD), 
                  (LPDWORD) &dwTmp, ( NULL)))
    {
        //errhandler("WriteFile", hwnd); 
    }
    // Copy the array of color indices into the .BMP file.  
    dwTotal = cb = pbih->biSizeImage; 
    //hp = lpBits;     
    if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL)) 
    {
        //errhandler("WriteFile", hwnd); 
        goto close_bmp;
    }

close_bmp:
    // Close the .BMP file.  
    if(hf != INVALID_HANDLE_VALUE)
    {
        if (!CloseHandle(hf)) 
        {
           //errhandler("CloseHandle", hwnd); 
        }
        else
        {
            ret = 1;
        }
    }
    // Free memory.  
    // GlobalFree((HGLOBAL)lpBits); 
    if(tHDC != NULL)
        DeleteObject(tHDC);
    if(h != NULL)
        DeleteObject(h);
    if(pbi != NULL)
    {
        //LocalFree(pbi);
        free(pbi);
    }
    return ret;
}