Code sample Home

Save window view into raster image in a memory buffer

void DemoRasterToMem (HANDLE hLcWnd)
{
  double Xmin, Ymin, Xmax, Ymax;
  int    ImgW, ImgH;
  int    nBytes, sz, InfoSize, PalSize, ImgSize;
  HANDLE hBlock;
  BYTE*  pMem;
  BITMAPFILEHEADER bfh;
  BITMAPINFOHEADER* pbih;
  BYTE*  pImgBits;

  // get currently visible block
  hBlock = lcPropGetHandle( hLcWnd, LC_PROP_WND_VIEWBLOCK );

  // get size of CAD window, pixels 
  ImgW = lcPropGetInt( hLcWnd, LC_PROP_WND_WIDTH );
  ImgH = lcPropGetInt( hLcWnd, LC_PROP_WND_HEIGHT );

  // get extents of currently visible drawing's area
  Xmin = lcPropGetFloat( hLcWnd, LC_PROP_WND_XMIN );
  Ymin = lcPropGetFloat( hLcWnd, LC_PROP_WND_YMIN );
  Xmax = lcPropGetFloat( hLcWnd, LC_PROP_WND_XMAX );
  Ymax = lcPropGetFloat( hLcWnd, LC_PROP_WND_YMAX );

  // get required size for the memory buffer
  nBytes = lcBlockRasterizeMem( hBlock, NULL, Xmin, Ymin, Xmax, Ymax, ImgW,ImgH );
  // allocate memory buffer
  pMem = new BYTE[nBytes];
  // write the block's image into the buffer
  nBytes = lcBlockRasterizeMem( hBlock, pMem, Xmin, Ymin, Xmax, Ymax, ImgW,ImgH );

  // just for testing, let's save the memory buffer as an image file
  // in order to see the image contents
  FILE* df = _wfopen( L"d:\\View.bmp", L"wb" );
  if (df){
    pbih = (BITMAPINFOHEADER*)pMem;
    ImgSize = pbih->biSizeImage;
    switch( pbih->biBitCount ){
      case 1: 
        PalSize = 2 * 4;
        break;
      case 4: 
        PalSize = 16 * 4;
        break;
      case 8: 
        PalSize = 256 * 4;
        break;
      default:
        PalSize = 0;
    }
    InfoSize = 40 + PalSize;

    // write BMP file header
    sz = sizeof(bfh);
    memset( &bfh, 0, sz );
    bfh.bfType = 0x4D42;   // BM
    // file size
    bfh.bfSize = 14 + nBytes;
    // offset to bitmap bits
    bfh.bfOffBits = 14 + InfoSize;   // sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + Palette
    fwrite( &bfh, sz, 1, df );

    // write bitmap info header
    fwrite( pbih, InfoSize, 1, df );

    // write image bits
    pImgBits = pMem + InfoSize;
    fwrite( pImgBits, ImgSize, 1, df );

    fclose( df );
  }

  // free the memory buffer
  delete[] pMem;
}

See Also:

Save window view into raster image file
Save active block into raster image file