mutleey Posted 16 hours ago Posted 16 hours ago Looking at a recent post about text in GDI+, I decided to test a script to insert text into an image in Pic. It works, but the image loses transparency. Any help is welcome. expandcollapse popup#include <GDIPlus.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> _GDIPlus_Startup() Global $hImage, $hBitmap Global $hGUI = GUICreate("", 84, 74, -1, -1, $WS_POPUP, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST)) Local $__pic = GUICtrlCreatePic("", 0, 0, 84, 74) GUICtrlSetState(-1, $GUI_DISABLE) GUISetState() $hImage = _GDIPlus_BitmapCreateFromFile(@ScriptDir & "\cart.png") _add_TEXT_to_IMG($hImage, String(5), 27, 5) $hBitmap = _GDIPlus_BitmapCreateDIBFromBitmap($hImage) __GUICtrlSendMsg($__pic, $hBitmap) _GDIPlus_BitmapDispose($hImage) _WinAPI_DeleteObject($hBitmap) Do Until GUIGetMsg() = $GUI_EVENT_CLOSE GUIDelete() _GDIPlus_Shutdown() Func __GUICtrlSendMsg($picCtrl, $imgPic) Local Const $STM_SETIMAGE = 0x0172 Local $msgRtrn = GUICtrlSendMsg($picCtrl, $STM_SETIMAGE, $IMAGE_BITMAP, $imgPic) If $msgRtrn Then _WinAPI_DeleteObject($msgRtrn) EndIf EndFunc Func _add_TEXT_to_IMG($hImage, $sText, $iX = 5, $iY = 70, $sFontName = "Arial", $fSize = 5, $fStyle = 1, $iColor = 0xFFFFFFFF) Local Const $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage) _GDIPlus_GraphicsSetSmoothingMode($hGraphic, 4) _GDIPlus_GraphicsSetTextRenderingHint($hGraphic, 3) Local Const $hBrush = _GDIPlus_BrushCreateSolid($iColor) Local Const $hFormat = _GDIPlus_StringFormatCreate() _GDIPlus_StringFormatSetAlign($hFormat, 1) ; Center text horizontally Local Const $hFamily = _GDIPlus_FontFamilyCreate($sFontName) Local Const $hFont = _GDIPlus_FontCreate($hFamily, $fSize, $fStyle) Local Const $aDim = _GDIPlus_ImageGetDimension($hImage) Local Const $tLayout = _GDIPlus_RectFCreate($iX, $iY, $aDim[0], $aDim[1]) _GDIPlus_GraphicsDrawStringEx($hGraphic, $sText, $hFont, $tLayout, $hFormat, $hBrush) _GDIPlus_FontDispose($hFont) _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_BrushDispose($hBrush) _GDIPlus_GraphicsDispose($hGraphic) EndFunc ;==>_add_TEXT_to_IMG
UEZ Posted 7 hours ago Posted 7 hours ago (edited) For me it works. Try GUISetBkColor(0xFFFF00) to see if it really loses transparency! The background should be yellow otherwise transparency lost. Edited 5 hours ago by UEZ mutleey 1 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
ioa747 Posted 7 hours ago Posted 7 hours ago (edited) oops Edited 6 hours ago by ioa747 mutleey 1 I know that I know nothing
mutleey Posted 4 hours ago Author Posted 4 hours ago 2 hours ago, UEZ said: For me it works. Try GUISetBkColor(0xFFFF00) to see if it really loses transparency! The background should be yellow otherwise transparency lost. It was really my mistake, after putting the yellow background I could see that it was working. Thanks UEZ
ioa747 Posted 4 hours ago Posted 4 hours ago (edited) expandcollapse popup; https://www.autoitscript.com/forum/topic/213213-text-in-gdi-images-loses-transparency/#findComment-1546336 #include <WinAPIConstants.au3> #include <GDIPlus.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <WinAPISysWin.au3> _GDIPlus_Startup() Global $iW, $iH, $hImage, $hGUI $hImage = _GDIPlus_BitmapCreateFromFile(@ScriptDir & "\cart.png") $iW = _GDIPlus_ImageGetWidth($hImage) $iH = _GDIPlus_ImageGetHeight($hImage) $hGUI = GUICreate("", $iW, $iH, -1, -1, $WS_POPUP, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST, $WS_EX_LAYERED)) GUISetState() _add_TEXT_to_IMG($hImage, String(5), 58, 5) ; Convert the MODIFIED GDI+ image to HBITMAP Local $hBitmapToDisplay = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage) ; Display the new HBITMAP transparently _WinAPI_BitmapDisplayTransparentInGUI($hBitmapToDisplay, $hGUI) Do Until GUIGetMsg() = $GUI_EVENT_CLOSE ; Cleanup ; _WinAPI_BitmapDisplayTransparentInGUI already deleted $hBitmapToDisplay _GDIPlus_BitmapDispose($hImage) _GDIPlus_Shutdown() GUIDelete() Func _add_TEXT_to_IMG(ByRef $hImage, $sText, $iX = 5, $iY = 70, $sFontName = "Arial", $fSize = 5, $fStyle = 1, $iColor = 0xFFFFFFFF) Local Const $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage) _GDIPlus_GraphicsSetSmoothingMode($hGraphic, 4) _GDIPlus_GraphicsSetTextRenderingHint($hGraphic, 3) Local Const $hBrush = _GDIPlus_BrushCreateSolid($iColor) Local Const $hFormat = _GDIPlus_StringFormatCreate() ;~ _GDIPlus_StringFormatSetAlign($hFormat, 1) ; Center text horizontally _GDIPlus_StringFormatSetAlign($hFormat, 0) ; Left Align for better text control Local Const $hFamily = _GDIPlus_FontFamilyCreate($sFontName) Local Const $hFont = _GDIPlus_FontCreate($hFamily, $fSize, $fStyle) Local Const $aDim = _GDIPlus_ImageGetDimension($hImage) ;~ Local Const $tLayout = _GDIPlus_RectFCreate($iX, $iY, $aDim[0], $aDim[1]) Local Const $tLayout = _GDIPlus_RectFCreate($iX, $iY, $aDim[0] - $iX, $aDim[1] - $iY) ; Adjusted layout for better text control _GDIPlus_GraphicsDrawStringEx($hGraphic, $sText, $hFont, $tLayout, $hFormat, $hBrush) _GDIPlus_FontDispose($hFont) _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_BrushDispose($hBrush) _GDIPlus_GraphicsDispose($hGraphic) EndFunc ;==>_add_TEXT_to_IMG Func _WinAPI_BitmapDisplayTransparentInGUI(ByRef $hHBitmap, ByRef $hGUI, $iOpacity = 0xFF, $bReleaseGDI = True) ; (by @UEZ) If Not BitAND(GUIGetStyle($hGUI)[1], $WS_EX_LAYERED) = $WS_EX_LAYERED Then Return SetError(1, 0, 0) Local $tDim = DllStructCreate($tagBITMAP) If Not _WinAPI_GetObject($hHBitmap, DllStructGetSize($tDim), DllStructGetPtr($tDim)) Then Return SetError(2, 0, 0) Local $tSize = DllStructCreate($tagSIZE), $tSource = DllStructCreate($tagPOINT), $tBlend = DllStructCreate($tagBLENDFUNCTION) Local Const $hScrDC = _WinAPI_GetDC(0), $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC), $hOld = _WinAPI_SelectObject($hMemDC, $hHBitmap) $tSize.X = $tDim.bmWidth $tSize.Y = $tDim.bmHeight $tBlend.Alpha = $iOpacity $tBlend.Format = 1 _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, DllStructGetPtr($tSize), $hMemDC, DllStructGetPtr($tSource), 0, DllStructGetPtr($tBlend), $ULW_ALPHA) _WinAPI_ReleaseDC(0, $hScrDC) _WinAPI_SelectObject($hMemDC, $hOld) _WinAPI_DeleteDC($hMemDC) If $bReleaseGDI Then _WinAPI_DeleteObject($hHBitmap) Return True EndFunc ;==>_WinAPI_BitmapDisplayTransparentInGUI Edited 3 hours ago by ioa747 added ByRef $hImage argumentum and mutleey 2 I know that I know nothing
ioa747 Posted 3 hours ago Posted 3 hours ago I did it as an exercise, (learning process), and since it concerns your work, I thought I would share my findings. I know that I know nothing
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now