473,378 Members | 1,510 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,378 software developers and data experts.

Getting the content in a window.

93
Hi,
I am in need of a way of doing the following.

1. I can get the handle to a window that I know of.
2. I can get the DC as well.

I want to Get the content of the window(DC) in to the memory as a bitmap so I can fiddle around with it.
I have seen this same problem all over internet and there are many suggestions. But I can't get my code to work.

Here's what I have in the end of a whole day:

Expand|Select|Wrap|Line Numbers
  1.  
  2.         RECT rc;
  3.  
  4.     ::GetWindowRect(hWnd, &rc);
  5.  
  6.     int w = rc.right-rc.left;
  7.     int h = rc.bottom-rc.top;
  8.  
  9.     HDC hDC = ::GetDC(hWnd);
  10.     HDC memDC = ::CreateCompatibleDC(hDC);
  11.  
  12.     HBITMAP memBM = ::CreateCompatibleBitmap(hDC, w, h);
  13.  
  14.     ::SelectObject(memDC, memBM );
  15.  
  16.     ::BitBlt(memDC, 0, 0, w, h , hDC, rc.left, rc.top , SRCCOPY );
  17.     int size = 3 * w * h;
  18.     int *lpBits = new int[size];
  19.  
  20.     ::GetBitmapBits(memBM, size, lpBits );
  21.  
  22.  
And that code is not mine. It is a Ctr + c + v from the net.

However, I can't figure it out. Can Anyone plz tell me what the format of the values in lpBits array? Is it RGBA ? etc. And how can I derive a single pixel out of it? Thanx very much in advance.
Mar 19 '07 #1
1 2137
Banfa
9,065 Expert Mod 8TB
Code commented in bold

Expand|Select|Wrap|Line Numbers
  1.         RECT rc;
  2.  
  3.     ::GetWindowRect(hWnd, &rc);
  4.     // GetWindowRect is the wrong function to call and will 
  5.     // result in the wrong values for w and h as it includes the 
  6.     // entire window including border and menu and title bars
  7.     // Use GetClientRect instead
  8.     // In a client rect rc.left == 0 and rc.top == 0 always
  9.     // so in the following code w = rc.right, h = rc.bottom
  10.  
  11.     int w = rc.right-rc.left;
  12.     int h = rc.bottom-rc.top;
  13.  
  14.     // GetDC gets the client DC
  15.     HDC hDC = ::GetDC(hWnd);
  16.     HDC memDC = ::CreateCompatibleDC(hDC);
  17.  
  18.     HBITMAP memBM = ::CreateCompatibleBitmap(hDC, w, h);
  19.  
  20.     ::SelectObject(memDC, memBM );
  21.  
  22.     ::BitBlt(memDC, 0, 0, w, h , hDC, rc.left, rc.top , SRCCOPY );
  23.     // The source position parameters are wrong, hDC is a client DC
  24.     // Since you called GetWindowRect you will be copying from just
  25.     // to the right and just beneath the actual window
  26.     // Source position needs to be 0, 0
  27.  
  28.  
  29.     // Why do all this when you can just manipulate the 
  30.     // Bitmap thorugh memDC?  You can use any of the drawing 
  31.     // functions that take a DC
  32.     int size = 3 * w * h;
  33.     int *lpBits = new int[size];
  34.  
  35.     ::GetBitmapBits(memBM, size, lpBits );
  36.  
  37.  
However, I can't figure it out. Can Anyone plz tell me what the format of the values in lpBits array? Is it RGBA ? etc. And how can I derive a single pixel out of it? Thanx very much in advance.
lpBits is not of a fixed format, it will depend on the display settings the user has on their computer. You could find out those settings using GetDeviceCaps and derive single pixels out of the data, however as I said in the code above why do this when you could just use GetPixel, SetPixel and all the other available drawing functions?
Mar 19 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Gregory Van Vooren | last post by:
When opening a new window with javascript it's possible to specify what features (location, toolbar, ...) should be visible. Is there also some way of checking which of these features are enabled...
0
by: Jim | last post by:
I need some help getting started with a .NET web project for a commercial site. I am new to .NET and my understanding of some (but not all) of its concepts is a little sparse. I apologize for the...
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
2
by: Alex | last post by:
Hi all, I'm writing a small web application which searches a database based on a date field, and populates a datagrid control with the results. The datagrid control has selection buttons added...
1
by: Paul D. Fox | last post by:
I'm trying to launch a Child Window from a hyperlink on a Datagrid and have it recieve multiple values from the Parent Window. Upon recieving the values in the Child Window, I need to access them...
3
by: sundew | last post by:
so here is the test case: <head> <script type='text/javascript'> // this function simply returns the number of enumerable namespaces(objects) function numNSpaces(){ var num = 0; for(var i in...
1
by: kamleshsharmadts | last post by:
I am using Ajax with struts in web application. from jsp i am calling a function of ajax.js onclick of a button. code of that call function which calling from jsp given as below:- ...
1
by: tsangkinyip | last post by:
Hi there, I have a diary page where I would like to fade in the text content and an image when a user tries to click on a date in the calendar. Things work fine offline, but when I upload the...
6
by: DRS.Usenet | last post by:
When I run alert("page contents:" + content.document.documentElement.innerHTML); I am able to pull the content. I see something like this page contents:<head><title>A Title</title>...
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.