ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Convert BMP to PNG with transparent color
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

r.federiconi

Italy
6 Posts

Posted - Nov 25 2015 :  02:35:24  Show Profile  Reply
I would like to use ImageEn component to convert Bitmap to PNG with trasparency. I tried with the code below but it doesn't work.. I could do the same thing using TPngImage but I would like implement only ImageEn components to manipulate images.. Thanks

######################################

  LImageEnIO := TImageEnIO.Create(nil);
  LBMPStream := TMemoryStream.Create;
  LImageEnProc := TImageEnProc.Create(nil);
  try
    ABMP.SaveToStream(LBMPStream);
    LBMPStream.Position := 0;
    LImageEnIO.LoadFromStreamBMP(LBMPStream);
    LImageEnProc.AttachedIEBitmap := LImageEnIO.IEBitmap;
    LImageEnProc.SetTransparentColors(CreateRGB(255, 255, 255), CreateRGB(255, 255, 255), 0);
    LImageEnIO.SaveToStreamPNG(AOutputStream);
  finally
    LImageEnIO.Free;
    LImageEnProc.Free;
    LBMPStream.Free; 
  end;

######################################

w2m

USA
1990 Posts

Posted - Nov 25 2015 :  10:18:25  Show Profile  Reply
If you want to use a png with a VCL component and not use ImageEn to view the png image then you must use Vcl.Imaging.PngImage then assign the TPngImage to the TImage.Picture. With out knowing which component you are using to load the bitmap it is difficult to answer your question directly, but I think the problem you may be having is loading the png image into the vcl component after you convert it. What VCL component are you using to load or view the bitmap?
Here is a sample that loads a bitmap into Image1, converts the bitmap to a PngImage, then assigns the PngImage to Image2. To compile this create a new VCL project, add a TPanel to the form and add 2 buttons named Open1 and Convert1 to the TPanel, then add two TImage components to the form. Set both TImage components Transparency property to true. Then add this code:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.ExtDlgs, Vcl.Imaging.PngImage,
  imageenio, imageenproc, iexbitmaps, hyiedefs, hyieutils;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Open1: TButton;
    Convert1: TButton;
    Image1: TImage;
    OpenPictureDialog1: TOpenPictureDialog;
    Image2: TImage;
    procedure Convert1Click(Sender: TObject);
    procedure Open1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Convert1Click(Sender: TObject);
var
  iBitmap: TBitmap;
  iRGB: TRGB;
  iImageEnIO: TImageEnIO;
  iBMPStream: TMemoryStream;
  iImageEnProc: TImageEnProc;
  iOutputStream: TMemoryStream;
  iPngImage: TPngImage;
begin
  iImageEnIO := TImageEnIO.Create(nil);
  try
    iBMPStream := TMemoryStream.Create;
    try
      iImageEnProc := TImageEnProc.Create(nil);
      try
        iBitmap := TBitmap.Create;
        try
          iBitmap.Assign(Image1.Picture.Bitmap);
          iBitmap.SaveToStream(iBMPStream);
          iBMPStream.Position := 0;
          iImageEnIO.LoadFromStreamBMP(iBMPStream);
          iImageEnIO.Params.BMP_HandleTransparency := True;
          iImageEnProc.AttachedIEBitmap := iImageEnIO.IEBitmap;
          iRGB := iImageEnIO.IEBitmap.Pixels[0, iImageEnIO.IEBitmap.Height-1];
          iImageEnProc.SetTransparentColors(iRGB,iRGB);
          iOutputStream := TMemoryStream.Create;
          try
            iImageEnIO.SaveToStreamPNG(iOutputStream);
            iOutputStream.Position := 0;
            iPngImage := TPngImage.Create;
            try
              iPngImage.LoadFromStream(iOutputStream);
              Image2.Picture.Assign(iPngImage);
              Image2.Update;
            finally
              iPngImage.Free;
            end;
          finally
            iOutputStream.Free;
          end;
        finally
          iBitmap.Free;
        end;
      finally
        iImageEnProc.Free;
      end;
    finally
      iBMPStream.Free;
    end;
  finally
    iImageEnIO.Free;
  end;
end;

procedure TForm1.Open1Click(Sender: TObject);
begin
  if OpenPictureDialog1.Execute then
    Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
end;

end.

When this is executed, when you open a bitmap from a file it appears in
Image 1. When you convert the image to PNG it appears in Image2 with full transparency.

This works correctly here with TImage, so if the component you are using correctly supports the alphachannel then it should work as well with similar code.

Bill Miller
Adirondack Software & Graphics
Email: w2m@hughes.net
EBook: http://www.imageen.com/ebook/
Custom Commercial ImageEn Development
Go to Top of Page

r.federiconi

Italy
6 Posts

Posted - Nov 26 2015 :  08:32:08  Show Profile  Reply
Thanks Bill,
I dont need view the image I need only convert a bitmap in PNG format with transparent color.
I wrote 2 procedures, one as you recommended and one using TPNGObject.. the output file are:

http://download.assistenzatelematica.it/FileDwn/Code1.png
http://download.assistenzatelematica.it/FileDwn/Code2.png

and, as you see from this screen-shot http://download.assistenzatelematica.it/FileDwn/PaintShopPro.png if I open the png with Paint Shop Pro only Code2.png has transparent color.. Why?

################# CODE 1############################
LTransparentColor := CreateRGB(255,255,255);
LImageEnIO := TImageEnIO.Create(nil);
LImageEnProc := TImageEnProc.Create(nil);
LBMPStream := TMemoryStream.Create;
try
ABMP.SaveToStream(LBMPStream);
LBMPStream.Position := 0;
LImageEnIO.LoadFromStreamBMP(LBMPStream);
LImageEnIO.Params.BMP_HandleTransparency := true;
LImageEnProc.AttachedIEBitmap := LImageEnIO.AttachedIEBitmap;
LImageEnProc.SetTransparentColors(LTransparentColor, LTransparentColor);
LImageEnIO.SaveToStreamPNG(AOutputStream);
finally
LImageEnIO.Free;
LImageEnProc.Free;
LBMPStream.Free;
end;
################# CODE 1############################


################# CODE 2############################
LPNG := TPNGObject.Create;
try
LPNG.Assign(ABMP);
LPNG.TransparentColor := clWhite;
LPNG.Transparent := true;
LPNG.SaveToStream(AOutputStream);
finally
LPNG.Free;
end;
################# CODE 2############################
Go to Top of Page

xequte

39053 Posts

Posted - Nov 26 2015 :  20:21:19  Show Profile  Reply
Hi

You probably just need to create the Alpha Channel:



procedure TForm1.Button1Click(Sender: TObject);
var
  BMP: TIEBitmap;
  ieProc: TImageEnProc;
  TransparentColor : TRGB;
begin
  // Replace white background with alpha transparency
  TransparentColor := CreateRGB(255,255,255);
  BMP := TIEBitmap.create;
  BMP.Read( 'D:\Trans.bmp' );

  // Create Alpha Channel
  BMP.AlphaChannel;
  ieProc := TImageEnProc.CreateFromBitmap( BMP );
  ieProc.SetTransparentColors( TransparentColor, TransparentColor );
  BMP.Write( 'D:\Trans.png' );
  ieProc.Free;
  BMP.Free;
end;







Nigel
Xequte Software
www.xequte.com
nigel@xequte.com
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: