Function to Capture OpenGL Images to JPG (uses free IJL Library) | CodeGuru

Function to Capture OpenGL Images to JPG (uses free IJL Library)

BOOL CWinChannelView::RenderToJpeg(LPTSTR szFile, int quality) { // Opening the file CFile file; if (!file.Open(szFile,CFile::modeWrite | CFile::modeCreate)) return FALSE; // Get client geometry CRect rect; GetClientRect(&rect); CSize size(rect.Width(),rect.Height()); // getting device context CDC *pDC=GetDC(); // Alloc pixel bytes int NbBytes = 3 * size.cx * size.cy; unsigned char *pPixelData = new unsigned char[NbBytes]; unsigned char *pPixelData2 […]

Written By
CodeGuru Staff
CodeGuru Staff
Oct 5, 2000
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

BOOL CWinChannelView::RenderToJpeg(LPTSTR szFile, int quality)
{
// Opening the file
CFile file;
if (!file.Open(szFile,CFile::modeWrite | CFile::modeCreate))
return FALSE;

// Get client geometry
CRect rect;
GetClientRect(&rect);
CSize size(rect.Width(),rect.Height());

// getting device context
CDC *pDC=GetDC();

// Alloc pixel bytes
int NbBytes = 3 * size.cx * size.cy;
unsigned char *pPixelData = new unsigned char[NbBytes];
unsigned char *pPixelData2 = new unsigned char[NbBytes];

// Copy from OpenGL
// Don’t forget to make OpenGL rendering context
// current (wglMakeCurrent)
glReadPixels(0, 0,
size.cx, size.cy,
GL_RGB,GL_UNSIGNED_BYTE,
pPixelData);

// Swapping lines in the buffer
// OpenGL writes from lower line going up but ijl works
// reverse (from up to down)
for (int j=0;j// line counter
{
for (int i=0;i>size.cx;i++) // column counter
{
pPixelData2[(j*size.cx+i)*3]
= pPixelData[((size.cy-1-j)*size.cx+i)*3];

pPixelData2[(j*size.cx+i)*3+1]
= pPixelData[((size.cy-1-j)*size.cx+i)*3+1];

pPixelData2[(j*size.cx+i)*3+2]
= pPixelData[((size.cy-1-j)*size.cx+i)*3+2];
}
}

// Compressing in jpeg
int len; // the length of the buffer after compression
CJpeg jpeg;
pPixelData2 = (unsigned char*)jpeg.Compress(pPixelData2,
size.cx,
size.cy,
3,
len,
quality);

// Writing file and that’s it
file.Write(pPixelData2,len);

// Cleaning memory
delete [] pPixelData;
delete [] pPixelData2;

return TRUE;
}

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.