在bmp上添加字符

简介: <p> </p> <p><br> //打开位图文件,得到位图句柄<br>   HBITMAP OpenBmpFile(HDC hDC, LPSTR lpszFileName)<br>   {<br>       HBITMAP hBmp = NULL;<br>       //读位图文件,得到位图句柄<br>       HANDLE hFile = CreateFile(<

 


//打开位图文件,得到位图句柄
  HBITMAP OpenBmpFile(HDC hDC, LPSTR lpszFileName)
  {
      HBITMAP hBmp = NULL;
      //读位图文件,得到位图句柄
      HANDLE hFile = CreateFile(
          lpszFileName,
          GENERIC_READ,
          FILE_SHARE_READ,
          NULL,
          OPEN_EXISTING,
          FILE_ATTRIBUTE_NORMAL,
          NULL);
      if(hFile == INVALID_HANDLE_VALUE)
          return NULL;
      //读位图文件头
      BITMAPFILEHEADER bmpFileHeader;
      DWORD dwNumberOfBytesRead;
      if(ReadFile(hFile, (LPVOID)&bmpFileHeader, sizeof(BITMAPFILEHEADER), &dwNumberOfBytesRead, NULL) == 0)
      {  
          CloseHandle(hFile);
          return NULL;
      }
      //读位图信息
      BITMAPINFO *pBmpInfo = new BITMAPINFO;
      if(ReadFile(hFile, pBmpInfo, sizeof(BITMAPINFOHEADER), &dwNumberOfBytesRead, NULL) == 0)
      {  
          CloseHandle(hFile);
          return NULL;
      }
      //读位图数据
      LPVOID pBmpData;
      //创建DIB位图句柄
      hBmp = CreateDIBSection(hDC, pBmpInfo, DIB_PAL_COLORS, &pBmpData, NULL, 0);
      if(hBmp == NULL)
      {
          DWORD dwErr = GetLastError();
          return NULL;
      }
      else  //读位图数据
          if(ReadFile(hFile, pBmpData, pBmpInfo->bmiHeader.biSizeImage, &dwNumberOfBytesRead, NULL) == 0)
          {  
              CloseHandle(hFile);
              return NULL;
          }      
      CloseHandle(hFile);
      return hBmp;
  }
  
  //下面的代码是把一张位图保存于文件中,参考MSDN
  PBITMAPINFO CreateBitmapInfoStruct(HWND hwnd, HBITMAP hBmp)
  {
      BITMAP bmp;
      PBITMAPINFO pbmi;
      WORD    cClrBits;
  
      // Retrieve the bitmap's color format, width, and height.
      if (!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp))
          return NULL;
  
      // Convert the color format to a count of bits.
      cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
      if (cClrBits == 1)
          cClrBits = 1;
      else if (cClrBits <= 4)
          cClrBits = 4;
      else if (cClrBits <= 8)
          cClrBits = 8;
      else if (cClrBits <= 16)
          cClrBits = 16;
      else if (cClrBits <= 24)
          cClrBits = 24;
      else cClrBits = 32;
  
      // Allocate memory for the BITMAPINFO structure. (This structure
      // contains a BITMAPINFOHEADER structure and an array of RGBQUAD
      // data structures.)
 
      if (cClrBits != 24)
          pbmi = (PBITMAPINFO) LocalAlloc(LPTR,
                     sizeof(BITMAPINFOHEADER) +
                     sizeof(RGBQUAD) * (1<< cClrBits));
 
      // There is no RGBQUAD array for the 24-bit-per-pixel format.
 
      else
          pbmi = (PBITMAPINFO) LocalAlloc(LPTR,
                     sizeof(BITMAPINFOHEADER));
 
     // Initialize the fields in the BITMAPINFO structure.
 
     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 (cClrBits < 24)
         pbmi->bmiHeader.biClrUsed = (1<<cClrBits);
 
     // If the bitmap is not compressed, set the BI_RGB flag.
     pbmi->bmiHeader.biCompression = BI_RGB;
 
     // Compute the number of bytes in the array of color
     // indices and store the result in biSizeImage.
     // For Windows NT/2000, the width must be DWORD aligned unless
     // the bitmap is RLE compressed. This example shows this.
     // For Windows 95/98, the width must be WORD aligned unless the
     // bitmap is RLE compressed.
     pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits +31) & ~31) /8
                                   * pbmi->bmiHeader.biHeight;
     // Set biClrImportant to 0, indicating that all of the
     // device colors are important.
      pbmi->bmiHeader.biClrImportant = 0;
      return pbmi;
  }
 
 void CreateBMPFile(HWND hwnd, LPTSTR 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;
 
     pbih = (PBITMAPINFOHEADER) pbi;
     lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
 
     if (!lpBits)
          return ;
 
     // 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))
     {
         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)
         return ;
     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))
     {
        return ;
     }
 
   // Copy the BITMAPINFOHEADER and RGBQUAD array into the file.
    if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER)
                  + pbih->biClrUsed * sizeof (RGBQUAD),
                  (LPDWORD) &dwTmp, ( NULL)))
        return ;
 
     // 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))
           return ;
 
     // Close the .BMP file.
      if (!CloseHandle(hf))
            return ;
GlobalFree((HGLOBAL)lpBits);
}


void CTextOnbmpDlg::OnButton1()
{
 // TODO: Add your control notification handler code here
    // TODO: Add your control notification handler code here   
HDC hDC = ::GetDC(GetSafeHwnd());
HDC hMemDC = CreateCompatibleDC(hDC);
//打开位图文件
HBITMAP hBmp = OpenBmpFile(hDC, "d:\\temp\\6370烧写\\6484884.bmp");
SelectObject(hMemDC, hBmp);
//在位图上写字
RECT rect = {50, 50, 200, 200};
SetBkMode(hMemDC, TRANSPARENT);
DrawText(hMemDC, "你好", -1, &rect, DT_VCENTER);
//保存位图到文件
PBITMAPINFO pBmpInfo = CreateBitmapInfoStruct(GetSafeHwnd(), hBmp);
CreateBMPFile(GetSafeHwnd(), "Test.bmp", pBmpInfo, hBmp, hDC);
DeleteDC(hMemDC);
DeleteObject(hBmp);

}

相关文章
|
7月前
|
人工智能 IDE API
白板秒变IDE,草图直接生成可运行代码!Pad.ws:白板+代码编辑器深度结合,创意到实现无缝衔接
Pad.ws是一款创新的在线开发环境,将交互式白板与完整IDE工具深度结合,支持多人实时协作和多种编程语言,无需安装即可通过浏览器访问。
348 1
白板秒变IDE,草图直接生成可运行代码!Pad.ws:白板+代码编辑器深度结合,创意到实现无缝衔接
|
数据可视化 图形学 开发者
【Qt 底层机制之图形渲染引擎】深入理解 Qt 的 渲染机制:从基础渲染到高级图形
【Qt 底层机制之图形渲染引擎】深入理解 Qt 的 渲染机制:从基础渲染到高级图形
1956 4
|
Linux 开发工具 内存技术
国产之路:复旦微zynq调试笔记2--PL网口
PL侧的网口需求相较于PS部分还是有一定区别的,主要需要添加axi ethernet 的移植
3529 0
|
4月前
|
Ubuntu 编译器 C语言
在Ubuntu22.04平台上交叉编译针对Rv1126架构的GCC13.2.0编译器的步骤。
遵循上述步骤,您应该能够在Ubuntu 22.04平台上成功交叉编译适用于RISC-V架构RV1126的GCC 13.2.0编译器,允许您为目标硬件构建应用程序和操作系统组件。
249 10
QT 软件打包为一个单独可执行.exe文件流程
QT 软件打包为一个单独可执行.exe文件流程
2033 0
|
Ubuntu Linux Shell
【linux】PetaLinux 2024.1安装笔记
【linux】PetaLinux 2024.1安装笔记
1541 0
|
JavaScript 前端开发 安全
【QML 与 C++ 之间的通讯机制】QML 与 Qt 通讯:讲解如何在QML 中使用C++类,以及如何在C++ 中获取QML的内容
【QML 与 C++ 之间的通讯机制】QML 与 Qt 通讯:讲解如何在QML 中使用C++类,以及如何在C++ 中获取QML的内容
1810 1
|
Ubuntu Linux C语言
Could not establish connection to “xx.xx.xx.xx“:The VS Code Server faild to start.【重要解决方案】
Could not establish connection to “xx.xx.xx.xx“:The VS Code Server faild to start.【重要解决方案】
1122 0
|
传感器 编解码 Linux
V4L2框架 | MIPI Camera指令调试笔记
V4L2框架 | MIPI Camera指令调试笔记
6767 2
|
Linux 开发工具 C语言
【研究Qt webengine 模块编译】linux 交叉编译qt5.12的webengine模块成功的条件
【研究Qt webengine 模块编译】linux 交叉编译qt5.12的webengine模块成功的条件
2131 1