Saturn image

Saturn Software Mills

Free Screen Capture, save any part of screen, free powerful video screen capture tool, save output in BMP, JPG, AVI file formats, download free screensaver and technical articles

 

Home    Free Software     Buy Now      Free Downloads     Articles     Contact Us     Link Exchange

 
 

 

Program to Copy Screen to HBITMAP Object

 

 

  The following code segment can be used to copy a portion or full screen in to a HBITMAP GDI object in visual C++. This code is tested on Visual C++ 6.0

HBITMAP CopyScreenToBitmap(int x1, int y1, int x2, int y2)
{
HDC hScrDC, hMemDC; // screen DC and memory DC
HBITMAP hBitmap, hOldBitmap; // handles to deice-dependent bitmaps
int nX, nY, nX2, nY2; // coordinates of rectangle to grab
int nWidth, nHeight; // DIB width and height
int xScrn, yScrn; // screen resolution


// create a DC for the screen and create
// a memory DC compatible to screen DC

if((hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL))==NULL){
AfxMessageBox("Can not create display DC");
return NULL;
}
if((hMemDC = CreateCompatibleDC(hScrDC))==NULL)
{
AfxMessageBox("Can not create compatible DC");
return NULL;
}
 

 


// get points of rectangle to grab

nX = x1;
nY = y1;
nX2 = x2;
nY2 = y2;

// get screen resolution

xScrn = GetDeviceCaps(hScrDC, HORZRES);
yScrn = GetDeviceCaps(hScrDC, VERTRES);

//make sure bitmap rectangle is visible

if (nX < 0)
nX = 0;
if (nY < 0)
nY = 0;
if (nX2 > xScrn)
nX2 = xScrn;
if (nY2 > yScrn)
nY2 = yScrn;

nWidth = nX2 - nX;
nHeight = nY2 - nY;

// create a bitmap compatible with the screen DC
if((hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight))==NULL)
{
AfxMessageBox("Can not create compatible bitmap");
return NULL;
}

// select new bitmap into memory DC
hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);

if(hOldBitmap == NULL)
{
AfxMessageBox("Can not select old object");
return NULL;
}

// bitblt screen DC to memory DC
if(!BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY))
{
AfxMessageBox("Can not read screen memory");
return NULL;
}

// select old bitmap back into memory DC and get handle to bitmap of the screen

hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap);
if(hBitmap == NULL)
{
AfxMessageBox("Can not select object");
return NULL;
}
// clean up
DeleteDC(hScrDC);
DeleteDC(hMemDC);

// return handle to the bitmap

return hBitmap;
}

 
 

 

 

  

 

 

Home | Free Software | Buy Now | Free Downloads | Articles | Contact Us | Link Exchange

 

All rights reserved with saturnsoftmills.com