Jump to content

[SOLVED] Save img read in mem to base64


Rex
 Share

Recommended Posts

Hi

I'm trying to dl an img from web, scale it and then save it in base64 format

$hBmp = _GDIPlus_BitmapCreateFromMemory(InetRead("https://" & $dImage)) ;to load an image from the net
    $hBitmap_Scaled = _GDIPlus_ImageResize($hBmp, 140, 200) ; Resizing the image to 140 x 200 pixels
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap_Scaled)

I have this code to handle the scaling, but I can't find any way to save the scaled img to base64

The only save img I could find in help file is "_GDIPlus_ImageSaveToFile"

I could use that to save the img to a temp, then do a read into base64 and then delete the tmp img, but it would be so much easier just to use the scaled img already in mem :)

 

Anyone have a sug on how I could managed this?

 

Cheers
/Rex

Edited by Rex
Solved
Link to comment
Share on other sites

Try this:

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

_GDIPlus_Startup()
$dImage = "https://www.autoitscript.com/forum/uploads/monthly_2015_04/profile_egypt.jpg.656c82deaa5d3f40f8dd3cead64a67c4.thumb.jpg.911aa23cb8c21db2434388eb7138fecf.jpg"
$hBmp = _GDIPlus_BitmapCreateFromMemory(InetRead($dImage)) ;to load an image from the net
$hBitmap_Scaled = _GDIPlus_ImageResize($hBmp, 100, 100) ; Resizing the image to 100 x 100 pixels
$sB64 = _Base64Encode(_GDIPlus_StreamImage2BinaryString($hBitmap_Scaled))
ConsoleWrite($sB64 & @CRLF)
_GDIPlus_ImageDispose($hBmp)
_GDIPlus_ImageDispose($hBitmap_Scaled)
_GDIPlus_Shutdown()

; #FUNCTION# ;===============================================================================
;
; Name...........: _Base64Encode
; Description ...: Returns the given strinng encoded as a Base64 string.
; Syntax.........: _Base64Encode($sData)
; Parameters ....: $sData
; Return values .: Success - Base64 encoded string.
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
;                  |1 - Could not create DOMDocument
;                  |2 - Could not create Element
;                  |3 - No string to return
; Author ........: turbov21
; Modified.......:
; Remarks .......:
; Related .......: _Base64Decode
; Link ..........;
; Example .......; Yes
;
; ;==========================================================================================
Func _Base64Encode($sData)
    Local $oXml = ObjCreate("Msxml2.DOMDocument")
    If Not IsObj($oXml) Then
        SetError(1, 1, 0)
    EndIf

    Local $oElement = $oXml.createElement("b64")
    If Not IsObj($oElement) Then
        SetError(2, 2, 0)
    EndIf

    $oElement.dataType = "bin.base64"
    $oElement.nodeTypedValue = Binary($sData)
    Local $sReturn = $oElement.Text

    If StringLen($sReturn) = 0 Then
        SetError(3, 3, 0)
    EndIf

    Return $sReturn
EndFunc

; #FUNCTION# ;===============================================================================
;
; Name...........: _Base64Decode
; Description ...: Returns the strinng decoded from the provided Base64 string.
; Syntax.........: _Base64Decode($sData)
; Parameters ....: $sData
; Return values .: Success - String decoded from Base64.
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
;                  |1 - Could not create DOMDocument
;                  |2 - Could not create Element
;                  |3 - No string to return
; Author ........: turbov21
; Modified.......:
; Remarks .......:
; Related .......: _Base64Encode
; Link ..........;
; Example .......; Yes
;
; ;==========================================================================================
Func _Base64Decode($sData)
    Local $oXml = ObjCreate("Msxml2.DOMDocument")
    If Not IsObj($oXml) Then
        SetError(1, 1, 0)
    EndIf

    Local $oElement = $oXml.createElement("b64")
    If Not IsObj($oElement) Then
        SetError(2, 2, 0)
    EndIf

    $oElement.dataType = "bin.base64"
    $oElement.Text = $sData
    Local $sReturn = BinaryToString($oElement.nodeTypedValue, 4)

    If StringLen($sReturn) = 0 Then
        SetError(3, 3, 0)
    EndIf

    Return $sReturn
EndFunc

Func _GDIPlus_StreamImage2BinaryString($hBitmap, $sFormat = "JPG", $iQuality = 80, $bSave = False, $sFilename = @ScriptDir & "\Converted.jpg") ;coded by UEZ 2013 build 2014-01-25 (based on the code by Andreik)
    Local $sImgCLSID, $tGUID, $tParams, $tData
    Switch $sFormat
        Case "JPG"
            $sImgCLSID = _GDIPlus_EncodersGetCLSID($sFormat)
            $tGUID = _WinAPI_GUIDFromString($sImgCLSID)
            $tData = DllStructCreate("int Quality")
            DllStructSetData($tData, "Quality", $iQuality) ;quality 0-100
            Local $pData = DllStructGetPtr($tData)
            $tParams = _GDIPlus_ParamInit(1)
            _GDIPlus_ParamAdd($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, $pData)
        Case "PNG", "BMP", "GIF", "TIF"
            $sImgCLSID = _GDIPlus_EncodersGetCLSID($sFormat)
            $tGUID = _WinAPI_GUIDFromString($sImgCLSID)
        Case Else
            Return SetError(1, 0, 0)
    EndSwitch
    Local $hStream = _WinAPI_CreateStreamOnHGlobal() ;http://msdn.microsoft.com/en-us/library/ms864401.aspx
    If @error Then Return SetError(2, 0, 0)
    _GDIPlus_ImageSaveToStream($hBitmap, $hStream, DllStructGetPtr($tGUID), DllStructGetPtr($tParams))
    If @error Then Return SetError(3, 0, 0)

    Local $hMemory = _WinAPI_GetHGlobalFromStream($hStream) ;http://msdn.microsoft.com/en-us/library/aa911736.aspx
    If @error Then Return SetError(4, 0, 0)
    Local $iMemSize = _MemGlobalSize($hMemory)
    If Not $iMemSize Then Return SetError(5, 0, 0)
    Local $pMem = _MemGlobalLock($hMemory)
    $tData = DllStructCreate("byte[" & $iMemSize & "]", $pMem)
    Local $bData = DllStructGetData($tData, 1)
    _WinAPI_ReleaseStream($hStream) ;http://msdn.microsoft.com/en-us/library/windows/desktop/ms221473(v=vs.85).aspx
    _MemGlobalFree($hMemory)
    If $bSave Then
        Local $hFile = FileOpen($sFilename, 18)
        If @error Then Return SetError(6, 0, $bData)
        FileWrite($hFile, $bData)
        FileClose($hFile)
    EndIf
    Return $bData
EndFunc   ;==>_GDIPlus_StreamImage2BinaryString

 

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...