0% found this document useful (0 votes)
622 views2 pages

C Program To Copy Screen To HBITMAP Object

It is a C program that copies the screen content in to a HBITMAP object. you can have similar articles at http://www.saturnsoftmills.com

Uploaded by

sudhir1975
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
622 views2 pages

C Program To Copy Screen To HBITMAP Object

It is a C program that copies the screen content in to a HBITMAP object. you can have similar articles at http://www.saturnsoftmills.com

Uploaded by

sudhir1975
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 2

Saturn Software Mills

Visit for more technical article http://www.saturnsoftmills.com/FreeDownloads.htm

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)
Saturn Software Mills
Visit for more technical article http://www.saturnsoftmills.com/FreeDownloads.htm

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;
}

You might also like