Using DrawText() API with 32-bit images [WINDOWS]
Posted: Sat Dec 24, 2016 5:48 pm
Hi there,
I am attempting to use Windows' DrawText_() API with 32-bit images, but the alpha channel of the text is not rendered correctly. I've borrowed some code from http://www.purebasic.fr/english/viewtop ... =4&t=54045 to help illustrate the issue:
I've used the same routine to render text in 24-bit and 32-bit images and then repeated the process, but with a magenta background as below:
http://www.dfstudios.co.uk/tmp/drawtext.png
I also noted that Danilo posted a solution to the issue at http://www.purebasic.fr/english/viewtop ... api+winapi, but this only works if the background that the text is being drawn on is not transparent. I'm looking to be able to draw the text on transparent backgrounds.
Kind regards,
Francis
I am attempting to use Windows' DrawText_() API with 32-bit images, but the alpha channel of the text is not rendered correctly. I've borrowed some code from http://www.purebasic.fr/english/viewtop ... =4&t=54045 to help illustrate the issue:
Code: Select all
Procedure DrawTextAPI(hndDeviceContext, x, y, Text$, FontID, FrontColor, BackColor, Mode = #OPAQUE)
Protected PrevFID = SelectObject_(hndDeviceContext, FontID)
Protected DrawRect.RECT
DrawRect\left = x
DrawRect\top = y
DrawRect\right = OutputWidth()
DrawRect\bottom = OutputHeight()
SetBkColor_(hndDeviceContext, BackColor)
SetTextColor_(hndDeviceContext, FrontColor)
SetBkMode_(hndDeviceContext, Mode)
DrawText_(hndDeviceContext, Text$, -1, @DrawRect, #DT_SINGLELINE | #DT_NOPREFIX)
SelectObject_(hndDeviceContext, PrevFID)
EndProcedure
Procedure Rebuild()
x = 10
y = 10
w = WindowWidth(0) - x << 1
h = 20
TestString.s = "Hello World!"
Front.i = GetSysColor_(#COLOR_MENUTEXT)
Back.i = GetSysColor_(#COLOR_MENU)
ButtonGadget(20, x, y, 100, 25, "Font...")
y + 30
TextGadget(0, x, y, w, h, "")
If (IsFont(0))
SetGadgetFont(0, FontID(0))
EndIf
FID.i = GetGadgetFont(0)
If (StartDrawing(WindowOutput(0)))
DrawingFont(FID)
h = TextHeight(TestString)
ResizeGadget(0, x, y, w, h)
y + h
StopDrawing()
EndIf
; Display DrawText_() ouput on 24-bit image and then 32-bit image, then repeat with a magenta background
For i = 1 To 4
If i % 2 : Depth = 24 : Else : Depth = 32 : EndIf
If i > 2 : bgColor = #Magenta : Else : bgColor = Back : EndIf
If CreateImage(i, w, h, Depth, bgColor)
hDC = StartDrawing(ImageOutput(i))
If hDC
DrawTextAPI(hDC, 0, 0, TestString + " ... DrawText_() API on " + Str(Depth) + "-bit image", FID, Front, Back, #TRANSPARENT)
StopDrawing()
EndIf
ImageGadget(i, x, y, w, h, ImageID(i))
y + h
EndIf
Next
EndProcedure
OpenWindow(0, 0, 0, 480, 360, "DrawText", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
Rebuild()
Repeat
Event = WaitWindowEvent()
If (Event = #PB_Event_CloseWindow)
Done = #True
ElseIf (Event = #PB_Event_Gadget) And (EventGadget() = 20)
If FontRequester("Arial", 12, 0)
LoadFont(0, SelectedFontName(), SelectedFontSize())
Rebuild()
EndIf
EndIf
Until Done
http://www.dfstudios.co.uk/tmp/drawtext.png
I also noted that Danilo posted a solution to the issue at http://www.purebasic.fr/english/viewtop ... api+winapi, but this only works if the background that the text is being drawn on is not transparent. I'm looking to be able to draw the text on transparent backgrounds.
Kind regards,
Francis