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

Graphics.FromHwnd(IntPtr hwnd) For The Entire Screen?

Hello. I can pInvoke "GetDC" with NULL or "IntPtr.Zero" and get the
hDC for the entire screen. I can use the hDC for stuff like "GetPixel"
and it works fine.

When I try to do the...

System.Drawing.Graphics myDC =
System.Drawing.Graphics.FromHwnd(IntPtr.Zero);

then a "myDC.GetHdc()" and use that with "GetPixel" it doesn't seem to
work.
If I do the ".FromHwnd(myGameHwnd);" then use the "myDC.GetHdc()" with
the "GetPixel" it works fine.

is there a way for me to use the managed "System.Drawing.Graphics" to
get a hDC for the entire screen like the pInvoke "GetDC" does?

Thanks

Aug 25 '06 #1
6 11765
Did you check the StatusException?

Aug 25 '06 #2

Sathyaish wrote:
Did you check the StatusException?
I'm sorry, I don't think I did... How do I? This is basically all I
do...

System.Drawing.Graphics myGraph =
System.Drawing.Graphics.FromHwnd(IntPtr.Zero);
uint nCol = GetPixel(myGraph.GetHdc(), xCoord, yCoord);
myGraph.ReleaseHdc();
return nCol;
nCol is always 0xFFFFFFFF (Which is CLR_INVALID), I've tried a lot of
different xCoord, yCoord's. IE 100,100. If .FromHwnd(IntPtr.Zero) is
returning the entire screen DC, how can I be getting CLR_INVALID?

Aug 25 '06 #3
Before you get to the GetPixel, put a try catch just for FromHwnd().

try
{
System.Drawing.Graphics myGraph =
System.Drawing.Graphics.FromHwnd(IntPtr.Zero);

if (myGraph != null)
uint nCol = GetPixel(myGraph.GetHdc(), xCoord, yCoord);

}
catch(System.Exception ex)
{
Debug.Assert(false);
Debug.WriteLine(ex.Message);
}

I am guessing that there was no exception thrown by the FromHwnd method
and that you are getting a valid graphics object.

I think the problem might be with the device (desktop) not supporting
ICM (color management). This would be because (I am guessing again) the
Graphics class's FromHwnd calls GdipCreateFromHWND API which disabled
color management and thus disables GetPixel.

One good way to check would be to call GetDeviceCaps on the desktop
hWnd before you call GetPixel().
Before you get to the GetPixel, put a try catch just for FromHwnd().

try
{
System.Drawing.Graphics myGraph =
System.Drawing.Graphics.FromHwnd(IntPtr.Zero);

if (myGraph != null)
if (GetDeviceCaps(COLORMGMTCAPS) != CM_NONE)
uint nCol = GetPixel(myGraph.GetHdc(), xCoord, yCoord);

}
catch(System.Exception ex)
{
Debug.Assert(false);
Debug.WriteLine(ex.Message);
}

And then, step through the debugger.
Note: GetDeviceCaps is an API you'll have to PInvoke.

Aug 25 '06 #4
if (GetDeviceCaps(COLORMGMTCAPS) != CM_NONE)
Oops! Sorry I forget the HDC as the argument to GetDevCaps. You'll send
the HDC you got from Graphics.GetHDC(). Make that a separate call.
Don't package in the call as the argument.

Aug 25 '06 #5
Okay :) This is what it is now....

/* Color Management caps */
public const int COLORMGMTCAPS = 121;
public const int CM_NONE = 0x00000000;
public const int CM_DEVICE_ICM = 0x00000001;
public const int CM_GAMMA_RAMP = 0x00000002;
public const int CM_CMYK_COLOR = 0x00000004;

/*Inside the Function Call*/
try {
System.Drawing.Graphics myGraph =
System.Drawing.Graphics.FromHwnd(IntPtr.Zero);
uint nCol = 0;
IntPtr myGraphHDC = myGraph.GetHdc();
int myGraphDevCaps = GetDeviceCaps(myGraphHDC, COLORMGMTCAPS);

if(myGraph != null && myGraphDevCaps != CM_NONE) nCol =
GetPixel(myGraphHDC, 100, 100);

myGraph.ReleaseHdc();
return nCol;
} catch(System.Exception ex) {
System.Diagnostics.Debug.Assert(false);
System.Diagnostics.Debug.WriteLine(ex.Message);
}
return 0;

GetDeviceCaps Returns "2" which is "CM_GAMMA_RAMP" means my desktop
should support it? nCol is still always 0xffffffff and thats what gets
returned. My desktop is 1600x1200 so the (100,100) should be fine.
Note: If I just do the pInvoke method like so

IntPtr myGraphHDC = GetDC(IntPtr.Zero);
nCol = GetPixel(myGraphHDC, 100, 100);
ReleaseDC(IntPtr.Zero, myGraphHDC);
return nCol;

Then GetPixel returns "0x00849ebd" which is most likly my Desktop
Color.

But I've also tried using the exact same code at the begining of this
post except chaning ".FromHwnd(IntPtr.Zero);" to
".FromHwnd(myGameHwnd);" and for point (100,100) (When Game is Up,
Windowed Mode) I get "0x00a5bece", and "int myGraphDevCaps = ..." gives
"2" like when using "IntPtr.Zero".

:) At this point its just more-so curiosity, I don't mind using the
pInvoke method with "GetDC" but wondering a little (for future
reference) if its possible to use the managed method to get the entire
screen DC like "GetDC" does with null. Is there anything else ya can
think of that I can try? I've with multiple points (not just 100,100),
and they all return "0xFFFFFFFF".
NvrBst

Aug 25 '06 #6
NvrBst,
If you are using .NET 2.0 I would recommend:

Graphics.CopyFromScreen to get a bitmap that contains the colors of pixels
on the screen.

http://msdn2.microsoft.com/en-us/lib...romscreen.aspx
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"NvrBst" <qu****@hotmail.comwrote in message
news:11*********************@74g2000cwt.googlegrou ps.com...
| Hello. I can pInvoke "GetDC" with NULL or "IntPtr.Zero" and get the
| hDC for the entire screen. I can use the hDC for stuff like "GetPixel"
| and it works fine.
|
| When I try to do the...
|
| System.Drawing.Graphics myDC =
| System.Drawing.Graphics.FromHwnd(IntPtr.Zero);
|
| then a "myDC.GetHdc()" and use that with "GetPixel" it doesn't seem to
| work.
|
|
| If I do the ".FromHwnd(myGameHwnd);" then use the "myDC.GetHdc()" with
| the "GetPixel" it works fine.
|
| is there a way for me to use the managed "System.Drawing.Graphics" to
| get a hDC for the entire screen like the pInvoke "GetDC" does?
|
| Thanks
|
Sep 1 '06 #7

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

Similar topics

1
by: edward hage | last post by:
The following program is an applet that reads a number from a file 10 times per second. It needs to be shown graphically. The file monitors a procesvariable and changes continuously. It is read...
12
by: Sanjay | last post by:
hi, We are currently porting our project from VB6 to VB .NET. Earlier we used to make scale transformations on objects like pictureBox , forms etc.Now Such transformations are made on the...
2
by: John Bailo | last post by:
I am walking through some of the very first sample code from the book "Beginning .NET Game Programming" from Apress. I identify his sample code with //SC This code puzzles me: Graphics graph...
3
by: Ron Vecchi | last post by:
I am creating my own MainMenu control similar to VS.Net. (yes I know theirs some out their). I have predominatly been a web developer for about 5 years and am playing with Windows forms. So this...
3
by: Jay | last post by:
Is there a something similar to initgraph in C#, or do I have to download the directx library? All I want to do is bring up a graphics screen and be able to recognize the mouse and color some...
8
by: benben | last post by:
I created a form and overrided OnPaint, OnClick and OnResize methods. My OnPaint calls base.OnPaint then repaints the whole screen. The screen flickers a lot! It didn't happen when the app was...
1
by: Benny Raymond | last post by:
I've seen both used... FromHwnd can be called like this FromHwnd(this.Handle). FromHdc needs a call to User32.GetWindowDC(this.Handle) - so my assumption is that FromHwnd is better because it's...
8
by: pigeonrandle | last post by:
Hi, Please pity me, i am on a dial-up connection for the first time in 5 years :( ! Does anyone know how the resulting Graphics objects differ ...? What i really mean is can someone explain it...
9
by: she_prog | last post by:
Dear All, I need to save the content of a panel to a bitmap. The panel can have many child controls which also need to be saved. The problem would be solved if I could have the panel saved to a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.