C++:Hbitmap/BITMAP 到 .bmp 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24644709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:47:02  来源:igfitidea点击:

C++: Hbitmap/BITMAP into .bmp file

c++hbitmapleptonicadib

提问by user3285714

Ok, whole story is, I am trying to use Leptonica+Tesseract OCR in C++ to take a screenshot, save it to a *.bmp file, then load it back up to OCR with it. I won't need to do this frequently, but as I cannot seem to copy the screenshot data directly into a Leptonica PIX structure, I need to save it to a file first..actually a solution to this would be preferably.

好的,整个故事是,我试图在 C++ 中使用 Leptonica+Tesseract OCR 来截取屏幕截图,将其保存到 *.bmp 文件,然后将其加载回 OCR。我不需要经常执行此操作,但由于我似乎无法将屏幕截图数据直接复制到 Leptonica PIX 结构中,因此我需要先将其保存到文件中。实际上,最好是解决此问题。

Here's some code I've found online, trying to help me out.

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

Screen cap:

屏幕盖:

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
}

Attempt to write function:

尝试编写函数:

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;
}

Shamelessly copied from people's posts over the years. Ok! Problem I face is, I cannot seem to understand how to GlobalAlloc the Hbitmap into a globally accessible Handle, that can be converted or use with LPBITMAPINFOHEADER. Soon as lpbi is created, every single field inside of it is "Unable to read memory" error in Visual Studio 2012 debugging. It's inaccessible, despite being created.

多年来无耻地抄袭人们的帖子。好的!我面临的问题是,我似乎无法理解如何将 Hbitmap GlobalAlloc 转换为全局可访问的句柄,该句柄可以转换或与 LPBITMAPINFOHEADER 一起使用。创建 lpbi 后,其中的每个字段都是 Visual Studio 2012 调试中的“无法读取内存”错误。尽管已创建,但它无法访问。

Solutions.. Go straight from screencap to PIX, inside of memory.. Find a way to save to bitmap and create them periodically to read.. Find another way entirely that makes more sense..

解决方案.. 直接从 screencap 到 PIX,在内存中.. 找到一种方法来保存到位图并定期创建它们以供阅读.. 找到另一种更有意义的方法..

Preferring first, but, I'm asking for a solution in this, to the second one..or third.

更喜欢第一个,但是,我在寻求解决方案,而不是第二个……或第三个。

If you need more info I can try to provide it. This mostly boils down to "I've never done code like this before and it wasn't taught in my classes so I'm trying to learn as I go".

如果您需要更多信息,我可以尝试提供。这主要归结为“我以前从未编写过这样的代码,而且我的课程中没有教过它,所以我正在努力学习”。

回答by enhzflep

A much easier way to save an HBITMAP to file is to make use of GDI+. This gives you the advantage of being able to save to any format that windows supports natively, while freeing you from the muck of playing around with or even needing to understand, various image formats.

将 HBITMAP 保存到文件的一种更简单的方法是使用 GDI+。这为您提供了能够保存为 Windows 本机支持的任何格式的优势,同时使您免于使用甚至需要理解各种图像格式的麻烦。

In the below example, I've just used LoadImage as a quik and dirty way of loading a pre-existing image - you could simply use the HBITMAP you've already captured.

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

Here's an example that loads a bitmap and saves it again. (I had initially used "image/png" as the output type, along with an appropriate output filename)

这是一个加载位图并再次保存的示例。(我最初使用“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;
}

回答by Serdalis

I recently had to do the same thing you are doing and successfully used GlobalAlloc.
The basis of this code is from This MSDN Article.

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

It looks like you Got your example code from here.

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

MSDN is really reliable for win32 operations, definitely prefer it over other sites in my experaince.

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

What seems to be happening is that the sizeof(hDIBtochange)is returning 4, so you are only allocating 4 bytes of memory. which would not be enough to hold a pbistructure.

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

Here is my code with a GlobalAllocwhich hopefully will show the correct usage.

这是我的代码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);
}


Here is the top function in that MSDN article, if you need it (again modified by me).

如果您需要,这是该 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;
}

回答by Chubosaurus Software

I'm guessing you got your code from here Storing an Image. A while back I had to modify the code to work with WinCE 5.0 and WinCE 6.0. Here is the beta-sample though it is kinda messy. It does it without the GlobalAlloc. It uses CreateDibSection instead.

我猜你从这里得到你的代码存储图像。不久前,我不得不修改代码以使用 WinCE 5.0 和 WinCE 6.0。这是测试版示例,虽然它有点乱。它在没有 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;

}