Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
real_Napster
Posts: 34
Joined: 19 Jan 2016, 13:43
Location: Germany

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

22 Nov 2016, 05:52

This is my script:

Code: Select all

^h::
	MsgBox % GetPixelcolorFromHwnd(300, 300)
return

GetPixelcolorFromHwnd(x, y, hwnd := 0)
{
	hDC := DllCall("user32.dll\GetDCEx", "Ptr", hwnd, "UInt", 0, "UInt", 1|2)
	pix := DllCall("gdi32.dll\GetPixel", "Ptr", hDC, "Int", x, "Int", y, "UInt")
	DllCall("user32.dll\ReleaseDC", "Ptr", hwnd, "Ptr", hDC)
	DllCall("gdi32.dll\DeleteDC", "Ptr", hDC)
	VarSetCapacity(hex, 8 << !!A_IsUnicode, 0)
    DllCall("shlwapi.dll\wnsprintf", "Str", hex, "Int", 8, "Str", "%06I64X", "Int", pix, "Int")
    return "0x" hex
}
If i set a pure Red background in Paint and Press CTRL + H it returns: 0x8EA0F00.
If i change color to white, it's just the same.... :crazy:
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

22 Nov 2016, 06:03

small mistake from my side... rewrite atm
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

22 Nov 2016, 06:05

Code: Select all

Format("0x{:06X}", ColorValue)
real_Napster
Posts: 34
Joined: 19 Jan 2016, 13:43
Location: Germany

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

22 Nov 2016, 06:49

Format("0x{:06X}", hex) returns 0x0000FF instead of 0xFF0000
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

22 Nov 2016, 07:00

You need to switch from BGR to RGB

Code: Select all

Format("0x{5:}{6:}{3:}{4:}{1:}{2:}",StrSplit(Format("{:06X}", ColorValue))*)
or:

Code: Select all

Format("{:06X}", (ColorValue>>12)|(ColorValue&0xFF00)|((ColorValue&0xFF)<<12))
Recommends AHK Studio
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

22 Nov 2016, 07:16

Code: Select all

AeroGetPixel(xpos, ypos, hWnd := 0)
{
    hDC := DllCall("user32.dll\GetDCEx", "ptr", hWnd, "uint", 0, "uint", 0x1|0x2)
    bgr := DllCall("gdi32.dll\GetPixel", "ptr", hDC, "int", xpos, "int", ypos, "uint")
    DllCall("user32.dll\ReleaseDC", "ptr", hWnd, "ptr", hDC)
    return Format("0x{:06X}", ((bgr & 0xff0000) >> 16) | (bgr & 0xff00) | ((bgr & 0xff) << 16))
}
-> https://github.com/Lexikos/AutoHotkey_L ... t.cpp#L179
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

22 Nov 2016, 07:22

jNizM wrote: Should be this way:

Code: Select all

MsgBox % DecToHex(15)     ; --> 0xf
MsgBox % DecToHex(255)    ; --> 0xff
That is exactly what I get with my suggestion.
[quote="jNizM"]
or:
[code]Format("{:06X}", (ColorValue>>12)|(ColorValue&0xFF00)|((ColorValue&0xFF)<<12))[/code]
[/quote]
Should be:
[code]
(ColorValue&0x0000ff)<<16 | ((ColorValue&0x00ff00)>>8)<<8 | (ColorValue>>16)
[/code]

Edit: Saw your new post.
real_Napster
Posts: 34
Joined: 19 Jan 2016, 13:43
Location: Germany

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

22 Nov 2016, 07:27

thanks, thats working perfectly!

Just one more thing.
Is "DeleteDC" not needed?
Or would it be better to include that line to your script?

Code: Select all

DllCall("gdi32.dll\DeleteDC", "ptr", hDC)
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

22 Nov 2016, 07:30

If I get the ahk source right (https://github.com/Lexikos/AutoHotkey_L ... t.cpp#L179) its not needed.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

22 Nov 2016, 09:15

DeleteDC wrote:An application must not delete a DC whose handle was obtained by calling the GetDC function. Instead, it must call the ReleaseDC function to free the DC.

Source: Remarks
The same is true for GetDCEx().
real_Napster
Posts: 34
Joined: 19 Jan 2016, 13:43
Location: Germany

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

22 Nov 2016, 09:22

Good to know, thanks!
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

22 Nov 2016, 10:38

just me wrote:
DeleteDC wrote:An application must not delete a DC whose handle was obtained by calling the GetDC function. Instead, it must call the ReleaseDC function to free the DC.

Source: Remarks
The same is true for GetDCEx().
i know what MSDN says, but i have seen memory leaks when not using DeleteDC, thats why its in there. i haven't seen any negative side effects from DeleteDC in there, so i suggest leaving it in

nnnik wrote:You need to switch from BGR to RGB

Code: Select all

Format("0x{5:}{6:}{3:}{4:}{1:}{2:}",StrSplit(Format("{:06X}", ColorValue))*)
or:

Code: Select all

Format("{:06X}", (ColorValue>>12)|(ColorValue&0xFF00)|((ColorValue&0xFF)<<12))
this is what i use:

Code: Select all

;// http://www.autohotkey.com/forum/post-200921.html#200921
FlipBlueAndRed(c) ; takes RGB or BGR and swaps the R and B
{
   return (c&255)<<16 | (c&65280) | (c>>16)
}

lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

22 Nov 2016, 16:07

jNizM wrote:Should be this way:
Why? Helgef's code works, whereas your code does not produce a hexadecimal string if dec is 0.
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

22 Nov 2016, 16:23

guest3456 wrote:i know what MSDN says, but i have seen memory leaks when not using DeleteDC, thats why its in there.
After using GetDC() or GetDCEx()?
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

22 Nov 2016, 22:24

just me wrote:
guest3456 wrote:i know what MSDN says, but i have seen memory leaks when not using DeleteDC, thats why its in there.
After using GetDC() or GetDCEx()?
both as i remember

User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

23 Nov 2016, 01:54

lexikos wrote:
jNizM wrote:Should be this way:
Why? Helgef's code works, whereas your code does not produce a hexadecimal string if dec is 0.
I've correct me later
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

23 Nov 2016, 02:09

And about DeleteDC
Return value

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.

Code: Select all

AeroGetPixel(xpos, ypos, hWnd := 0)
{
    hDC := DllCall("user32.dll\GetDCEx", "ptr", hWnd, "uint", 0, "uint", 0x1|0x2)
    bgr := DllCall("gdi32.dll\GetPixel", "ptr", hDC, "int", xpos, "int", ypos, "uint")
    DllCall("user32.dll\ReleaseDC", "ptr", hWnd, "ptr", hDC)
	MsgBox % ret := DllCall("gdi32.dll\DeleteDC", "ptr", hDC)    ; -> 0
    return Format("0x{:06X}", ((bgr & 0xff0000) >> 16) | (bgr & 0xff00) | ((bgr & 0xff) << 16))
}
In your example you call DeleteDC after ReleaseDC and it returns zero.. so it fails here

ref: https://stackoverflow.com/questions/356 ... 7#35657397
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

23 Nov 2016, 02:48

:trollface:
jNizM wrote:And about DeleteDC...
Image


Can you remember my post here:
guest3456 wrote: i know what MSDN says, but i have seen memory leaks when not using DeleteDC, thats why its in there. i haven't seen any negative side effects from DeleteDC in there, so i suggest leaving it in


i'm not changing production code that could potentially affect my users and have them come complaining to me. even if true, there has been no side effects as a result, and until i'm shown otherwise, if it aint broke, i'm not fixing it. i can't have a user's computer crash due to a memory leak and they come yelling at me

lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

23 Nov 2016, 03:19

So you know that MSDN says you must not do what you are doing, but you do it anyway. Well, if your script stops working or starts to have strange side-effects after Windows gets updated or under conditions which you and your users have never tested, you can't say Microsoft didn't warn you.

Personally, I would recommend complying with the documentation unless it is proven to be incorrect.
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Gdip_BitmapFromHWND Problem with Windows 7 Basic Design

23 Nov 2016, 09:58

lexikos wrote:So you know that MSDN says you must not do what you are doing, but you do it anyway. Well, if your script stops working or starts to have strange side-effects after Windows gets updated or under conditions which you and your users have never tested, you can't say Microsoft didn't warn you.
sure. fair enough. i def take responsibility if my code fails for me. i'm aware that i'm defying the docs and will know where to look if things pop up.
lexikos wrote:Personally, I would recommend complying with the documentation unless it is proven to be incorrect.
you're right that my suggestion is no good for most people. its prob best to follow the docs.

does GetPixel return BGR for you instead of the documented RGB ?


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Rohwedder, Thalon and 413 guests