Code: Select all
pToken := Gdip_Startup()
Gui, Font, s12, Consolas
Gui, Add, Text, hWndTXThWnd, Hello World
Gui, Show, w200 h100
File = %A_ScriptDir%\pbitmap.png
DllCall("gdiplus\GdipCreateBitmapFromFile", "uint", File, "uint*", pBitmap)
DrawTXT(PBITMAP, "Hello World", 0xB3FFFFFF, TXThWnd)
Gdip_SaveBitmapToFile(pBitmap, "txt_pbitmap.png", 100)
Return
DrawTxt(PBITMAP, TXT, TXTCOLOR, HWND) {
; Get the pointer to the bitmap graphics.
DllCall("Gdiplus.dll\GdipGetImageGraphicsContext", "Ptr", PBITMAP, "PtrP", PGRAPHICS)
; Create a StringFormat object
DllCall("Gdiplus.dll\GdipStringFormatGetGenericTypographic", "PtrP", HFORMAT)
; Fill the text color
;TxtColor := GetARGB(TxtColor)
TxtColor := 0xB3FFFFFF
DllCall("Gdiplus.dll\GdipCreateSolidFill", "UInt", TxtColor, "PtrP", PBRUSH)
; 0x01 Center
DllCall("Gdiplus.dll\GdipSetStringFormatAlign", "Ptr", HFORMAT, "Int", 0x01)
; Vertical alignment 1 - Middle
DllCall("Gdiplus.dll\GdipSetStringFormatLineAlign", "Ptr", HFORMAT, "Int", 1)
; Set render quality to system default
DllCall("Gdiplus.dll\GdipSetTextRenderingHint", "Ptr", PGRAPHICS, "Int", 0)
VarSetCapacity(RECT, 16, 0)
NumPut(0, RECT, 0, "Float")
NumPut(0, RECT, 4, "Float")
DllCall("gdiplus\GdipGetImageDimension", Ptr, pBitmap, "Float*", W, "Float*", H)
FileAppend, W: %W% H: %H%`n,*
NumPut(W, RECT, 8, "Float")
NumPut(H, RECT, 12, "Float")
; Use the FONT from the given control
HFONT := DllCall("User32.dll\SendMessage", "Ptr", HWND, "UInt", 0x31, "Ptr", 0, "Ptr", 0, "Ptr")
DC := DllCall("User32.dll\GetDC", "Ptr", HWND, "Ptr")
DllCall("Gdi32.dll\SelectObject", "Ptr", DC, "Ptr", HFONT)
DllCall("Gdiplus.dll\GdipCreateFontFromDC", "Ptr", DC, "PtrP", PFONT)
DllCall("User32.dll\ReleaseDC", "Ptr", HWND, "Ptr", DC)
DllCall("Gdiplus.dll\GdipDeleteFont", "Ptr", HFONT)
DllCall("Gdiplus.dll\GdipDrawString", "Ptr", PGRAPHICS, "WStr", Txt, "Int", -1, "Ptr", PFONT, "Ptr", &RECT, "Ptr", HFORMAT, "Ptr", PBRUSH)
;DeleteObject(PFONT)
DllCall("Gdiplus.dll\GdipDeleteBrush", "Ptr", PBRUSH)
DllCall("Gdiplus.dll\GdipDeleteStringFormat", "Ptr", HFORMAT)
DllCall("Gdiplus.dll\GdipDeleteGraphics", "Ptr", PGRAPHICS)
}
GetARGB(RGB) {
ARGB := This.HTML.HasKey(RGB) ? This.HTML[RGB] : RGB
Return (ARGB & 0xFF000000) = 0 ? 0xFF000000 | ARGB : ARGB
}
Gdip_Startup(multipleInstances:=0) {
Static Ptr := "UPtr"
pToken := 0
If (multipleInstances=0)
{
if !DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
DllCall("LoadLibrary", "str", "gdiplus")
} Else DllCall("LoadLibrary", "str", "gdiplus")
VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0), si := Chr(1)
DllCall("gdiplus\GdiplusStartup", "UPtr*", pToken, Ptr, &si, Ptr, 0)
return pToken
}
Gdip_SaveBitmapToFile(pBitmap, sOutput, Quality:=75) {
Static Ptr := "UPtr"
nCount := 0
nSize := 0
_p := 0
SplitPath sOutput,,, Extension
If !RegExMatch(Extension, "^(?i:BMP|DIB|RLE|JPG|JPEG|JPE|JFIF|GIF|TIF|TIFF|PNG)$")
Return -1
Extension := "." Extension
DllCall("gdiplus\GdipGetImageEncodersSize", "uint*", nCount, "uint*", nSize)
VarSetCapacity(ci, nSize)
DllCall("gdiplus\GdipGetImageEncoders", "uint", nCount, "uint", nSize, Ptr, &ci)
If !(nCount && nSize)
Return -2
If (A_IsUnicode) {
StrGet_Name := "StrGet"
N := (A_AhkVersion < 2) ? nCount : "nCount"
Loop %N%
{
sString := %StrGet_Name%(NumGet(ci, (idx := (48+7*A_PtrSize)*(A_Index-1))+32+3*A_PtrSize), "UTF-16")
If !InStr(sString, "*" Extension)
Continue
pCodec := &ci+idx
Break
}
}
Else {
N := (A_AhkVersion < 2) ? nCount : "nCount"
Loop %N%
{
Location := NumGet(ci, 76*(A_Index-1)+44)
nSize := DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "uint", 0, "int", 0, "uint", 0, "uint", 0)
VarSetCapacity(sString, nSize)
DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "str", sString, "int", nSize, "uint", 0, "uint", 0)
If !InStr(sString, "*" Extension)
Continue
pCodec := &ci+76*(A_Index-1)
Break
}
}
If !pCodec
Return -3
If (Quality!=75) {
Quality := (Quality < 0) ? 0 : (Quality > 100) ? 100 : Quality
If (quality>90)
Quality := 90
If RegExMatch(Extension, "^\.(?i:JPG|JPEG|JPE|JFIF)$")
{
DllCall("gdiplus\GdipGetEncoderParameterListSize", Ptr, pBitmap, Ptr, pCodec, "uint*", nSize)
VarSetCapacity(EncoderParameters, nSize, 0)
DllCall("gdiplus\GdipGetEncoderParameterList", Ptr, pBitmap, Ptr, pCodec, "uint", nSize, Ptr, &EncoderParameters)
nCount := NumGet(EncoderParameters, "UInt")
N := (A_AhkVersion < 2) ? nCount : "nCount"
Loop %N%
{
elem := (24+A_PtrSize)*(A_Index-1) + 4 + (pad := A_PtrSize = 8 ? 4 : 0)
If (NumGet(EncoderParameters, elem+16, "UInt") = 1) && (NumGet(EncoderParameters, elem+20, "UInt") = 6)
{
_p := elem+&EncoderParameters-pad-4
NumPut(Quality, NumGet(NumPut(4, NumPut(1, _p+0)+20, "UInt")), "UInt")
Break
}
}
}
}
_E := DllCall("gdiplus\GdipSaveImageToFile", Ptr, pBitmap, "WStr", sOutput, Ptr, pCodec, "uint", _p ? _p : 0)
Return _E ? -5 : 0
}
I have added the functions it was missing, its just Gdip_SaveBitmapToFile() and Gdip_Startup().
Comparing it with the image on my first post where i wrote (It would look like this) the text is not 'visible trought'.
Its because the transparency too need to be applied to the background of the image?