Sign in to follow this  
Followers 0
Kyan

Load JPG from memory and set it to CtrlCreatePic

4 posts in this topic

Hi

I'm trying to set a jpg (in binary) to a ctrlPic, but by some reason the loaded picture doesn't shows up

Here how I do it (some code from other help threads):

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <TabConstants.au3>
#include <GDIPlus.au3>
#include <SendMessage.au3>
#include <Memory.au3>
#Region ### START Koda GUI section ### Form=
Global Const $STM_SETIMAGE = 0x0172
Global $semPIC =_LoadSemPic()
$Form1 = GUICreate("Form1", 158, 159, 192, 124)
$Pic1 = GUICtrlCreatePic("", 8, 8, 140, 140,BitOR($GUI_SS_DEFAULT_PIC,$SS_CENTERIMAGE,$SS_BITMAP,$WS_BORDER))
$hPic1 = GUICtrlGetHandle($Pic1)
_GDIPlus_Startup()
_WinAPI_DeleteObject(_SendMessage($hPic1, $STM_SETIMAGE, 0, $semPIC))
_WinAPI_UpdateWindow($hPic1)
_GDIPlus_Shutdown()
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit

EndSwitch
WEnd


Func _LoadSemPic()
Local $bsempic = "0xFFD8FFDB0043000201010201010202020202020202030503030303030604040305070607070706070708090B0908080A0807070A" & _
"0D0A0A0B0C0C0C0C07090E0F0D0C0E0B0C0C0CFFDB004301020202030303060303060C0807080C0C0C0C0C0C0C0C0C0C0C0C0C0C" & _
"0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0CFFC0001108008C008C03012200021101" & _
"031101FFC4001F0000010501010101010100000000000000000102030405060708090A0BFFC400B5100002010303020403050504" & _
"040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A16171819"
Return Load_BMP_From_Mem(Binary($bsempic),True)
EndFunc


;======================================================================================
; Function Name:        Load_BMP_From_Mem
; Description:          Loads a image which is saved as a binary string and converts it to a bitmap or hbitmap
;
; Parameters:           $mem_image:     the binary string which contains any valid image which is supported by GDI+
; Optional:                 $hHBITMAP:      if false a bitmap will be created, if true a hbitmap will be created
;
; Remark:                   hbitmap format is used generally for GUI internal images
;
; Requirement(s):       GDIPlus.au3, Memory.au3
; Return Value(s):  Success: handle to bitmap or hbitmap, Error: 0
; Error codes:          1: $mem_image is not a binary string
;
; Author(s):                UEZ
; Additional Code:  thanks to progandy for the MemGlobalAlloc and tVARIANT lines
; Version:                  v0.95 Build 2011-06-11 Beta
;=======================================================================================
Func Load_BMP_From_Mem($mem_image, $hHBITMAP = False)
    If Not IsBinary($mem_image) Then Return SetError(1, 0, 0)
    Local $declared = True
    If Not $ghGDIPDll Then
        _GDIPlus_Startup()
        $declared = False
    EndIf
    Local Const $memBitmap = Binary($mem_image) ;load image  saved in variable (memory) and convert it to binary
    Local Const $len = BinaryLen($memBitmap) ;get length of image
    Local Const $hData = _MemGlobalAlloc($len, $GMEM_MOVEABLE) ;allocates movable memory  ($GMEM_MOVEABLE = 0x0002)
    Local Const $pData = _MemGlobalLock($hData) ;translate the handle into a pointer
    Local $tMem = DllStructCreate("byte[" & $len & "]", $pData) ;create struct
    DllStructSetData($tMem, 1, $memBitmap) ;fill struct with image data
    _MemGlobalUnlock($hData) ;decrements the lock count  associated with a memory object that was allocated with GMEM_MOVEABLE
    Local $hStream = DllCall("ole32.dll", "int", "CreateStreamOnHGlobal", "handle", $pData, "int", True, "ptr*", 0)
    $hStream = $hStream[3]
    Local $hBitmap = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromStream", "ptr", $hStream, "int*", 0) ;Creates a Bitmap object based on an IStream COM interface
    $hBitmap = $hBitmap[2]
    Local Const $tVARIANT = DllStructCreate("word vt;word r1;word r2;word r3;ptr data; ptr")
    DllCall("oleaut32.dll", "long", "DispCallFunc", "ptr", $hStream, "dword", 8 + 8 * @AutoItX64, _
                                           "dword", 4, "dword", 23, "dword", 0, "ptr", 0, "ptr", 0, "ptr", DllStructGetPtr($tVARIANT)) ;release memory from $hStream to avoid memory leak
    $tMem = 0
    If $hHBITMAP Then
        Local Const $hHBmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
        _GDIPlus_BitmapDispose($hBitmap)
        If Not $declared Then _GDIPlus_Shutdown()
        Return $hHBmp
    EndIf
    If Not $declared Then _GDIPlus_Shutdown()
    Return $hBitmapEndFunc   ;==>Load_BMP_From_Mem

I think that $STM_SETIMAGE needs to be different to pic ctrl, but in help file/command list I didn't found anything


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites



This works for me

#include <StaticConstants.au3>
#include <Memory.au3>
#include <GDIPlus.au3>
#include <WindowsConstants.au3>

_GDIPlus_Startup()
Global Const $STM_SETIMAGE = 0x0172
Global $binImage = InetRead( "http://userdisk.webry.biglobe.ne.jp/017/247/75/N000/000/000/124739380243016220257_korn.jpg", 1)
$hHBITMAP = Load_BMP_From_Mem($binImage, True)
$hImage = _GDIPlus_BitmapCreateFromHBITMAP( $hHBITMAP )
Local $iH = _GDIPlus_ImageGetHeight( $hImage )
Local $iW = _GDIPlus_ImageGetWidth( $hImage )

$hGUI = GUICreate("Displays an image from memory in button control", $iW, $iH)
$idButton = GUICtrlCreatePic("", 0, 0, $iW, $iH, $SS_BITMAP)
$hButton = GUICtrlGetHandle($idButton)
_WinAPI_DeleteObject(_SendMessage($hButton, $STM_SETIMAGE, 0, $hHBITMAP))
_WinAPI_UpdateWindow($hButton)
_GDIPlus_Shutdown()
GUISetState()


While 1
$msg = GUIGetMsg()
If $msg = -3 Or $msg = $idButton Then ExitLoop
WEnd
_WinAPI_DeleteObject($hHBITMAP)
GUIDelete($hGUI)
Exit

;======================================================================================
; Function Name:        Load_BMP_From_Mem
; Description:          Loads a image which is saved as a binary string and converts it to a bitmap or hbitmap
;
; Parameters:           $mem_image:     the binary string which contains any valid image which is supported by GDI+
; Optional:                 $hHBITMAP:      if false a bitmap will be created, if true a hbitmap will be created
;
; Remark:                   hbitmap format is used generally for GUI internal images
;
; Requirement(s):       GDIPlus.au3, Memory.au3
; Return Value(s):  Success: handle to bitmap or hbitmap, Error: 0
; Error codes:          1: $mem_image is not a binary string
;
; Author(s):                UEZ
; Additional Code:  thanks to progandy for the MemGlobalAlloc and tVARIANT lines
; Version:                  v0.95 Build 2011-06-11 Beta
;=======================================================================================
Func Load_BMP_From_Mem($mem_image, $hHBITMAP = False)
If Not IsBinary($mem_image) Then Return SetError(1, 0, 0)
Local $declared = True
If Not $ghGDIPDll Then
_GDIPlus_Startup()
$declared = False
EndIf
Local Const $memBitmap = Binary($mem_image) ;load image  saved in variable (memory) and convert it to binary
Local Const $len = BinaryLen($memBitmap) ;get length of image
Local Const $hData = _MemGlobalAlloc($len, $GMEM_MOVEABLE) ;allocates movable memory  ($GMEM_MOVEABLE = 0x0002)
Local Const $pData = _MemGlobalLock($hData) ;translate the handle into a pointer
Local $tMem = DllStructCreate("byte[" & $len & "]", $pData) ;create struct
DllStructSetData($tMem, 1, $memBitmap) ;fill struct with image data
_MemGlobalUnlock($hData) ;decrements the lock count  associated with a memory object that was allocated with GMEM_MOVEABLE
Local $hStream = DllCall("ole32.dll", "int", "CreateStreamOnHGlobal", "handle", $pData, "int", True, "ptr*", 0)
$hStream = $hStream[3]
Local $hBitmap = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromStream", "ptr", $hStream, "int*", 0) ;Creates a Bitmap object based on an IStream COM interface
$hBitmap = $hBitmap[2]
Local Const $tVARIANT = DllStructCreate("word vt;word r1;word r2;word r3;ptr data; ptr")
DllCall("oleaut32.dll", "long", "DispCallFunc", "ptr", $hStream, "dword", 8 + 8 * @AutoItX64, _
"dword", 4, "dword", 23, "dword", 0, "ptr", 0, "ptr", 0, "ptr", DllStructGetPtr($tVARIANT)) ;release memory from $hStream to avoid memory leak
$tMem = 0
If $hHBITMAP Then
Local Const $hHBmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
_GDIPlus_BitmapDispose($hBitmap)
If Not $declared Then _GDIPlus_Shutdown()
Return $hHBmp
EndIf
If Not $declared Then _GDIPlus_Shutdown()
Return $hBitmap
EndFunc   ;==>Load_BMP_From_Mem


My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

Simultaneously this script concluded that the binary you posted isn't a valid image

Global $binImage = _LoadSemPic()

$hFile = FileOpen( @ScriptDir & '\Test.bmp', 16 + 2 )
FileWrite( $hFile , $binImage )
FileClose( $hFile )

Func _LoadSemPic()
Return Binary( "0xFFD8FFDB0043000201010201010202020202020202030503030303030604040305070607070706070708090B0908080A0807070A" & _
"0D0A0A0B0C0C0C0C07090E0F0D0C0E0B0C0C0CFFDB004301020202030303060303060C0807080C0C0C0C0C0C0C0C0C0C0C0C0C0C" & _
"0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0CFFC0001108008C008C03012200021101" & _
"031101FFC4001F0000010501010101010100000000000000000102030405060708090A0BFFC400B5100002010303020403050504" & _
"040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A16171819" )
EndFunc   ;==>_LoadSemPic


My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

Simultaneously this script concluded that the binary you posted isn't a valid image

Global $binImage = _LoadSemPic()

$hFile = FileOpen( @ScriptDir & '\Test.bmp', 16 + 2 )
FileWrite( $hFile , $binImage )
FileClose( $hFile )

Func _LoadSemPic()
Return Binary( "0xFFD8FFDB0043000201010201010202020202020202030503030303030604040305070607070706070708090B0908080A0807070A" & _
"0D0A0A0B0C0C0C0C07090E0F0D0C0E0B0C0C0CFFDB004301020202030303060303060C0807080C0C0C0C0C0C0C0C0C0C0C0C0C0C" & _
"0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0CFFC0001108008C008C03012200021101" & _
"031101FFC4001F0000010501010101010100000000000000000102030405060708090A0BFFC400B5100002010303020403050504" & _
"040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A16171819" )
EndFunc ;==>_LoadSemPic

I'm so stupid, ya, hex data is a lot bigger than that (hex editor copy limit)

I replaced with the total hex code, and now works --', thanks, I lost more than 2hours searching to find what is wrong with the code, and was that simple mistake -.-


Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0