Creating a png file without any background

I create a picture “pct”
pct = new picture(theWidth,theheight,32)
'I draw something inside
I create a second picture “masque” to be a mask
masque = new picture(theWidth,theheight,32)
'I draw a black and white picture for a mask
I apply this mask to the first picture
pct.ApplyMask(masque)
I save this picture with
pct.Save(fichier, Picture.Formats.PNG)

It’s OK on a mac : the picture is seen as a png file with a transparent background.
On windows : I get a white background !!

How can I get rid of this sticky white background ?

try this at windows desktop pc

Var pic As New Picture(128,128)
Var g As Graphics = pic.Graphics
g.DrawingColor = Color.Clear
g.FillRectangle 0,0,g.Width,g.Height
g.DrawingColor = Color.Red
g.FillRectangle 0,0,g.Width,g.Height/2

Var mask As New Picture(128,128)
g = mask.Graphics
g.DrawingColor = Color.White
g.FillRectangle 0,0,g.Width,g.Height

g.DrawingColor = Color.Black
g.FillOval 0,0,g.Width,g.Height

pic.ApplyMask(mask)

Canvas1.Backdrop = pic

Var f As FolderItem = SpecialFolder.Desktop.Child("test.png")
Call pic.Save(f, Picture.Formats.PNG)

You need to Picture.Graphics.DrawPicture() your Picture into a new Picture e.g.

Protected Function getResizePicture(myPicture As Picture, areaWidth As Integer, areaHeight As Integer, isProportional As Boolean = True, isCentrePicture As Boolean = True) As Picture
  Var tempPicture As Picture
  Var Ratio As Double
  
  If myPicture = Nil Then
    Return Nil
  End If
  
  If areaWidth < 1 Or areaHeight < 1 Then
    Return Nil
  End If
  
  Ratio = myPicture.width / myPicture.height
  
  If isProportional And Ratio <> 1 Then
    If myPicture.Width > myPicture.Height Then
      tempPicture = New Picture(areaWidth, areaHeight)
      If isCentrePicture Then
        Var NewHeight As Double = areaHeight * (myPicture.height / myPicture.width)
        Var NewTop As Double = (areaHeight - (NewHeight)) / 2
        
        tempPicture.Graphics.DrawPicture(myPicture, 0, NewTop, areaWidth, NewHeight, 0, 0, myPicture.Width, myPicture.Height)
      Else
        tempPicture.Graphics.DrawPicture(myPicture, 0, 0, areaWidth, areaWidth / Ratio, 0, 0, myPicture.Width, myPicture.Height)
      End If
    Else
      tempPicture = New Picture(areaWidth, areaHeight)
      If isCentrePicture Then
        Var NewWidth As Double = areaWidth * (myPicture.Width / myPicture.Height)
        Var NewLeft As Double = (areaWidth - (NewWidth)) / 2
        
        tempPicture.Graphics.DrawPicture(myPicture, NewLeft, 0, NewWidth, areaHeight, 0, 0, myPicture.Width, myPicture.Height)
        
      Else
        tempPicture.Graphics.DrawPicture(myPicture, 0, 0, areaHeight * Ratio, areaHeight, 0, 0, myPicture.Width, myPicture.Height)
      End If
    End If
    
  Else
    tempPicture = New Picture(areaWidth, areaHeight)
    tempPicture.Graphics.DrawPicture(myPicture, 0, 0, areaWidth, areaHeight, 0, 0, myPicture.Width, myPicture.Height)
  End If
  
  Return tempPicture
  
End Function

Thanks for the
Color.Clear
unknown to me until today ! :slight_smile:

1 Like

In fact, my real purpose is to display icons from svg files using GraphickMagick.
It looks that on Windows, we always get a png file that is displayed with this white background, whatever we do…
Thanks for your help.