Author Topic: A simple demo to show how to merge with transparency 2 images  (Read 3311 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
A simple demo to show how to merge with transparency 2 images
« on: December 20, 2021, 10:16:06 am »
Hi Guys and Gals of QB64 Universe

on Discord there is this request.
I find it useful for sharing in a more stable place (the Forum).
Quote
There are alot of mentioning about transparency on putimage wiki, but cant find any examples Im looking for.
[11:43]
I mean if there is no thing for it internally, its no problem to use just multiple images with they own transparency in it. I was just thinking that there is since I remember seeing some things about transparency. But it could be for other things as wel

a bit of simple graphic functions of QB64 that mimic the AND/OR/XOR/PSET option of QB45 PUT graphic keyword.
In QB64 you needn't use the old graphic tecniques both for the more available graphic memory to use both for the simpler graphic keywords.

here I share a very simple demonstration about  how to merge 2 images with different grade of transparency

Code: QB64: [Select]
  1. Const w = 600, h = 600
  2. A1& = _NewImage(w, h, 32) ' first image THE SCREEN
  3. A2& = _NewImage(w, h, 32) ' second image to show on the screen
  4. A3& = _NewImage(w / 2, h / 2, 32) ' resizing the second image into a smaller image
  5. ' making background image of the screeen
  6. _Dest A1&
  7. Cls , _RGB32(100, 0, 0)
  8. Line (300, 300)-(400, 400), _RGB32(0, 0, 200, 255), BF
  9. ' making new image to paste with transparency to the SCREEN
  10. _Dest A2&
  11. Cls , _RGB32(0, 200, 0, 255)
  12. _PutImage , A2&, A3& ' save A2 in A3 changing its size
  13. _PutImage , A1&, A2& ' save a1 in a2 to restore background
  14.  
  15. Screen A1& ' it creates the screen
  16. For q = 1 To 255 Step 5
  17.     _PutImage , A2&, A1& ' it restores background to screen visible
  18.     Locate 1, 1: Print "Alpha channel source image: "; q ' onscreen output
  19.     _SetAlpha q, , A3& ' here it is changing transparency of A3&
  20.     _PutImage (200, 200)-(400, 400), A3&, A1& ' it puts A3& on the center of A1&
  21.     _Delay .2 'it waits to let you see changement
  22.     _Display ' it shows at the istant the changement
  23.     _Limit 10 ' it saves CPU work
  24.  

On the other side if you want only have the background transparent you must use an image with Black background that  it works as transparent color in QB64!
Moreover if you want  to make  transparent a specific color or range of colors you must use _SETALPHA keyword.

Good Coding
Programming isn't difficult, only it's  consuming time and coffee