Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

How to convert Image data (JPEG/PNG/GIF) to hBITMAP ?


  • Please log in to reply
49 replies to this topic
SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
I have what I wanted .. Many thanks Sean!! :D :D :D

Working example for loading and displaying a script embedded PNG image file:

Gui -Caption
Gui, Margin, 0, 0
Gui, Add, [color=red]Text[/color], x0 y0 w164 h65 [color=red]+0xE[/color] hWnd[color=red]Pic1[/color] ; +0xE is SS_BITMAP

GoSub, LoadPictureToBuffer ; and initialise nSize

hData := DllCall("GlobalAlloc", UInt,2, UInt, nSize )
pData := DllCall("GlobalLock",  UInt,hData )
DllCall( "RtlMoveMemory", UInt,pData, UInt,&Buffer, UInt,nSize )
DllCall( "GlobalUnlock", UInt,hData )
DllCall( "ole32\CreateStreamOnHGlobal", UInt,hData, Int,True, UIntP,pStream )

DllCall( "LoadLibrary", Str,"gdiplus" )
VarSetCapacity(si, 16, 0), si := Chr(1)
DllCall( "gdiplus\GdiplusStartup", UIntP,pToken, UInt,&si, UInt,0 )

DllCall( "gdiplus\GdipCreateBitmapFromStream", UInt,pStream, UIntP,pBitmap )
DllCall( "gdiplus\GdipCreateHBITMAPFromBitmap", UInt,pBitmap, UIntP,hBitmap, UInt,0 )

SendMessage, (STM_SETIMAGE:=0x172), (IMAGE_BITMAP:=0x0), hBitmap,, ahk_id %[color=red]Pic1[/color]%
SoundPlay, *16, 1
Gui, Show,, Just for fun

DllCall( "gdiplus\GdipDisposeImage", UInt,pBitmap )
DllCall( "gdiplus\GdiplusShutdown", UInt,pToken )
DllCall( NumGet(NumGet(1*pStream)+8 ), UInt,pStream )

Return                                                    ; // End of Auto-Execute section

LoadPictureToBuffer: ; creates Binary buffer with PNG image sized 164x65 ( 1725 bytes )
hex=
( Join
89504E470D0A1A0A0000000D49484452000000A4000000410802000000D05E9827000000157445587443726561
74696F6E2054696D650007D7090B091823E35252930000000774494D4507D7090B09182DD1EB9CE40000000970
48597300000B1200000B1201D2DD7EFC0000063B4944415478DAED9D0B50545518C7BFBB2C2F812520F14113A4
24192596823AE6848F91668C49CB49C707630F75540688D4CC34451A340B4531CB6C02A9C446344149284218C7
19904732C2E02B096B5494485C511ECB6E6739CDE5B27BB97BF7EEB277D9737EC39CFDEEB9E79CFDCEFD9FF39D
73D9BDC0D45E28030A01C4266C50A297D0B049727B42195892929250AAC4077535E572FB43197094AC15313F0B
18600C31CEE1CB54882BC6DB9A95EB8A6A4DDF538598377584CE86B95DC6122BE41E6D14DB41C526082A3641D8
42EC4B459BEA7FFD48EE9E52381B3416F5A574831CBF7189CD35A9DC9C11133FC0C6DFE5296C66D04B9B1BCE6E
E3161B33631B58C0B923B1DCC3C8A55FC97DB9063786623FB8BC0FA5AAB171065B3B7CD6FF8575C86EAADE79AB
F2D391E11B50CE13933662BD03A76E628B8D7E792BB2AF956CB952FC71C8CC640B5D9CB6683FEB89DC976B7023
258C0F9FB01EA5372B76985BB1AE70636DC187D8AEC95F7FE1D43A6C579D78BFF2A7C48AE3EF9D3F9620DCC299
AC95C5875620A328E35D94FEF2CD3B8507DF46C6E903CB708153FB634E7EB1346FDF92DCF4C538E778DAC263BB
1722E368EA9B36BFBCF685D2F2262CA43A772D4A27CEDBD57B9FD897B3875763830DE3BF652EE71628F8FA2D6C
E47F1983D2E835DFA14690DE27F62E9A179F8D4FE5EC5A207747E54748EC7B7569D8406BB6598DFE51BA151B68
CD16197BD1CC4669C41B69C6A78CC3F8CC6507B9C3E2951519BCA384CBFCC41F4D96717884C2F863A109A2DBE9
035AB38323939E9E9E24A6F08BAF7DCEDA26C338C512A4ACD9B7AB76A2146FD0ACC284B9A9288CCB7D291C1FC3
30EE19128B36E4F7EBF7F296BEF3FB67D840B75E3824B2B75E8DE73E41B75EC26F161A9582F668174FF7192578
CDC6A0302E66CDEE8F39ABB2D0B28D3668F8706EDC61C2E3B6014CED85B2D0B0497535E5F4831047ED6C98DBE5
9C1305252525F4D7A50441C526082A364150B109A277377E3E27466E671C0F5DCF8FBD4067364150B109828A4D
10546C82A0621304159B20A8D80441C526082A364150B10942BAD8639F9F1D382A5E6EFF296620516CBFA129D0
DDE6E955BE3979BBDC5DA08845A2D85A4D9EC27928C053C7737277EEAE90BB17145148F9DEB8AF779A872B7A6D
D5576F83BB4DA500E1266B3D377E32F7D0923FEF819A92501DD7925097D7738376B887DCF2C6EF25CD79AB20E9
2181AE23AA2015C3A8019A753ABF9F4FE6AC899FBD7AC53893F5ECE1EFB748F38157575ED9040681EC981DC67D
3C0E0C7141AFDD68E10698AA6FA115EAEBCEC8DD113BC57E94062933FB518677800AE03E40F04805AA7E17BC86
169FCCDEB83D6AF18267CC6D0C473C1C5D811321B9578A7B8AB73AF7AC400E7BC80673839685C3AF31D216055E
E7FBF344C037E33226314F6C954BB69BFE9B173AD0A254FD6C5820B4A035DC1D9AA0B0B0C4A4D8BCFEB1B6716C
64EDFE94162869BC42F3AA2EA624983F0E448A61D065F1BDE0EDBB49CC135BF1608F77801BB4A851BD85B334AF
47974107C0ED9B3A17B7AADCCC1D59D3A3E7840854B7414C33D6C92ACEF4575860721BCC3FABF74242CB6688AD
52E6BB758393A73BB4B4A3253B6E65CB98C087D005A0D130A382E0FAB5EFB34B85C51E688467E700C106611BF7
42C2CC316383E6A24EF6F5F584E67BA04102C394290FFC02B47AB1117FDD00578FDADC6F0B8B1AACD8EDC18E6D
069CF877113BB3BD204FA903C6DD19FE452F00DEC0F8FB848CF0BE54F6273C0268EF849040A8BF9A79A8286AD6
F2FE1A11B3F271278AF0E0352E693CC9C44F3B4B26A88127BCB6B4D6C4E4889FE2BDCF7A0997F3F967F2B0C75D
9D9C3B7AB666009EC004F93DE9B1A4F1C61EB8D5F37D5957E76E0DF3B0B533B3FC871991A3A55D353BC1AE6E8E
ADE21B7ED64BECCCEE68873675A7D289C1817F48B72E79956AFCB8EACEEBA0D1EA9F9464DABABA34A0F286B55B
2AAB4B079FD812F6DB83CE37B1623BB9875F69AC5230DAFF6736C0ABD31A74BA86DAAB005A9D7E0468C1DD05D2
B22386F9B4CB7D71A4606F020F846F62C556FBA6B76A1E5EACBBE3D4F3BC6B2F288033BDA94FB026EF6890DC17
87C28F19B75EC3FD870CF70F92DB618A74E8375508828A4D10546C82A0621304159B20A8D80441C526082A3641
50B109828A4D10546C82A0621304159B20A8D804A1FF8813FF0B568AC3C3444646CAED03C546FC0772437DB7F8
BEC2BC0000000049454E44AE426082
) 

; Laszlo's MCode() ; http://www.autohotkey.com/forum/viewtopic.php?p=135302#135302
nSize := StrLen(Hex)//2
VarSetCapacity( Buffer,nSize ) 
Loop % nSize 
  NumPut( "0x" . SubStr(hex,2*A_Index-1,2), Buffer, A_Index-1, "Char" )
Return

GuiEscape:
  ExitApp

:)

Andreone
  • Members
  • 257 posts
  • Last active: Mar 19 2008 05:30 PM
  • Joined: 20 Jul 2007

So, you never search the forum.

Of course I search the forum, but I agree not to find answer to questions, unless I remember seeing something related. In that case, I know you are not a newbie with ahk (I saw a lot of time your name in posts), so I assumed you have done the research yourself but find nothing. As I know a bit of C++, I thought I might find something in that area.

What made you think that we need the external article for this kind of basic operations like GdipLoadImage...()? There are already numerous examples on it in the forum.


You are referring to my first response, which I told you was not adequate.
Then, I mentioned the LoadImageFromStream function, which is the same function that Sean told you to use.
I can't see how this was not a meaningful hint.

I remember you as I found your scripts kinda insulting to the forum members, well at least to me.

:shock: I don't see what/where you found me insulting? It had never be my point not to respect people. Actually, this is something I'm very sensitive about.
Surely I have poor english so it's quite hard to find the good words when I want to post. Maybe that creates confusion.

The last script of yours I saw could be done by almost every member in this forum, if he/she is not new, I suppose.

I didn't know that only advanced scripts should be posted. I never said that what I post can only be achieved by me and only by me because I'm a genius.
I have read a lot the forum when I started with autohotkey and reading examples/suggestions/ideas was very helpful to me.
I've created two topics, about stuffs I never found as is on the forum, and I was glad to contribute, thinking it might help someone who starts with autohotkey. What's bad in that?

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

Working example for loading and displaying a script embedded PNG image file:

Very clever and funny. :)

BoBo¨
  • Guests
  • Last active:
  • Joined: --
Unfortuantely it isn't working for me :cry:.
"Error loading PNG" (GDIPlus Error) :shock:

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

Unfortuantely it isn't working for me :cry:.
"Error loading PNG" (GDIPlus Error) :shock:


Gotcha! :D

We see what we believe!
That was no error message! It was the PNG displayed in -Caption GUI

I am too one of those who run a test script without looking into the code.

Best Regards, :D

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

You are referring to my first response, which I told you was not adequate.
Then, I mentioned the LoadImageFromStream function, which is the same function that Sean told you to use.

Looks like you were confused between Sean and Skan.
It was Sean, not Skan, who blamed you. I suppose Skan would never do that kind of things.

I realized why I had responded so badly to you: you appeared to me too arrogant. I think I now understand why you appeared so. Judging from your reaction to my post, you seem to be a decent person. I'm feeling really bad to attack a decent person. Maybe it was me who was really arrogant. Many apologies.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

What made you think that we need the external article for this kind of basic operations like GdipLoadImage...()? There are already numerous examples on it in the forum.


You are referring to my first response, which I told you was not adequate.
Then, I mentioned the LoadImageFromStream function, which is the same function that Sean told you to use.
I can't see how this was not a meaningful hint.


Are you addressing me or Sean ? :shock:

Edit: Oh! Sean has clarified .. Thanks Sean. That was a very nice gesture.

BoBo¨
  • Guests
  • Last active:
  • Joined: --

We see what we believe!
That was no error message! It was the PNG displayed in -Caption GUI

:shock: :lol::lol:

I am too one of those who run a test script without looking into the code.

I looked at the code! And sadly I've to agree that I'm not skilled enough to identify the image by reading its hex equivalent. My appologize :cry:

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

I'm not skilled enough to identify the image by reading its hex equivalent.


:lol:

For others, the PNG image below was embedded in the script:

Posted Image

:)

Andreone
  • Members
  • 257 posts
  • Last active: Mar 19 2008 05:30 PM
  • Joined: 20 Jul 2007

I realized why I had responded so badly to you: you appeared to me too arrogant

To be honest, I'm afraid I must have my own responsibility in this. This is not the first time that someone misunderstands me (when I write), even in my own native language, even with my friends. :(
I don't know what it is.
I'll put more efforts to make myself clear.

Autohotkey forum is the very first one I'm participating in. I'm very enthusiastic with it as I think autohotkey is great and the community looked nice. Your answer comforts me in that opinion. Thanks.

:oops: @Skan: sorry, as Sean said, I was quite confused and I made a mistake.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

@Skan: sorry


You do not owe me one!
You have been trying to help me, and I thank you for that. :)

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

I looked at the code! And sadly I've to agree that I'm not skilled enough to identify the image by reading its hex equivalent. My appologize :cry:

I just saved the content of buffer using ReadMemory() in FileHelper.ahk, then open it with the viewer in Total Commander.

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

This is not the first time that someone misunderstands me (when I write), even in my own native language, even with my friends. :(
I don't know what it is.

Maybe you wrote too shortly and definitely. I'm also misunderstood occasionally, but while talking, not writing.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear Sean, have a look at this: Embed images in compiled AutoHotkey scripts? :)

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

Dear Sean, have a look at this: Embed images in compiled AutoHotkey scripts? :)

Thanks for it, Skan. BTW, I just realized that I've never compiled a script! But, I'm pretty sure I'll use it when I try to compile a script.