473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

screen capture problem

xc
Greetings.

I encountered a wield problem when grabing screen images.
Sometimes in some computers I can capture the screen, but other times not
so.
In some computer I cannot capture the screen image, but when I change its
color bits, say from 32bits to 24 or 16bits, the program can capture the
image.

Can anyone kindly examine my following code, and point out the flaw:

void CGetScreenToBitmap::SetArea(CRect rcArea, int nArea)
{
HWND hFocus;
int xScrn, yScrn;
HDC hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);

xScrn = GetDeviceCaps(hScrDC, HORZRES);
yScrn = GetDeviceCaps(hScrDC, VERTRES);
DeleteDC(hScrDC);
m_rcArea.left = 0;
m_rcArea.top = 0;
m_rcArea.right = xScrn;
m_rcArea.bottom = yScrn;

hFocus = GetActiveWindow();
if (nArea == 1 && hFocus != NULL)
GetWindowRect(hFocus, &m_rcArea);
else if(nArea == 2)
m_rcArea = rcArea;

if (m_rcArea.left < 0)
m_rcArea.left = 0;
if (m_rcArea.top < 0)
m_rcArea.top = 0;
if (m_rcArea.right > xScrn)
m_rcArea.right = xScrn;
if (m_rcArea.bottom > yScrn)
m_rcArea.bottom = yScrn;
}

BOOL CGetScreenToBitmap::GetScreen(CRect rcArea, int nBits, int nArea)
{
HDC hScrDC, hMemDC;
HBITMAP hBitmap, hOldBitmap;

WORD wBitCount;

DWORD dwPaletteSize=0;
BITMAP Bitmap;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
HANDLE hFile, handle;
FILE * f1;
DEVMODE dmSettings;
HPALETTE hPal = NULL;

memset(&dmSettings,0,sizeof(dmSettings)); // Makes Sure Memory's Cleared

// Get current settings -- This function fills our the settings
// This makes sure NT and Win98 machines change correctly
EnumDisplaySettings(NULL,ENUM_CURRENT_SETTINGS,&dm Settings);

dmSettings.dmBitsPerPel = 16;
dmSettings.dmFields = DM_BITSPERPEL;

// This function actually changes the screen to full screen
// CDS_FULLSCREEN Gets Rid Of Start Bar.
// We always want to get a result from this function to check if we failed
int result = ChangeDisplaySettings(&dmSettings,0);

// Check if we didn't recieved a good return message From the function
if(result = DISP_CHANGE_SUCCESSFUL)
{
f1=fopen("C:\\winnt\\ae.bmp","a+");
fwrite("ChangeDisplaySettings error!!!",1,30,f1);
fclose(f1);
}

ResetVariable();
SetArea(rcArea, nArea);
hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);

hMemDC = CreateCompatibleDC(hScrDC);
hBitmap = CreateCompatibleBitmap(hScrDC, m_rcArea.Width(),
m_rcArea.Height());

hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);

BitBlt(hMemDC, 0, 0, m_rcArea.Width(), m_rcArea.Height(), hScrDC,
m_rcArea.left, m_rcArea.top, SRCCOPY);

hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap);

wBitCount = nBits;

if (wBitCount <= 8)
dwPaletteSize = (1 << wBitCount) * sizeof(RGBQUAD);

GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = Bitmap.bmWidth;
bi.biHeight = Bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wBitCount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = dwPaletteSize/sizeof(RGBQUAD);
bi.biClrImportant = 0;

m_dwBmpInfoSize = dwPaletteSize + sizeof(BITMAPINFOHEADER);

m_lpBmpData = (LPSTR)GlobalAlloc(GMEM_FIXED, m_dwBmpInfoSize);
lpbi = (LPBITMAPINFOHEADER)m_lpBmpData;
*lpbi = bi;

if (wBitCount <= 8)
hPal = (HPALETTE)GetStockObject(DEFAULT_PALETTE);
if (hPal)
{
SelectPalette(hMemDC, hPal, TRUE);
RealizePalette(hMemDC);
}
// Call GetDIBits with a NULL lpBits param, so the device driver
// will calculate the biSizeImage field
if(!GetDIBits(
hMemDC,
hBitmap,
0L,
(UINT) Bitmap.bmHeight,
(LPBYTE)NULL,
(BITMAPINFO *)lpbi,
DIB_RGB_COLORS
))
{
f1=fopen("C:\\winnt\\ae.bmp","a+");
fwrite("First GetDIBits error!!!",1,24,f1);
fclose(f1);
}

bi = *lpbi;

// If the driver did not fill in the biSizeImage field, then compute it
// Each scan line of the image is aligned on a DWORD (32bit) boundary
if (bi.biSizeImage == 0)
bi.biSizeImage = ((Bitmap.bmWidth * wBitCount+31)& ~31) /8 *
Bitmap.bmHeight;
m_dwBmpSize = bi.biSizeImage + m_dwBmpInfoSize;

handle = GlobalReAlloc(m_lpBmpData, m_dwBmpSize, GMEM_MOVEABLE);
m_lpBmpData = (char *)handle;
lpbi = (LPBITMAPINFOHEADER)m_lpBmpData;

// FINALLY get the DIB
int Lines = GetDIBits(hMemDC, hBitmap, 0L, (UINT)Bitmap.bmHeight,
(LPSTR)lpbi + sizeof(BITMAPINFOHEADER) + dwPaletteSize,
(BITMAPINFO *)lpbi, DIB_RGB_COLORS);
char szBuf[12];
wsprintf(szBuf, "Lines: %i", Lines);
f1=fopen("C:\\winnt\\ae.bmp","a+");
fwrite(szBuf,1,12,f1);
fclose(f1);
// OK, so we've got the bits, and the BITMAPINFOHEADER.
// Now we can put them in a file.
hFile = CreateFile("C:\\winnt\\aa.bmp", GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, 0, NULL);

// .BMP file begins with a BITMAPFILEHEADER,
// so we'll write that.
BITMAPFILEHEADER bmfh;
bmfh.bfType = MAKEWORD('B','M');
bmfh.bfSize = sizeof(BITMAPFILEHEADER) + m_dwBmpSize;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + m_dwBmpInfoSize;

DWORD bytesWritten;
WriteFile(hFile, &bmfh, sizeof(BITMAPFILEHEADER), &bytesWritten, NULL);

// Then it's followed by the BITMAPINFOHEADER structure
WriteFile(hFile, lpbi, m_dwBmpInfoSize, &bytesWritten, NULL);

// Then the pixel data.
WriteFile(hFile, lpbi + m_dwBmpInfoSize, bi.biSizeImage, &bytesWritten,
NULL);

CloseHandle(hFile);

DeleteObject (hBitmap);
DeleteObject (hOldBitmap);
DeleteDC(hScrDC);
DeleteDC(hMemDC);
return TRUE;
}

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Jul 22 '05 #1
1 2930

"xc" <fx*****@163.com> wrote in message news:41********@127.0.0.1...
Greetings.

I encountered a wield problem when grabing screen images.


I would have been a good idea if instead of wasting time with test posts
(use news:alt.test for that) you had spend some time considering which group
to post to. We don't do Windows programming here (this is a C++ language
group) so you are off topic. Try posting here
news:comp.os.ms-windows.programmer.win32.

john
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stan Shankman | last post by:
C# -- Visual Studio.Net – Windows Application Greetings all, How do I copy a Form’s image to a bitmap? – And do so before it gets to the screen. I haven’t been able to find anyone that knows...
5
by: TerryWilson | last post by:
I am developing a web based service tool using asp.net that we will distribute with our product. This service tool will be used for remotely configuring the product, problem determination, etc. ...
0
by: kris_scheyer | last post by:
Ok, here's what im trying to do: I have a machine running XP embedded that has a webserver. I have a button on the aspx web form that is supposed to capture the screen of the computer hosting the...
2
by: py | last post by:
I need to take a screen shot of the computer screen. I am trying to use PIL and I saw there is ImageGrab...however it only works on Windows. Is there a platform-independent ability to work...
4
by: gwhite1 | last post by:
I use this code to capture a screen in a regular VB 2005 windows app. It works great! I found the code in google. But when I create a windows service it does not capture the screen. It only...
2
by: dumbledad | last post by:
Hi All, I'm using ASP.Net web services to provide the logic required for a Flash based prototype. The Flash and the ASP .Net run together on the client machine. One of the functions I'd like to...
4
by: Paolo Pantaleo | last post by:
Hi, I need to capture a screen snapshot in Linux. PIL has a module IageGrab, but in the free version it only works under Windows. Is there any package to capture the screen on Linux? Thnx...
5
Atran
by: Atran | last post by:
Hello Everyone. In this article: You will know to capture the screen in 2 ways: 1)- Capture full screen. 2)- Capture region. Let's Begin: First make a new Windows Application project. And...
2
by: raylopez99 | last post by:
Beware newbies: I spent a day before I figured this out: copying a bitmap (image) file to file is not quite like copying a text file--you have to do some tricks (see below), like using a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.