• 将CRgn rgn对象以图片形式输出便于查看


    为了调试方便,将CRgn rgn对象以图片形式输出,便于查看
    {
     CRgn rgn
     int w = m_lpShapePoints[4].x+4;
     int h = m_lpShapePoints[4].y+4;
     HWND hDesk = GetDesktopWindow();
     HDC hDC = ::GetDC(hDesk);
     CMemDC MemDC(hDC, CSize(w, h), 0);
     for (int x=0; x<w; x++)
     {
      for (int y=0; y<h; y++)
      {
       CPoint pt(x, y);
       if (rgn.PtInRegion(pt))
       {
        CDC *pDC = CDC::FromHandle(MemDC.GetMemDC());
        pDC->SetPixel(x, y, RGB(255, 0, 0));
       }
      }
     }

     SaveBitmap(MemDC.GetHBITMAP(), _T("c:\\rgnoutput.bmp");
      ::ReleaseDC(hDesk, hDC);
    }

    BOOL SaveBitmap(HBITMAP hBitmap, LPCTSTR ptzFileName)
    {
     HDC hDC;
     DWORD dwSize;
     BOOL bReturn;
     LPBYTE pBits;
     HANDLE hFile;
     BITMAP bmBitmap;
     BITMAPFILEHEADER bfhFile;
     BITMAPINFOHEADER bihInfo;

     // 返回值初始化为 FALSE
     bReturn = FALSE;
     // 取得屏幕兼容位图的对象信息
     if (GetObject(hBitmap, sizeof(BITMAP), &bmBitmap))
     {
      // 设置 BITMAPINFOHEADER 结构
      bihInfo.biSize = sizeof(BITMAPINFOHEADER);
      bihInfo.biWidth = bmBitmap.bmWidth;
      bihInfo.biHeight = bmBitmap.bmHeight;
      bihInfo.biPlanes = 1;
      bihInfo.biBitCount = 24;
      bihInfo.biCompression = BI_RGB;
      bihInfo.biSizeImage =  _WidthPitch(bmBitmap.bmWidth) * bmBitmap.bmHeight;
      bihInfo.biXPelsPerMeter = 0;
      bihInfo.biYPelsPerMeter = 0;
      bihInfo.biClrUsed = 0;
      bihInfo.biClrImportant = 0;

      // 设置 BITMAPFILEHEADER 结构
      bfhFile.bfType = 0x4D42;  // "BM"
      bfhFile.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + bihInfo.biSizeImage;
      bfhFile.bfReserved1 = 0;
      bfhFile.bfReserved2 = 0;
      bfhFile.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

      // 分配内存
      pBits = (LPBYTE) HeapAlloc(GetProcessHeap(), 0, bihInfo.biSizeImage);
      if (pBits)
      {
       // 获取屏幕设备场景
       hDC = CreateIC(_T("DISPLAY"), NULL, NULL, NULL);

       // 获取 DIB 数据
       if (GetDIBits(hDC, hBitmap, 0, bmBitmap.bmHeight,
        pBits, (LPBITMAPINFO) &bihInfo, DIB_RGB_COLORS))
       {
        // 创建文件
        hFile = CreateFile(ptzFileName, GENERIC_WRITE, 0, NULL,
         CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
        if (hFile != INVALID_HANDLE_VALUE)
        {
         // 写入 BITMAPFILEHEADER  信息
         bReturn = WriteFile(hFile, &bfhFile, sizeof(BITMAPFILEHEADER), &dwSize, 0);
         if (bReturn)
         {
          // 写入 BITMAPINFOHEADER 信息
          bReturn = WriteFile(hFile, &bihInfo, sizeof(BITMAPINFOHEADER), &dwSize, 0);
          if (bReturn)
          {
           // 写入 DIB 数据
           bReturn = WriteFile(hFile, pBits, bihInfo.biSizeImage, &dwSize, 0);
          }
         }

         // 关闭文件句柄
         CloseHandle(hFile);
        }
       }

       // 删除设备场景,释放内存
       DeleteDC(hDC);
       HeapFree(GetProcessHeap(), 0, (LPVOID) pBits);
      }
     }

     return bReturn;
     return 0;
    }

  • 相关阅读:
    彻底理解数字图像处理中的卷积-以Sobel算子为例
    CSK & KCF(tracking)
    内积、标量积、点积、点乘
    C++拷贝构造函数详解
    从RGB色转为灰度色算法
    混合高斯背景建模原理及实现
    单高斯背景建模
    计算机视觉目标跟踪的算法分类
    SVM:从理论到OpenCV实践
    HOG特征(Histogram of Gradient)学习总结
  • 原文地址:https://www.cnblogs.com/carl2380/p/1919539.html
Copyright © 2020-2023  润新知