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
hiren
  • Guests
  • Last active:
  • Joined: --
i have to open jpg image and then return hbitmap. can anyone help me? i really need this as soon as possible.

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

i have to open jpg image and then return hbitmap.


FileGetSize, nSize, C:\file.jpg
FileRead, Buffer, C:\file.jpg

For rest of code, you can copy paste my example:

:)

Sarah
  • Members
  • 103 posts
  • Last active: Oct 12 2009 05:42 PM
  • Joined: 12 Jul 2007
Hello, LexiKos was kind to point me to this thread, which maybe someone knows how to do this below.

Basically, I am hoping to have a function which will do the following procedure:

1) Read in a PNG image, and convert it to textual data, anything

2) Read this textual data separately back into the same image without loss of quality

The posts in this thread look promising, but not sure what to do. I think the post by Skan in here has the direction I need... but I am not getting success running the code, or at least not sure how to use it :oops: I just get a blank GUI on the screen. If someone knows how to do this could you please post a working demonstration that follows concept of #1 and #2 above? I would thank you very very much.

:D

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

This method would work only for small images, let say imgs < 10K

but I am not getting success running the code, or at least not sure how to use it :oops: I just get a blank GUI on the screen.


What OS? It should work in XP and greater. For W2K You should have GDI+ installed.

:)

Sarah
  • Members
  • 103 posts
  • Last active: Oct 12 2009 05:42 PM
  • Joined: 12 Jul 2007
Hi Skan!

Win XP (64 Bit) :D So far, no Autohotkey incompatibilities to report.

--
Ah... less than 10K may be a problem for me. How about a 100K image or more? :) Could the textual converted data be fed in from a external .TXT file to get past string limits? That would be really cool. LexiKos said there are many ways this could be approached, ...

but goal being

1) not a very big .txt file
2) high quality result

We did some BBCODE stuff from this thread...

<!-- m -->http://www.autohotke...8334.html image<!-- m -->

and I even managed to write a parser which will read the BBCODE (the last couple of posts from that thread).... but it is stuck there (not yet able to convert/save to image or re-render it to a usable image yet from a big original image) :(

Sarah

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

Win XP (64 Bit) :D So far, no Autohotkey incompatibilities to report.


You mean this this script does not work for you ? I am asking you whether you tried it standalone?

Could the textual converted data be fed in from a external .TXT file to get past string limits?


Great thinking.. It would work. :)

Sarah
  • Members
  • 103 posts
  • Last active: Oct 12 2009 05:42 PM
  • Joined: 12 Jul 2007
I am in tune now --- purrfect :D

Sarah
  • Members
  • 103 posts
  • Last active: Oct 12 2009 05:42 PM
  • Joined: 12 Jul 2007
Skan, all is well, however, I am confused on the process to create the (machine?) code, hex code like what you had in that example. I understand the process to read an image into the buffer using this

FileRead, Buffer, C:\image.jpg


However, what is next step to create the raw text data stream? I could write that via FileAppend to a text file?

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Just allow me some time ( 20 minutes ? ) .. :)

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
File = %1%
Loop %File%
  File := A_LoopFileLongPath
ATC := A_TickCount
IfNotExist, %File%, Return
FileGetSize, dataSz , %File%
FileRead, BinData , %File%
hexData := Bin2Hex( &BinData, dataSz )
hFile := DllCall( "_lcreat", Str,File ".txt", Int,0 ) 
DllCall( "_lwrite", Int,hFile, Int, &hexData, Int,dataSz*2 ) 
DllCall( "_lclose", Int,hFile ) 
MsgBox,64, % "Binary to Hex [ DONE! ]", %File%.txt
Return

/*

  Bin2Hex() and Hex2Bin()

  Machine code functions: Bit Wizardry [ By Laszlo Hars ]

  Topic : http://www.autohotkey.com/forum/viewtopic.php?t=21172
  Post  : http://www.autohotkey.com/forum/viewtopic.php?p=180469#180469

*/

Bin2Hex(addr,len) { ; Bin2Hex(&x,4) 

   Static fun 
   If (fun = "") 
      Hex2Bin(fun,"8B4C2404578B7C241085FF7E2F568B7424108A06C0E8042C0A8AD0C0EA05" 
      . "2AC2044188018A06240F2C0A8AD0C0EA052AC2410441468801414F75D75EC601005FC3") 
   VarSetCapacity(hex,2*len+1) 
   dllcall(&fun, "uint",&hex, "uint",addr, "uint",len, "cdecl") 
   VarSetCapacity(hex,-1) ; update StrLen 
   Return hex 
} 

Hex2Bin(ByRef bin, hex) { ; Hex2Bin(fun,"8B4C24") = MCode(fun,"8B4C24") 
   Static fun 
   If (fun = "") { 
      h:="568b74240c8a164684d2743b578b7c240c538ac2c0e806b109f6e98ac802cac0e104880f8" 
       . "a164684d2741a8ac2c0e806b309f6eb80e20f02c20ac188078a16474684d275cd5b5f5ec3" 
      VarSetCapacity(fun,StrLen(h)//2) 
      Loop % StrLen(h)//2 
         NumPut("0x" . SubStr(h,2*A_Index-1,2), fun, A_Index-1, "Char") 
   } 
   VarSetCapacity(bin,StrLen(hex)//2) 
   dllcall(&fun, "uint",&bin, "Str",hex, "cdecl") 
}

Save the above script and drag'n'drop a PNG file over it.
If you drop sarah.png you will get sarah.png.txt in same folder as the source.

:)

Sarah
  • Members
  • 103 posts
  • Last active: Oct 12 2009 05:42 PM
  • Joined: 12 Jul 2007
Skan, that is so awesome :shock:

Now for the glory question --

How to pull in this data from the generated .txt file to make an image... without breaking the script? (i.e String Continuation Limit)

If it can do that I will be one --very-- happy person.

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

Skan, that is so awesome :shock:


:O I never expected it will work for you.
Did you really try in 64Bit OS ?

Here is a template to adapt from:

SetWorkingDir, %A_ScriptDir%

[color=red]IfNotExist, cat.png.txt ; 2.72MB ( 1200x797 ) 
   URLDownloadToFile, http://arian.suresh.googlepages.com/cat.png.txt, cat.png.txt[/color]

FileRead, Hex, [color=red]cat.png.txt[/color]
FileGetSize, nSize, [color=red]cat.png.txt[/color]
Hex2Bin( Buffer, Hex )


Gui -Caption
Gui, Margin, 0, 0
Gui, Add, Text, x0 y0 w[color=red]1200[/color] h[color=red]797[/color] +0xE hWndPic1 ; +0xE is SS_BITMAP

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 %Pic1%
Gui, Show,, cat.png

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

Return                                                    ; // End of Auto-Execute section

Hex2Bin(ByRef bin, hex) { ; Hex2Bin(fun,"8B4C24") = MCode(fun,"8B4C24") 
; Laszlo : http://www.autohotkey.com/forum/viewtopic.php?p=180469#180469
   Static fun 
   If (fun = "") { 
      h:="568b74240c8a164684d2743b578b7c240c538ac2c0e806b109f6e98ac802cac0e104880f8" 
       . "a164684d2741a8ac2c0e806b309f6eb80e20f02c20ac188078a16474684d275cd5b5f5ec3" 
      VarSetCapacity(fun,StrLen(h)//2) 
      Loop % StrLen(h)//2 
         NumPut("0x" . SubStr(h,2*A_Index-1,2), fun, A_Index-1, "Char") 
   } 
   VarSetCapacity(bin,StrLen(hex)//2) 
   dllcall(&fun, "uint",&bin, "Str",hex, "cdecl") 
}

GuiEscape:
GuiClose:
  ExitApp
Return

Remove the URlDownload and adjust the filename, width, height

:)

Edit: Forgot to credit Laszlo.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Slightly better. You do not have to feed the Width and Height:

SetWorkingDir, %A_ScriptDir%

;Remove the following 2 lines after first run
IfNotExist, cat.png.txt ; 2.72MB ( 1200x797 ) 
   URLDownloadToFile, http://arian.suresh.googlepages.com/cat.png.txt, cat.png.txt

FileRead, Hex, [color=red]cat.png.txt[/color]
nSize := StrLen(Hex)//2
Hex2Bin( Buffer, Hex )


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 )
DllCall("gdiplus\GdipGetImageWidth" , "Uint", pBitmap, "UintP", nW) 
DllCall("gdiplus\GdipGetImageHeight", "Uint", pBitmap, "UintP", nH) 

Gui -Caption
Gui, Margin, 0, 0
Gui, Add, Text, x0 y0 w%nW% h%nH% +0xE hWndPic1 ; +0xE is SS_BITMAP
SendMessage, (STM_SETIMAGE:=0x172), (IMAGE_BITMAP:=0x0), hBitmap,, ahk_id %Pic1%
Gui, Show,, cat.png

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

Return                                                    ; // End of Auto-Execute section

Hex2Bin(ByRef bin, hex) { ; Hex2Bin(fun,"8B4C24") = MCode(fun,"8B4C24") 
; Laszlo : http://www.autohotkey.com/forum/viewtopic.php?p=180469#180469
   Static fun 
   If (fun = "") { 
      h:="568b74240c8a164684d2743b578b7c240c538ac2c0e806b109f6e98ac802cac0e104880f8" 
       . "a164684d2741a8ac2c0e806b309f6eb80e20f02c20ac188078a16474684d275cd5b5f5ec3" 
      VarSetCapacity(fun,StrLen(h)//2) 
      Loop % StrLen(h)//2 
         NumPut("0x" . SubStr(h,2*A_Index-1,2), fun, A_Index-1, "Char") 
   } 
   VarSetCapacity(bin,StrLen(hex)//2) 
   dllcall(&fun, "uint",&bin, "Str",hex, "cdecl")
}

GuiEscape:
GuiClose:
  ExitApp
Return

:)

Edit: Forgot to Credit Laszlo

Sarah
  • Members
  • 103 posts
  • Last active: Oct 12 2009 05:42 PM
  • Joined: 12 Jul 2007
That is very very cool. Your code is brilliant Skan -- and works too! I am sorry, I am so tired, ... but I can definitely confirm this runs on Win x64 models just right. You are the best....

Sincerest wishes, Sarah.

Sarah
  • Members
  • 103 posts
  • Last active: Oct 12 2009 05:42 PM
  • Joined: 12 Jul 2007
If I wanted to display two or more images, how would that be done,

like

FileRead, Hex, cat.png.txt
FileRead, Hex, sun.png.txt
FileRead, Hex, moon.png.txt
FileRead, Hex, stars.png.txt


and keep the later references to them elegant? I am usually intelligent to figure this stuff out, but the hWndPic1 is not linking in for me, how that is obtained.