Code: Select all
; Watermark 1.o by Michael Vogel
Procedure.l CreateWatermarkMask(image.i,text.s,angle.i,fontname.s,fontsize.i,x.i=0,y.i=0)
Protected tx.i,ty.i
Protected alpha.f
Protected font.i
If x<=0
x=ImageWidth(image)
y=ImageHeight(image)
EndIf
image=CreateImage(#PB_Any,x,y,32)
If image
font=LoadFont(#PB_Any,fontname,fontsize)
StartDrawing(ImageOutput(Image))
DrawingMode(#PB_2DDrawing_AlphaChannel)
Box(0,0,x,y,0)
DrawingFont(FontID(font))
tx=TextWidth(text)>>1
ty=TextHeight(text)>>1
alpha=angle/180*#PI
ty = y>>1 + Sin(alpha)*(tx)+Cos(alpha)*ty-ty
tx = x>>1 - Cos(alpha)*tx
;DrawingMode(#PB_2DDrawing_Default); don't touch anything here :)
DrawRotatedText(tx,ty,text,angle); don't add a color here :––––)
StopDrawing()
FreeFont(font)
EndIf
ProcedureReturn Image
EndProcedure
Procedure.l SetWatermarkColor(image.i,color.i)
StartDrawing(ImageOutput(Image))
DrawingMode(#PB_2DDrawing_Default)
Box(0,0,ImageWidth(image),ImageHeight(image),color)
StopDrawing()
EndProcedure
Procedure DoWatermark(image.i,watermark.i,color.i,level.i,alpha.i=128)
Protected x.i,y.i
Protected ox.i,oy.i
SetWatermarkColor(watermark,color)
ox=(ImageWidth(image)-ImageWidth(watermark))>>1
oy=(ImageHeight(image)-ImageHeight(watermark))>>1
StartDrawing(ImageOutput(image))
DrawingMode(#PB_2DDrawing_AlphaBlend)
For x=-level To level
For y=-level To level
DrawAlphaImage(ImageID(Watermark),ox+x,oy+y,alpha)
Next y
Next x
StopDrawing()
EndProcedure
UsePNGImageDecoder()
LoadImage(1,"C:\Something.png")
ResizeImage(1,500,500)
Watermark=CreateWatermarkMask(1,"Hello PB!",30,"Trebuchet MS",48,300,250)
DoWatermark(1,Watermark,#Yellow,4,10)
DoWatermark(1,Watermark,#Red,1)
DoWatermark(1,Watermark,#White,0,255)
OpenWindow(0,0,0,500,500,"Watermarks",#PB_Window_ScreenCentered)
ImageGadget(0,0,0,0,0,ImageID(1))
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver
Even everything seems to be said already, I just add another way to do such things -- but rotated text is not easy to handle, so I add also a puzzle as a bonus-code

...
How to get all 'O' of the text centered on just one point? And will there be a possibility to get it over the red colored 'O' as well? And with different font sizes? And different fonts? And...
Code: Select all
If OpenWindow(0, 0, 0, 200, 200, "2DDrawing Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If CreateImage(0, 200, 200) And StartDrawing(ImageOutput(0))
Box(0, 0, 200, 200, RGB(255, 255, 255))
text.s="X<<< O >>>X"
tx=TextWidth(text)>>1
ty=TextHeight(text)>>1
DrawText(100-tx,100,text,#Red,#Yellow)
Plot(100,100,#Red)
Plot(100-tx,100,#Red)
For Angle = 0 To 90 Step 10
alpha.f=angle/180*#PI
zx = 100 - Cos(alpha)*(tx)
zy = 100 + Sin(Alpha)*(tx+ty*2.5)+Cos(alpha)*ty
DrawRotatedText(zx,zy,text, Angle, RGB(0, 0, 0))
Next Angle
StopDrawing()
ImageGadget(0, 0, 0, 200, 200, ImageID(0))
EndIf
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf