Recent

Author Topic: How to make color of PNG image transparent  (Read 23853 times)

widoman

  • New Member
  • *
  • Posts: 14
How to make color of PNG image transparent
« on: January 19, 2013, 02:18:48 am »
Hi,
  • I create a bitmap capturing the application form, the bitmap have white background and others colors.
  • I want to save the bitmap has png making the white color transparent
  • When the png is created apparently the background is transparent in Microsoft Photo Editor
  • When open the png in Google-earth (kml overlay) the png have black background >:( its not transparent
  • When open the png in Microsoft paint have black background
  • When open the png in Microsoft Word and put over text the image its transparent
  • When open the png in Microsoft Photo Editor, i have to change the transparent color(show black) and save the image; then when open the png in Google-earth its ok with transparent background but some lines(clfucshia) are transparent

%) I try various ways but none work, or the png have white background(not transparent) or this last situation.
How to create a correct transparent png in Lazarus??
What its wrong?


PNG opened in applications
pnggoogleearth.png
pngphotoeditor.png

Same PNG edited manually with Microsoft Photo editor and opened in google-earth
png2photoeditor.png
png2googleearth.png

Code: [Select]
function fmyScreen.MakeBitmap(pControl:TCustomControl):TBitmap;
var
  tmp: TBitmap;
  r: TRect;
begin
  r:= Rect(0, 0, pControl.ClientWidth, pControl.ClientHeight);
  tmp:= TBitmap.Create;
  tmp.SetSize(pControl.ClientWidth, pControl.ClientHeight);
  tmp.Canvas.CopyRect(r, pControl.Canvas, r);
  result:= tmp;
end;

procedure fmyScreen.MakePng(FileName: String; Trans: TColor);
var
  screen: TBitmap;
  c: TBGRABitmap;
  i,j: integer;
  xcolor: TBGRAPixel;
  zcolor: TFPColor;
begin
  screen := MakeBitmap(fmyScreen);//form
  xcolor:= ColorToBGRA(trans,255);
  zcolor.alpha := xcolor.alpha;
  zcolor.red := xcolor.red;
  zcolor.green := xcolor.green;
  zcolor.blue := xcolor.blue;

  c := TBGRABitmap.Create(screen);
  for i:=0 to c.Width-1 do
  begin
      for j:=0 to c.Height-1 do
      begin
        if(c.Canvas.Pixels[i,j]=Trans)then
        begin
          c.CanvasBGRA.Colors[i,j]:= zcolor;
        end;
      end;
  end;
  c.SaveToFile(FileName);
  c.free;
  screen.free;
end;

************
on button click
************
procedure fmyScreen.Button1Click(Sender: TObject);
begin
   MakePng('aa.png', clWhite);
end;
« Last Edit: February 07, 2013, 12:02:16 am by widoman »

dcelso

  • Full Member
  • ***
  • Posts: 158
Re: How to make color of PNG image transparent
« Reply #1 on: January 27, 2013, 05:07:30 pm »
You can do it converting it in 32 bits and change white color for a color with alpha channel equals to 0.
You must change bmp.pixelformat To pf32bit
and after of your copy add the next lines

Myimg :Tlazintfimage
 
myimg:=bmp.createlazintf

for i:=0 to bmp.width -1
 for j:=0 to bmp.height -1
    if myimg.colors[i,j]= colwhite then
       myimg.colors[i,j]:= coltransparent
Bmp.loadfrominterfaz(myimg)

NOTE: This method in linux does not work. :'(
« Last Edit: January 28, 2013, 01:32:11 pm by dcelso »

widoman

  • New Member
  • *
  • Posts: 14
Re: How to make color of PNG image transparent
« Reply #2 on: January 29, 2013, 12:35:36 am »
dcelso, i cant make work
 :'( the background of the png its not transparent, its black or white

Code: [Select]
procedure TfGrid.CrearPng(FileName: String; Transparencia: TColor);
var
  pantalla: TBitmap;
  c: TBGRABitmap;
  i,j: integer;
  xcolor: TBGRAPixel;
  zcolor: TFPColor;
  m1: Tlazintfimage;
begin
  pantalla := CrearBitmap(fGrid);
  m1:= pantalla.CreateIntfImage;
  xcolor:= ColorToBGRA(transparencia, 0);
  for i:=0 to m1.width -1 do
  begin
    for j:=0 to m1.height -1 do
    begin
      if(FPColorToTColor(m1.colors[i,j]) = Transparencia) then
        m1.colors[i,j]:= BGRAToFPColor(xcolor);
        //colTransparent => black background not transparent
        //BGRAToFPColor(xcolor) => white background not transparent
    end;
  end;
  pantalla.LoadFromIntfImage(m1);
  c := TBGRABitmap.Create(pantalla);
  c.SaveToFile(FileName);
end;

function TfGrid.CrearBitmap(pControl : TCustomControl):TBitmap;
var
  tmp: TBitmap;
  r: TRect;
begin
  r:= Rect(0, 0, pControl.ClientWidth, pControl.ClientHeight);
  tmp:= TBitmap.Create;
  tmp.PixelFormat := pf32bit;
  tmp.SetSize(pControl.ClientWidth, pControl.ClientHeight);
  tmp.Canvas.CopyRect(r, pControl.Canvas, r);
  result:= tmp;
end;

circular

  • Hero Member
  • *****
  • Posts: 4220
    • Personal webpage
Re: How to make color of PNG image transparent
« Reply #3 on: January 29, 2013, 12:52:25 am »
Try this:
Code: [Select]
procedure fmyScreen.MakePng(FileName: String; Trans: TColor);
var
  screen: TBitmap;
  c: TBGRABitmap;
  i,j: integer;
  xcolor: TBGRAPixel;
begin
  screen := MakeBitmap(fmyScreen);//form
  xcolor:= ColorToBGRA(trans,255);

  c := TBGRABitmap.Create(screen);
  screen.free;
  for i:=0 to c.Width-1 do
  begin
      for j:=0 to c.Height-1 do
      begin
        if(c.GetPixel(i,j)=xcolor)then
          c.SetPixel(i,j, BGRAPixelTransparent);
      end;
  end;
  c.SaveToFile(FileName);
  c.free;
end;
Conscience is the debugger of the mind

widoman

  • New Member
  • *
  • Posts: 14
Re: How to make color of PNG image transparent
« Reply #4 on: January 29, 2013, 01:55:42 am »
circular,
Png created open with:
Microsoft Word 2000 => image transparent OK
Microsoft Photo Editor => some fuchsia lines changed to black, image transparent OK
Google-earth => KML with image as overlay the image background its black not transparent  :'(
** image like the first example in this post
« Last Edit: January 29, 2013, 02:08:22 am by widoman »

circular

  • Hero Member
  • *****
  • Posts: 4220
    • Personal webpage
Re: How to make color of PNG image transparent
« Reply #5 on: January 29, 2013, 12:37:03 pm »
What if you do c.SetPixel(0,0,BGRAPixelTransparent) before c.SaveToFile ?
Conscience is the debugger of the mind

dcelso

  • Full Member
  • ***
  • Posts: 158
Re: How to make color of PNG image transparent
« Reply #6 on: January 29, 2013, 01:44:05 pm »
Without using BGRA library would be as the next, using it, I dont know.
Code: [Select]
unit unit3;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, IntfGraphics, FPimage;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    function CrearBitmap(pControl : TCustomControl):TPortableNetworkGraphic;
    procedure CrearPng(FileName: String; Transparencia: TColor);
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

function TForm1.CrearBitmap(pControl : TCustomControl):TPortableNetworkGraphic;
var
  tmp: TPortableNetworkGraphic;
  r: TRect;
begin
  r:= Rect(0, 0, pControl.ClientWidth, pControl.ClientHeight);
  tmp:= TPortableNetworkGraphic.Create;
  tmp.PixelFormat := pf32bit;
  tmp.SetSize(pControl.ClientWidth, pControl.ClientHeight);
  tmp.Canvas.CopyRect(r, pControl.Canvas, r);
  result:= tmp;
end;

procedure TForm1.CrearPng(FileName: String; Transparencia: TColor);
var
  pantalla: TPortableNetworkGraphic;
  m1: Tlazintfimage;
  tempcolor: TFPColor;
  i,j : Integer;
begin
  pantalla := CrearBitmap(Form1);
  m1:= pantalla.CreateIntfImage;
  for i:=0 to m1.width -1 do
    for j:=0 to m1.height -1 do
    begin
      tempcolor:=m1.colors[i,j];
      if  tempcolor= TColorToFPColor(Transparencia) then
       tempcolor.alpha:=0
      else
       tempcolor.alpha:=$FFFF;

      m1.colors[i,j]:= tempcolor; // if you want keep RGB information
   end;
  pantalla.LoadFromIntfImage(m1);
  m1.free;
  pantalla.SaveToFile(FileName);
  pantalla.free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
      CrearPng(Edit1.Text,clWhite);
end;

end.


The form example is the next:
Code: [Select]
object Form1: TForm1
  Left = 292
  Height = 46
  Top = 224
  Width = 321
  Caption = 'Form1'
  ClientHeight = 46
  ClientWidth = 321
  LCLVersion = '1.1'
  object Edit1: TEdit
    Left = 16
    Height = 23
    Top = 18
    Width = 176
    Color = clWhite
    TabOrder = 0
    Text = '32bits.png'
  end
  object Button1: TButton
    Left = 216
    Height = 25
    Top = 16
    Width = 75
    Caption = 'Button1'
    OnClick = Button1Click
    TabOrder = 1
  end
end

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: How to make color of PNG image transparent
« Reply #7 on: January 29, 2013, 03:02:29 pm »
Code: [Select]
r:= Rect(0, 0, pControl.ClientWidth, pControl.ClientHeight);
tmp.Canvas.CopyRect(r, pControl.Canvas, r);
You use CopyRect for drawing everything. Would Draw() work? There used to be some problems with CopyRect dealing with transparency.

widoman

  • New Member
  • *
  • Posts: 14
Re: How to make color of PNG image transparent
« Reply #8 on: January 30, 2013, 02:54:55 am »
Code: [Select]
r:= Rect(0, 0, pControl.ClientWidth, pControl.ClientHeight);
tmp.Canvas.CopyRect(r, pControl.Canvas, r);
You use CopyRect for drawing everything. Would Draw() work? There used to be some problems with CopyRect dealing with transparency.
:'( dont work, same result.
I used Draw() with GetFormImage() like you say, GetFormImage() return a Bitmap.
« Last Edit: January 30, 2013, 03:09:07 am by widoman »

widoman

  • New Member
  • *
  • Posts: 14
Re: How to make color of PNG image transparent
« Reply #9 on: January 30, 2013, 03:04:34 am »
Without using BGRA library would be as the next, using it, I dont know.
Code: [Select]
function TForm1.CrearBitmap(pControl : TCustomControl):TPortableNetworkGraphic;
begin
   //exactly like your example
end;

procedure TForm1.CrearPng(FileName: String; Transparencia: TColor);
begin
   //exactly like your example
end;
I change the functions
CrearBitmap() => use TPortableNetworkGraphic and return a PNG like you say
CrearPng() => modify alpha and use PNG like you say
 :'( dont work
Microsoft Word => ok image transparent
Microsoft Photo Editor => ok image transparent, some fuchsia lines changed to white
Google-earth => show png with white background not transparent
** image like the first example in this post
** I have to edit png manually to change transparent color but some lines dissapear because are white like background detected to make transparent
« Last Edit: January 30, 2013, 03:06:10 am by widoman »

widoman

  • New Member
  • *
  • Posts: 14
Re: How to make color of PNG image transparent
« Reply #10 on: January 30, 2013, 03:07:27 am »
What if you do c.SetPixel(0,0,BGRAPixelTransparent) before c.SaveToFile ?
:'( dont work, same result.

widoman

  • New Member
  • *
  • Posts: 14
Re: How to make color of PNG image transparent
« Reply #11 on: January 30, 2013, 03:31:12 am »
:) maybe the problem is the software
Lazarus 1.0.4
Rev SVN: 39422
FPC 2.6.0
Packages list
« Last Edit: February 06, 2013, 11:56:08 pm by widoman »

lainz

  • Guest
Re: How to make color of PNG image transparent
« Reply #12 on: January 30, 2013, 05:06:19 am »
Attach a transparent PNG not generated in lazarus that works in all the programs you've mentioned, then maybe circular can analize it, im wrong?

5) Paint in Windows 8 load transparent as White.. and if you save it loses transparency. So transparency in this program is not supported.

widoman

  • New Member
  • *
  • Posts: 14
Re: How to make color of PNG image transparent
« Reply #13 on: January 30, 2013, 05:37:28 am »
Example image not generated in Lazarus => 12345rq.png
its transparent in: Microsoft Word 2000, Google-earth and Microsoft Photo Editor
* image  "12345rq.png" need to be in same place than "xyz.kml"
* open  "xyz.kml" with google-earth

Save next xml has "xyz.kml"
Code: [Select]
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<ScreenOverlay id="12345">
<name>12345rq</name>
<Icon><href>12345rq.png</href></Icon>
<overlayXY x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>
<screenXY x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>
<size x="0" y="0" xunits="pixels" yunits="pixels"/>
</ScreenOverlay>
</kml>
« Last Edit: February 06, 2013, 11:58:27 pm by widoman »

widoman

  • New Member
  • *
  • Posts: 14
Re: How to make color of PNG image transparent
« Reply #14 on: February 05, 2013, 10:48:11 pm »
 :'(conclusion, Lazarus can not create a transparent PNG.

I will change manually the PNG.

:D Thanks for your time.


 

TinyPortal © 2005-2018