How to snap an OpenGL client and send it to the clipboard | CodeGuru

How to snap an OpenGL client and send it to the clipboard

This article demonstrates how to snap an OpenGL client and send it to ClipBoard. We suppose your screen configuration is set to true color. We could send the same buffer in a .bmp file in a few lines too…. // Snap OpenGL client and send it to ClipBoard // so that you can insert it […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 10, 1999
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

This article demonstrates how to snap an OpenGL client and send it to
ClipBoard.

We suppose your screen configuration is set to true color.

We could send the same buffer in a .bmp file in a few lines too….

// Snap OpenGL client and send it to ClipBoard

// so that you can insert it in your favorite

// image editor, Powerpoint, etc...

// Replace CRenderView by your own CView-derived
class


void CRenderView::SnapClient()

{

 BeginWaitCursor();

 // Get client geometry

 CRect rect;

 GetClientRect(&rect);

 CSize size(rect.Width(),rect.Height());

 TRACE("  client zone : (%d;%d)\n",size.cx,size.cy);

 // Lines have to be 32 bytes aligned,
suppose 24 bits per pixel


 // I just cropped it

 size.cx -= size.cx % 4;

 TRACE("  final client zone : (%d;%d)\n",size.cx,size.cy);

 // Create a bitmap and select it in the device context

 // Note that this will never be used 😉 but no matter

 CBitmap bitmap;

 CDC *pDC = GetDC();

 CDC MemDC;

 ASSERT(MemDC.CreateCompatibleDC(NULL));

 ASSERT(bitmap.CreateCompatibleBitmap(pDC,size.cx,size.cy));

 MemDC.SelectObject(&bitmap);

 // Alloc pixel bytes

 int NbBytes = 3 * size.cx *
size.cy;


 unsigned char
*pPixelData = new unsigned char[NbBytes];

 // Copy from OpenGL

 ::glReadPixels(0,0,size.cx,size.cy,GL_RGB,GL_UNSIGNED_BYTE,pPixelData);

 // Fill header

 BITMAPINFOHEADER header;

 header.biWidth = size.cx;

 header.biHeight = size.cy;

 header.biSizeImage = NbBytes;

 header.biSize = 40;

 header.biPlanes = 1;

 header.biBitCount =  3 * 8; // RGB

 header.biCompression = 0;

 header.biXPelsPerMeter = 0;

 header.biYPelsPerMeter = 0;

 header.biClrUsed = 0;

 header.biClrImportant = 0;

 // Generate handle

 HANDLE handle = (HANDLE)::GlobalAlloc (GHND,sizeof(BITMAPINFOHEADER)
+ NbBytes);


 if(handle != NULL)

 {

  // Lock handle

  char *pData = (char
*) ::GlobalLock((HGLOBAL)handle);


  // Copy header and data

  memcpy(pData,&header,sizeof(BITMAPINFOHEADER));

  memcpy(pData+sizeof(BITMAPINFOHEADER),pPixelData,NbBytes);

  // Unlock

  ::GlobalUnlock((HGLOBAL)handle);

  // Push DIB in clipboard

  OpenClipboard();

  EmptyClipboard();

  SetClipboardData(CF_DIB,handle);

  CloseClipboard();

 }

 // Cleanup

 MemDC.DeleteDC();

 bitmap.DeleteObject();

 delete [] pPixelData;

 EndWaitCursor();

}

 

 

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.