Save any image to a Transparent PNG [Windows]

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Save any image to a Transparent PNG [Windows]

Post by RASHAD »

New era then :)

Code: Select all

UsePNGImageDecoder()
UsePNGImageEncoder()
UseJPEGImageDecoder()
UseJPEG2000ImageDecoder()
UseTGAImageDecoder()

Procedure TransPNG(File$,TransColor)
LoadImage(0,File$)

If TransColor <> #White
   StartDrawing(ImageOutput(0))
       For x = 0 To ImageWidth(0) - 1
          For y = 0 To ImageHeight(0) - 1
             If Point(x,y) = TransColor
                Plot(x,y,RGB(255, 255, 255))
             EndIf
          Next
       Next
   StopDrawing()
 EndIf 

i.ICONINFO
i\fIcon = #False
i\hbmMask = ImageID(0)
i\hbmColor = ImageID(0)

curHnd = CreateIconIndirect_(i)

CreateImage(10,ImageWidth(0),ImageHeight(0),32)
StartDrawing(ImageOutput(10))
DrawingMode(#PB_2DDrawing_AllChannels)
DrawImage(curHnd,0,0,ImageWidth(0),ImageHeight(0))
StopDrawing()

FreeImage(0)
EndProcedure

TransPNG("e:\girlgreen.bmp",#Green)   ;The filename including path ,The required color to be transparent
SaveImage(10, "e:\girltrans.png",#PB_ImagePlugin_PNG)
   
Egypt my love
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: Save any image to a Transparent PNG [Windows]

Post by Bisonte »

But not so "Q'n'D" , you are the first ... :wink:

More like

Code: Select all

;:=============================================================================
;:- TransPNG()
;:- Author          : Rashad
;:- Date            : August 19, 2013
;:- Compiler        : PureBasic 5.20 beta 11 LTS
;:- Target OS       : Windows
;:- Source --------------------------------------------------------------------
;:- http://purebasic.fr/english/viewtopic.php?p=421780#p421780
;:=============================================================================

EnableExplicit

; Init ImagePlugins

UsePNGImageDecoder()
UsePNGImageEncoder()
UseJPEGImageDecoder()
UseJPEG2000ImageDecoder()
UseTGAImageDecoder()

Procedure TransPNG(File.s, TransColor)
  
  Protected Image, curHnd, x, y
  Protected i.ICONINFO
  Protected SImage = #False
  
  Image = LoadImage(#PB_Any, File)
  
  If IsImage(Image)
    
    If TransColor <> #White
      If StartDrawing(ImageOutput(Image))
        For x = 0 To ImageWidth(Image) - 1
          For y = 0 To ImageHeight(Image) - 1
            If Point(x,y) = TransColor
              Plot(x,y,RGB(255, 255, 255))
            EndIf
          Next
        Next
        StopDrawing()
      EndIf  
    EndIf
  
    i\fIcon = #False
    i\hbmMask = ImageID(Image)
    i\hbmColor = ImageID(Image)

    curHnd = CreateIconIndirect_(i)
    
    SImage = CreateImage(#PB_Any, ImageWidth(Image), ImageHeight(Image), 32, #PB_Image_Transparent)
    
    If IsImage(SImage)
      If StartDrawing(ImageOutput(SImage))
        DrawingMode(#PB_2DDrawing_AllChannels)
        DrawImage(curHnd, 0, 0, ImageWidth(Image), ImageHeight(Image))
        StopDrawing()
      EndIf
      FreeImage(Image)  
    EndIf
  EndIf
  
  ProcedureReturn SImage
  
EndProcedure

;: Usage :
;:
;: Image = TransPNG(File, TransColor)
;:
;: Parameter :
;: File       = The filename including path
;: TransColor = Color To be transparent

Define Image, File.s, TColor, SFile.s

File    = #PB_Compiler_Home + "Examples\Sources\Data\Geebee2.bmp"
TColor  = RGB(255,0,255)
SFile   = "D:\GeeBeeTrans.png"

Image = TransPNG(File, TColor)

If IsImage(Image)
  SaveImage(Image, SFile, #PB_ImagePlugin_PNG)
EndIf
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Save any image to a Transparent PNG [Windows]

Post by fsw »

Hi Rashad,
could you please change the topic from
Save any image to a Transparent PNG [Windows]
to
[Windows] Save any image to a Transparent PNG
this way it's consistent with what Fred and Co. normally write.

Examples...
[Done] Limits of CanvasGadget
[Implemented] Rename Frame3DGadget() to FrameGadget()
Thank you.

I am to provide the public with beneficial shocks.
Alfred Hitshock
Fred
Administrator
Administrator
Posts: 18247
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Save any image to a Transparent PNG [Windows]

Post by Fred »

No need to nitpick about topic subject format, there is no absolute enforcement on the forum, so it's not possible to force everyone to do it the same way. It never been a real problem anyway :)
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Save any image to a Transparent PNG [Windows]

Post by fsw »

Fred wrote:...so it's not possible to force everyone
I was forcing no one.

The phrase
fsw wrote: could you please change
is a simple request.
Following a request is voluntary :wink:

I am to provide the public with beneficial shocks.
Alfred Hitshock
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Save any image to a Transparent PNG [Windows]

Post by Olliv »

Just a private request so!

Such a break prevent me to get the code of Rashad, quickly, wildly and secretly!!!

When I will have time, I rework my own codes. There are obsolete!!! Built-in now! Good new! Thanks Fred and Hervé also!
Post Reply