Hello,
I would need your assistance to work on images with a alpha layer.
Here my problem:
I charge an image with a layer alpha and it is impossible for me to add on this image of the text, the text which I writes above becomes transparent! Twisted Evil
Of course one can draw the image on the support and then to draw the text and there not problem but I need absolutely to add text on the image directly.
Have already work yourself above?
image alpha layer + text
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
I'm not 100% sure I get what you mean, but I think you want to draw semi-transparent text onto an image. If that's the ticket, there are several ways to do it. Here's one of the simplest, a homebrewed solution using nearly all PB native functions, just a touch of API:
Code: Select all
; Some mischief for a Friday evening...
; by netmaestro
Procedure ConvertToAlpha(image, transcolor, intensity)
Structure BGRA
blue.c
green.c
red.c
alpha.c
EndStructure
GetObject_(ImageID(image), SizeOf(BITMAP), bmp.BITMAP)
*bits.BGRA = bmp\bmBits
For i=0 To bmp\bmHeight-1
For j=0 To bmp\bmWidthBytes-1 Step 4
*px.BGRA = *bits + bmp\bmWidthBytes * i + j
If *px\blue=Blue(transcolor) And *px\green=Green(transcolor) And *px\red=Red(transcolor)
*px\alpha = 0
Else
*px\alpha = intensity
EndIf
Next
Next
EndProcedure
;================================================================================
; Step 1
; Create a 32-bit image to use for your text
; Make the background color close to the text color, just noticeably darker
; Draw your text on the background
;================================================================================
LoadFont(0, "Arial", 48, #PB_Font_Bold|#PB_Font_HighQuality)
CreateImage(0, 400, 70, 32)
StartDrawing(ImageOutput(0))
Box(0,0,400,70,RGB(230,0,0))
DrawingFont(FontID(0))
DrawText(0,0, "Alpha Text", #Red, RGB(230,0,0))
StopDrawing()
;================================================================================
; Step 2
; Apply an alpha value to all non-transparent pixels in your text image
; using the supplied ConvertToAlpha procedure
;================================================================================
ConvertToAlpha(0, RGB(230,0,0), 90)
;================================================================================
; Step 3
; Create or load an image of any desired depth to draw the text on
; here we'll just create one
;================================================================================
CreateImage(1, 600,400)
StartDrawing(ImageOutput(1))
Box(0,0,200,200,#White)
Box(200,0,200,200,#Gray)
Box(400,0,200,200,#White)
Box(0,200,200,200,#Gray)
Box(200,200,200,200,#White)
Box(400,200,200,200,#Gray)
StopDrawing()
;================================================================================
; Step 4
; Use PureBasic's DrawAlphaImage command to imprint the text on the image
;================================================================================
StartDrawing(ImageOutput(1))
DrawAlphaImage(ImageID(0), 110, 160)
StopDrawing()
;================================================================================
; You're finished! Give it a test:
;================================================================================
OpenWindow(0,0,0,600,400,"",$CA0001)
CreateGadgetList(WindowID(0))
ImageGadget(0,0,0,0,0,ImageID(1))
Repeat:Until WaitWindowEvent()=#WM_CLOSE
BERESHEIT
Thank's for the code but my problem is :
I have a png image with alpha, I wants to add text on this image but the text become transparent!!
I have a png image with alpha, I wants to add text on this image but the text become transparent!!
Code: Select all
UsePNGImageDecoder()
Global OriginProc.l
LoadFont (1, "Arial", 30)
LoadImage(0,"c:\image.png")
hdc =StartDrawing(ImageOutput(0))
If hdc
SetBkMode_(hdc,#TRANSPARENT)
SetTextColor_(hdc,RGB(255,255,0))
texte.s="Nico"
DrawingFont(FontID(1))
DrawText(10,20,"texte")
StopDrawing()
EndIf
Procedure WindowCallback(Window, message, wParam, lParam)
Select message
Case #WM_PAINT
Debug "#WM_PAINT"
ps.PAINTSTRUCT
BeginPaint_(Window,ps)
EndPaint_(Window,ps)
hdc =StartDrawing(WindowOutput(0))
If hdc
DrawAlphaImage(ImageID(0),0,0)
StopDrawing()
EndIf
ProcedureReturn 0
EndSelect
ProcedureReturn CallWindowProc_(OriginProc,Window, message, wParam, lParam)
EndProcedure
If OpenWindow(0, 100, 200, 400, 400, "PureBasic Window", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
OriginProc=SetWindowLong_(WindowID(0),#GWL_WNDPROC,@WindowCallback())
CreateGadgetList(WindowID(0))
Repeat
EventID = WaitWindowEvent()
If EventID = #PB_Event_CloseWindow ; If the user has pressed on the close button
Quit = 1
EndIf
Until Quit = 1
EndIf
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Ok, good. The problem is that the alpha values in the text portion are zeroed out, causing the text pixels to be ignored by the DrawAlphaImage. The code I already gave you can be used to solve it, just added to what you already have:
Seems to give the desired result.
Code: Select all
Procedure ConvertToAlpha(image, transcolor, intensity)
Structure BGRA
blue.c
green.c
red.c
alpha.c
EndStructure
GetObject_(ImageID(image), SizeOf(BITMAP), bmp.BITMAP)
*bits.BGRA = bmp\bmBits
For i=0 To bmp\bmHeight-1
For j=0 To bmp\bmWidthBytes-1 Step 4
*px.BGRA = *bits + bmp\bmWidthBytes * i + j
If *px\blue=Blue(transcolor) And *px\green=Green(transcolor) And *px\red=Red(transcolor)
*px\alpha = 0
Else
*px\alpha = intensity
EndIf
Next
Next
EndProcedure
UsePNGImageDecoder()
Global OriginProc.l
LoadFont (1, "Arial", 30)
LoadImage(0,"image.png")
CreateImage(1, 85,40,32)
StartDrawing(ImageOutput(1))
DrawingFont(FontID(1))
Box(0,0,85,40,RGB(220,220,0))
DrawText(0,0,"texte",RGB(255,255,0),RGB(220,220,0))
StopDrawing()
ConvertToAlpha(1, RGB(220,220,0),255)
StartDrawing(ImageOutput(0))
DrawAlphaImage(ImageID(1),10,24)
StopDrawing()
Procedure WindowCallback(Window, message, wParam, lParam)
Select message
Case #WM_PAINT
ps.PAINTSTRUCT
BeginPaint_(Window,ps)
EndPaint_(Window,ps)
hdc =StartDrawing(WindowOutput(0))
If hdc
DrawAlphaImage(ImageID(0),0,0)
StopDrawing()
EndIf
ProcedureReturn 0
EndSelect
ProcedureReturn CallWindowProc_(OriginProc,Window, message, wParam, lParam)
EndProcedure
If OpenWindow(0, 100, 200, 400, 400, "PureBasic Window", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
OriginProc=SetWindowLong_(WindowID(0),#GWL_WNDPROC,@WindowCallback())
CreateGadgetList(WindowID(0))
Repeat
EventID = WaitWindowEvent()
If EventID = #PB_Event_CloseWindow ; If the user has pressed on the close button
Quit = 1
EndIf
Until Quit = 1
EndIf
BERESHEIT