Drawing image with transparent background on canvas

Just starting out? Need help? Post your questions and find answers here.
User avatar
jacdelad
Addict
Addict
Posts: 1477
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Drawing image with transparent background on canvas

Post by jacdelad »

I am, again, failing to create an image with transparent background on runtime and putting it on a canvas.

Code: Select all

OpenWindow(0,0,0,300,300,"Test",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
CanvasGadget(1,0,0,300,300)
CreateImage(2,300,300,32,#PB_Image_Transparent)
StartDrawing(ImageOutput(2))
Box(100,100,100,100,RGB(255,0,0))
StopDrawing()
StartDrawing(CanvasOutput(1))
Box(0,0,300,300,GetSysColor_(15))
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawImage(ImageID(2),0,0)
StopDrawing()
Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
Why is the background always black? According to the help and a thread I found #PB_2DDrawing_AlphaBlend should be the right mode, so I assume I'm doing something wrong with the image itself.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Drawing image with transparent background on canvas

Post by StarBootics »

Hello,

I think I got it :

Code: Select all

OpenWindow(0,0,0,300,300,"Test",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)

CanvasGadget(1,0,0,300,300)

CreateImage(2,300,300,32, #PB_Image_Transparent)
StartDrawing(ImageOutput(2))
DrawingMode(#PB_2DDrawing_AlphaChannel)
Box(0,0,300,300,RGBA(0,0,0,255))
DrawingMode(#PB_2DDrawing_AllChannels)
Box(100,100,100,100,RGBA(255,0,0, 255))
StopDrawing()

StartDrawing(CanvasOutput(1))
Box(0,0,300,300, 0)

DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawAlphaImage(ImageID(2), 0, 0)
StopDrawing()

Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1252
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Drawing image with transparent background on canvas

Post by Paul »

I would do this...

Code: Select all

OpenWindow(0,0,0,300,300,"Test",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
CanvasGadget(1,0,0,300,300)
CreateImage(2,300,300,32,#PB_Image_Transparent)

StartDrawing(ImageOutput(2))
  DrawingMode(#PB_2DDrawing_AlphaBlend)
  Box(100,100,100,100,RGBA(255,0,0,255))
StopDrawing()

StartDrawing(CanvasOutput(1))
  Box(0,0,300,300,GetSysColor_(15))
  DrawingMode(#PB_2DDrawing_AlphaBlend)
  DrawImage(ImageID(2),0,0)
StopDrawing()
Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
Image Image
User avatar
jacdelad
Addict
Addict
Posts: 1477
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Drawing image with transparent background on canvas

Post by jacdelad »

Ayeyeye...thanks. Very stupid mistake(s)...
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Post Reply