Jump to content

How to insert picture to Rich Edit


Recommended Posts

Try this:

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <GuiRichEdit.au3>
#include <WindowsConstants.au3>

Example()

Func Example()
    Local $hGui, $iMsg, $idBtnExit, $hRichEdit
    $hGui = GUICreate("Example (" & StringTrimRight(@ScriptName, StringLen(".exe")) & ")", 520, 550, -1, -1)
    $hRichEdit = _GUICtrlRichEdit_Create($hGui, "This is a test.", 10, 10, 500, 490, _
            BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
    $idBtnExit = GUICtrlCreateButton("Exit", 10, 510, 40, 30)
    GUISetState(@SW_SHOW)

    _GUICtrlRichEdit_InsertText($hRichEdit, "Inserting image..." & @LF & @LF)
    _GUICtrlRichEdit_InsertText($hRichEdit, @LF & "JPG image scaled:" & @LF & @LF)
    _GUICtrlRichEdit_InsertBitmap($hRichEdit, "c:\Program Files (x86)\AutoIt3\Examples\GUI\mslogo.jpg", "\qc", "\picw6747\pich1058\picwgoal6690\pichgoal1860\") ;\qc = centered
    _GUICtrlRichEdit_InsertText($hRichEdit, @LF & @LF & "PNG image:" & @LF & @LF)
    _GUICtrlRichEdit_InsertBitmap($hRichEdit, "c:\Program Files (x86)\AutoIt3\Examples\GUI\Torus.png")
    _GUICtrlRichEdit_InsertText($hRichEdit, @LF & @LF & "Done.")

    While True
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idBtnExit
                _GUICtrlRichEdit_Destroy($hRichEdit) ; needed unless script crashes
                GUIDelete()
                Exit
        EndSwitch
    WEnd
EndFunc   ;==>Example

Func _GUICtrlRichEdit_InsertBitmap($hWnd, $sFile, $sFormatFunctions = "\", $sBitmapFunctions = "\", $iBgColor = Default) ;coded by UEZ build 2016-02-16
    If Not FileExists($sFile) Then Return SetError(0, 0, 1)
    If Not _WinAPI_IsClassName($hWnd, $__g_sRTFClassName) Then Return SetError(0, 0, 2)
    _GDIPlus_Startup()
    Local $hImage = _GDIPlus_ImageLoadFromFile($sFile)
    If @error Then
        _GDIPlus_Shutdown()
        Return SetError(0, 0, 3)
    EndIf
    Local Const $aDim = _GDIPlus_ImageGetDimension($hImage)
    Local Const $hBitmap = _GDIPlus_BitmapCreateFromScan0($aDim[0], $aDim[1]), $hGfx = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    If $iBgColor = Default Then
        $iBgColor = 0xFF000000 + _WinAPI_SwitchColor(_GUICtrlRichEdit_GetBkColor($hWnd))
    EndIf
    _GDIPlus_GraphicsClear($hGfx, $iBgColor)
    _GDIPlus_GraphicsDrawImageRect($hGfx, $hImage, 0, 0, $aDim[0], $aDim[1])
    _GDIPlus_GraphicsDispose($hGfx)
    Local $binStream = _GDIPlus_StreamImage2BinaryString($hBitmap, "BMP")
    If @error Then
        _GDIPlus_ImageDispose($hImage)
        _GDIPlus_ImageDispose($hBitmap)
        _GDIPlus_Shutdown()
        Return SetError(0, 0, 4)
    EndIf
    Local $binBmp = StringMid($binStream, 31)
    Local Const $binRtf = "{\rtf1\viewkind4" & $sFormatFunctions & " {\pict{\*\picprop}" & $sBitmapFunctions & "dibitmap " & $binBmp & "}\par}" ;check out http://www.biblioscape.com/rtf15_spec.htm
    _GUICtrlRichEdit_AppendText($hWnd, $binRtf)
    $binStream = 0
    $binBmp = 0
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_ImageDispose($hBitmap)
    _GDIPlus_Shutdown()
    Return 1
EndFunc   ;==>_GUICtrlRichEdit_InsertBitmap

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

Probably there is a much better solution...

Edited by UEZ
Added background color for images to avoid black background on transparent bitmaps

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

Thanks AutoBert. Transparent PNG images currently not supported (black background).

 

Currently I'm searching for a solution to handle also transparent PNG images using Metafiles...:think:

 

But now I go to sleep (6.5h) ;)

 

Edited by UEZ

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

12 hours ago, UEZ said:

Try this:

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <GuiRichEdit.au3>
#include <WindowsConstants.au3>

Example()

Func Example()
    Local $hGui, $iMsg, $idBtnExit, $hRichEdit
    $hGui = GUICreate("Example (" & StringTrimRight(@ScriptName, StringLen(".exe")) & ")", 520, 550, -1, -1)
    $hRichEdit = _GUICtrlRichEdit_Create($hGui, "This is a test.", 10, 10, 500, 490, _
            BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
    $idBtnExit = GUICtrlCreateButton("Exit", 10, 510, 40, 30)
    GUISetState(@SW_SHOW)

    _GUICtrlRichEdit_InsertText($hRichEdit, "Inserting image..." & @LF & @LF)
    _GUICtrlRichEdit_InsertText($hRichEdit, @LF & "JPG image scaled:" & @LF & @LF)
    _GUICtrlRichEdit_InsertBitmap($hRichEdit, "c:\Program Files (x86)\AutoIt3\Examples\GUI\mslogo.jpg", "\qc", "\picw6747\pich1058\picwgoal6690\pichgoal1860\") ;\qc = centered
    _GUICtrlRichEdit_InsertText($hRichEdit, @LF & @LF & "PNG image:" & @LF & @LF)
    _GUICtrlRichEdit_InsertBitmap($hRichEdit, "c:\Program Files (x86)\AutoIt3\Examples\GUI\Torus.png")
    _GUICtrlRichEdit_InsertText($hRichEdit, @LF & @LF & "Done.")

    While True
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idBtnExit
                _GUICtrlRichEdit_Destroy($hRichEdit) ; needed unless script crashes
                GUIDelete()
                Exit
        EndSwitch
    WEnd
EndFunc   ;==>Example

Func _GUICtrlRichEdit_InsertBitmap($hWnd, $sFile, $sFormatFunctions = "\", $sBitmapFunctions = "\") ;coded by UEZ build 2016-02-16
    If Not FileExists($sFile) Then Return SetError(0, 0, 1)
    If Not _WinAPI_IsClassName($hWnd, $__g_sRTFClassName) Then Return SetError(0, 0, 2)
    _GDIPlus_Startup()
    Local $hImage = _GDIPlus_ImageLoadFromFile($sFile)
    If @error Then
        _GDIPlus_Shutdown()
        Return SetError(0, 0, 3)
    EndIf
    Local Const $aDim = _GDIPlus_ImageGetDimension($hImage)
    Local Const $hBitmap = _GDIPlus_BitmapCreateFromScan0($aDim[0], $aDim[1]), $hGfx = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsClear($hGfx, 0xFFFFFFFF)
    _GDIPlus_GraphicsDrawImageRect($hGfx, $hImage, 0, 0, $aDim[0], $aDim[1])
    _GDIPlus_GraphicsDispose($hGfx)
    Local $binStream = _GDIPlus_StreamImage2BinaryString($hBitmap, "BMP")
    If @error Then
        _GDIPlus_ImageDispose($hImage)
        _GDIPlus_ImageDispose($hBitmap)
        _GDIPlus_Shutdown()
        Return SetError(0, 0, 4)
    EndIf
    Local $binBmp = StringMid($binStream, 31)
    Local Const $binRtf = "{\rtf1\viewkind4" & $sFormatFunctions & " {\pict{\*\picprop}" & $sBitmapFunctions & "dibitmap " & $binBmp & "}\par}" ;check out http://www.biblioscape.com/rtf15_spec.htm
    _GUICtrlRichEdit_AppendText($hWnd, $binRtf)
    $binStream = 0
    $binBmp = 0
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_ImageDispose($hBitmap)
    _GDIPlus_Shutdown()
    Return 1
EndFunc   ;==>_GUICtrlRichEdit_InsertBitmap

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

Probably there is a much better solution...

tks u, I love you <3

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...