[solved] image with alpha channel has white background

Just starting out? Need help? Post your questions and find answers here.
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

[solved] image with alpha channel has white background

Post by eck49 »

This image should have a transparent background, but when I put it onto my form in an image gadget, I get a white background - not the same as the form it sits in. Have I missed something?

The image was created in the GIMP application, and exported to a file in png format. When opened in GIMP, GIMP still thinks the image has an alpha channel.
Here it is in a DataSection. (I see the same thing if I include the file directly with IncludeBinary)

Code: Select all

DataSection
image_man_standing:		;the image is 16x16
  Data.b $89, $50, $4E, $47, $0D, $0A, $1A, $0A, $00, $00, $00, $0D, $49, $48, $44, $52, $00, $00, $00, $10, $00, $00, $00, $10, $08, $06, $00, $00, $00, $1F, $F3, $FF
  Data.b $61, $00, $00, $00, $06, $62, $4B, $47, $44, $00, $FF, $00, $FF, $00, $FF, $A0, $BD, $A7, $93, $00, $00, $00, $09, $70, $48, $59, $73, $00, $00, $0B, $13, $00
  Data.b $00, $0B, $13, $01, $00, $9A, $9C, $18, $00, $00, $00, $07, $74, $49, $4D, $45, $07, $E5, $05, $11, $0D, $18, $34, $01, $6F, $60, $A4, $00, $00, $00, $19, $74
  Data.b $45, $58, $74, $43, $6F, $6D, $6D, $65, $6E, $74, $00, $43, $72, $65, $61, $74, $65, $64, $20, $77, $69, $74, $68, $20, $47, $49, $4D, $50, $57, $81, $0E, $17
  Data.b $00, $00, $00, $50, $49, $44, $41, $54, $38, $CB, $DD, $92, $41, $0A, $00, $20, $08, $04, $77, $A3, $FF, $7F, $79, $BB, $14, $54, $24, $42, $0A, $41, $7B, $54
  Data.b $D2, $71, $88, $92, $30, $87, $E4, $5A, $E8, $91, $C4, $53, $BD, $20, $98, $F7, $03, $B8, $3B, $D8, $5D, $58, $B7, $E7, $11, $00, $D0, $CD, $C3, $41, $F6, $81
  Data.b $83, $EA, $6D, $F6, $48, $F2, $1C, $78, $9B, $AC, $7E, $DE, $3F, $F0, $6C, $5B, $24, $61, $82, $06, $42, $40, $34, $0B, $BE, $B1, $C3, $2D, $00, $00, $00, $00
  Data.b $49, $45, $4E, $44, $AE, $42, $60, $82
 EndDataSection
This is put into an Image in code

Code: Select all

Enumeration
#im1
#im101
#imgad
EndEnumeration

Global.i abfl		;this is global just while I am experimenting.

Procedure MakeImages()
  abfl=?image_man_standing
  UsePNGImageDecoder()
  CreateImage(#im1, 16, 16, 32)
  If StartDrawing(ImageOutput(#im1))
    DrawingMode(#PB_2DDrawing_Default)
    DrawImage(CatchImage(#im101, abfl), 0, 0)
    StopDrawing()
  EndIf
  
EndProcedure
I call the MakeImages procedure before opening the main window and in the main window put the image in a gadget thus

Code: Select all

  ImageGadget(#imgad, 50, 50, 16, 16, ImageID(#im1))
I've not done this before, so maybe I've missed how an alpha channel works?
Last edited by eck49 on Mon May 17, 2021 7:31 pm, edited 1 time in total.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: image with alpha channel has white background

Post by IdeasVacuum »

Well, even though you have an Alpha Channel, that does not mean the Channel is set to be transparent. In a paint program, you typically have to select a colour that will be transparent (or translucent), so may be the Alpha Channel is being saved as opaque?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: image with alpha channel has white background

Post by eck49 »

@IdeasVacuum

It's a thought. But I think it isn't so in this case, because I have just tried putting the png file directly on my desktop, and the background does show through there.

I know most PB users are Windows people and there are occasional obscure differences in Linux. Could this be one of them?

EDIT: I thought I had found the problem. If I add a 5th parameter to the CreateImage call, #PB_Image_Transparent, the manual suggests it would do as I wish. It doesn't: now the image is totally invisible. Maybe that's because the default background colour (according to the manual) is black which happens to be the only real colour in that image. No that's not it - I have one with some red in it and that disappears as well.
Last edited by eck49 on Mon May 17, 2021 7:20 pm, edited 1 time in total.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
fluent
User
User
Posts: 68
Joined: Sun Jan 24, 2021 10:57 am

Re: image with alpha channel has white background

Post by fluent »

Try:

Code: Select all


Enumeration
#im1
#im101
#imgad
EndEnumeration

Global.i abfl		;this is global just while I am experimenting.

Procedure MakeImages()
  abfl=?image_man_standing
  UsePNGImageDecoder()
  CreateImage(#im1, 16, 16, 32)
  If StartDrawing(ImageOutput(#im1))
          DrawingMode(#PB_2DDrawing_AlphaBlend) 
        DrawAlphaImage(CatchImage(#im101, abfl), 0, 0, 200)
    StopDrawing()
  EndIf
  
EndProcedure


eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

[solved] image with alpha channel has white background

Post by eck49 »

@fluent Yes! That's it, combined with using #PB_Image_Transparent as well (otherwise the background is black, not transparent).

Many thanks.

So the relevant piece of code becomes

Code: Select all

Enumeration
#im1
#im101
#imgad
EndEnumeration

Global.i abfl		;this is global just while I am experimenting.

Procedure MakeImages()
  abfl=?image_man_standing
  UsePNGImageDecoder()
  CreateImage(#im1, 16, 16, 32, #PB_Image_Transparent)
  If StartDrawing(ImageOutput(#im1))
          DrawingMode(#PB_2DDrawing_AlphaBlend) 
        DrawAlphaImage(CatchImage(#im101, abfl), 0, 0, 200)
    StopDrawing()
  EndIf
  
EndProcedure


Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
Post Reply