Alphablend question

Just starting out? Need help? Post your questions and find answers here.
dangerfreak
User
User
Posts: 32
Joined: Tue Jan 12, 2010 4:56 pm

Alphablend question

Post by dangerfreak »

I'm sorry to ask this question, but I already read a lot about alphablending and still can't get it to work. So I really hope you can help me.

The problem:
- First I created a completely black image #1
- Then I created a white image #2 with some yellow text on it [I used drawtext() for the yellow writing]
- Now I want to draw image #2 in image #1 [using drawimage() ], but the white color of image #2 must be transparent, so the result should be a black image with the yellow writing on it.

I tried all I know using drawingmode() or RBGA()-colors, but nothing worked. Can anyone pleeeeeaaaaase post a short source code for it?
User avatar
Bisonte
Addict
Addict
Posts: 1232
Joined: Tue Oct 09, 2007 2:15 am

Re: Alphablend question

Post by Bisonte »

You have to created image #2 with a transparent background, not with white as color, and with a depth of 32Bit !
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Alphablend question

Post by kenmo »

Experiment with this

Code: Select all

Size.i = 128

CreateImage(0, Size, Size, 32, #Blue) ; Background image (blue)

CreateImage(1, Size, Size, 32, #PB_Image_Transparent) ; Foreground text (yellow on transparent)
LoadFont(0, "Arial", Size/5)
If StartDrawing(ImageOutput(1))
  DrawingFont(FontID(0))
  
  DrawingMode(#PB_2DDrawing_AllChannels)
  #YellowOpaque = #Yellow | $FF000000 ; or RGBA(255, 255, 0, 255)
  #YellowTransparent = #Yellow ; or RGBA(255, 255, 0, 0)
  DrawText(5, 5, "Hello!", #YellowOpaque, #YellowTransparent)
  
  ; or
  ;DrawingMode(#PB_2DDrawing_AlphaBlend)
  ;DrawText(5, 5, "Hello!", RGBA(255, 255, 0, 255), RGBA(255, 255, 0, 0))
  
  StopDrawing()
EndIf

If StartDrawing(ImageOutput(0)) ; Draw foreground onto background
  DrawAlphaImage(ImageID(1), 0, 0)
  StopDrawing()
EndIf

If SaveImage(0, GetTemporaryDirectory() + "temp.bmp")
  RunProgram(GetTemporaryDirectory() + "temp.bmp")
EndIf
dangerfreak
User
User
Posts: 32
Joined: Tue Jan 12, 2010 4:56 pm

Re: Alphablend question

Post by dangerfreak »

Thank you so much - finally it works!!!! :) :) :) :)
Post Reply