Jump to content

Encode image in memory without saving to file [Save image to stream]


Chance
 Share

Go to solution Solved by Chance,

Recommended Posts

_GDIPlus_Startup()
$hImage = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
$hBrush = _GDIPlus_BrushCreateSolid(0xFF000000)
_GDIPlus_GraphicsFillRect($hGraphic, 0, 0, $iWidth, $iHeight, $hBrush)
$hFormat = _GDIPlus_StringFormatCreate()
$hFamily = _GDIPlus_FontFamilyCreate("OCR A Extended")
$hFont = _GDIPlus_FontCreate($hFamily, $FontSize, 0)
$tLayout = _GDIPlus_RectFCreate(10, 10, 0, 0)
$aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
$hBrush = _GDIPlus_BrushCreateSolid(0xFF37FF00)
_GDIPlus_GraphicsDrawStringEx($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush)
$hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
_WinAPI_DeleteObject($hBitmap)
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_GraphicsDispose($hGraphic)
$TempImg = _TempFile(@ScriptDir, "", ".png")
_GDIPlus_ImageSaveToFile($hImage, $TempImg)
$NewImage = FileRead($TempImage)

I'd like to get rid of this

 

$TempImg = _TempFile(@ScriptDir, "", ".png")
_GDIPlus_ImageSaveToFile($hImage, $TempImg)
$NewImage = FileRead($TempImage)

For example, I'm looking to encode the image object from bmp (as I think it is) to PNG, jpg, jpeg, tiff etc without actually having to save the image to the hard drive and then re-reading it to memory.

I've failed to locate anything like this on the forums...

Edited by FlutterShy
Link to comment
Share on other sites

Select what you want to get rid of, and hit the delete key.

use $hImage however you see fit.

Ok.

My point is I'd like to figure out how to convert an image in memory from bmp to Jpg, png or gif, any would work fine, so far I only know how to do it by saving the file.

On a side note, the other day you asked why you haven't become an MVP, maybe look at some of your posts to get some insight.

Edited by FlutterShy
Link to comment
Share on other sites

  • Solution

What have you "figured out" so far?

I've been looking at some C++ function that did this by saving an image to a stream and doing it in memory, lead me to a post by the one and only UEZ, modified the function I found and surprisingly enough, it's exactly what I needed.

Func _GDIPlus_SaveImageToStream($hBitmap, $iQuality = 50, $Encoder = "jpg") ;coded by Andreik, modified by UEZ, FS
    Local $tParams
    Local $tData
    Local $pData
    Local $pParams

    Local $sImgCLSID = _GDIPlus_EncodersGetCLSID($Encoder)
    Local $tGUID = _WinAPI_GUIDFromString($sImgCLSID)
    Local $pEncoder = DllStructGetPtr($tGUID)

    If $Encoder == "jpg" Or $Encoder == "jpeg" Then
        $tParams = _GDIPlus_ParamInit(1)
        $tData = DllStructCreate("int Quality")
        DllStructSetData($tData, "Quality", $iQuality) ;quality 0-100
        $pData = DllStructGetPtr($tData)
        _GDIPlus_ParamAdd($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, $pData)
        $pParams = DllStructGetPtr($tParams)
    Else
        $pParams = 0
    EndIf

    Local $hStream = DllCall("ole32.dll", "uint", "CreateStreamOnHGlobal", "ptr", 0, "bool", True, "ptr*", 0)
    If @error Then Return SetError(1, 0, 0)
    $hStream = $hStream[3]
    DllCall($ghGDIPDll, "uint", "GdipSaveImageToStream", "ptr", $hBitmap, "ptr", $hStream, "ptr", $pEncoder, "ptr", $pParams)
    _GDIPlus_BitmapDispose($hBitmap)
    Local $hMemory = DllCall("ole32.dll", "uint", "GetHGlobalFromStream", "ptr", $hStream, "ptr*", 0)
    If @error Then Return SetError(2, 0, 0)

    $hMemory = $hMemory[2]
    Local $iMemSize = _MemGlobalSize($hMemory)
    Local $pMem = _MemGlobalLock($hMemory)

    $tData = DllStructCreate("byte[" & $iMemSize & "]", $pMem)
    Local $bData = DllStructGetData($tData, 1)
    Local $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)) ;http://msdn.microsoft.com/en-us/library/windows/desktop/ms221473(v=vs.85).aspx
    _MemGlobalFree($hMemory)

    Return $bData
EndFunc   ;==>_GDIPlus_SaveImageToStream

P.S. Thanks for the "help".

Edited by FlutterShy
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

×
×
  • Create New...