Lazarus

Free Pascal => FPC development => Topic started by: OLLI_S on July 12, 2018, 07:37:21 pm

Title: Saving icon files with transparent background
Post by: OLLI_S on July 12, 2018, 07:37:21 pm
Hello,

I use Lazarus 1.8.4 in Windows 10 Pro German.
I coded a small procedure that can extract the icon from an application and stores the icon on the hard drive.
The name and the full path of the application are in an edit field (edtFileName).
Unfortunately all icons on the hard drive have a black background although they are shown with a transparent background in the Lazarus form.

Here is my code:

Code: Pascal  [Select][+][-]
  1. procedure TfrmMainForm.btnSaveClick(Sender: TObject);
  2.  
  3. var
  4.   IconHandle: THandle;
  5.   ExtractedIcon: Ticon;
  6.   IconDatei: Array[0..255] of char;
  7.   IconSaveFile: String;
  8.  
  9. begin
  10.   // Create an Icon
  11.   ExtractedIcon := Ticon.create;
  12.  
  13.   // Copy the name + path of the EXE to an Array of Char
  14.   StrPcopy(IconDatei,edtFileName.Text);
  15.  
  16.   // Extract the icon and show it in the Image control
  17.   IconHandle := ExtractIcon(hInstance, IconDatei, 0);
  18.   ExtractedIcon.Handle := IconHandle;
  19.   imgIcon.Picture.Icon := ExtractedIcon;
  20.  
  21.   // File Name of the Icon file is the current folder + File-name of the EXE (where the icon is extractedfrom)
  22.   IconSaveFile := ExtractFilePath(Paramstr(0));
  23.   IconSaveFile := IconSaveFile + ExtractFileName(edtFileName.Text);
  24.   IconSaveFile := StringReplace(IconSaveFile, '.exe', '.ico', [rfReplaceAll, rfIgnoreCase]);
  25.   IconSaveFile := StringReplace(IconSaveFile, '.dll', '.ico', [rfReplaceAll, rfIgnoreCase]);
  26.  
  27.   ExtractedIcon.SaveToFile(IconSaveFile);
  28.  
  29.   ExtractedIcon.Free;
  30.  
  31. end;

I also tried to save the icon from the TImage control but this has the same effect:

Code: Pascal  [Select][+][-]
  1. imgIcon.Picture.Icon.SaveToFile(IconSaveFile');

Any idea where the problem might be?
Thank you!

Regards

OLLI
Title: Re: Saving icon files with transparent background
Post by: Ñuño_Martínez on July 13, 2018, 12:30:34 pm
Seems as it can't deal with alpha channel or something.  Not sure, but try to save the imgIcon in to a PNG file.  Or you can try to move the icon to a BGRABitmap object (http://wiki.freepascal.org/BGRABitmap).
Title: Re: Saving icon files with transparent background
Post by: OLLI_S on July 13, 2018, 03:09:14 pm
Can I somehow define the file format or is this done automatically by the file extension?
Does PNG also have transparency?
Users should be able to click a button and then the result file should have a transparent background (no matter if it is ICO or PNG).

You all here are awesome (helped me more than in a German Lazarus forum).
Title: Re: Saving icon files with transparent background
Post by: Ñuño_Martínez on July 14, 2018, 01:05:10 pm
PNG has transparency.  Also it should detect format from extension.  Anyway may be there's a "Transparent" property somewhere that may affect if it has transparency or not, not sure about this last thing.
Title: Re: Saving icon files with transparent background
Post by: Handoko on July 14, 2018, 01:09:47 pm
Yes, png files support transparency. But I failed to make it work on OP's code. I modified the code to save to png format but still no transparent color, it replaced transparent with black.

Please try the code and modify it. I failed, maybe you can. After more than an hour of testing I gave up. But I'm interesting to know how to make it works correctly.
Title: Re: Saving icon files with transparent background
Post by: lainz on July 14, 2018, 06:37:05 pm
If I remember correctly, LazPaint can open .ico files and display them with transparency. Maybe you can take a look there.
Title: Re: Saving icon files with transparent background
Post by: Handoko on July 14, 2018, 07:28:02 pm
This is not about openning .ico files but extracting icon from .exe files.
Title: Re: Saving icon files with transparent background
Post by: lainz on July 14, 2018, 07:31:58 pm
This is not about openning .ico files but extracting icon from .exe files.

Oh I see. Sorry I missed the point.

There is another Lazarus application that does this, and is called Greenfish Icon Editor Pro, and it can open icons from exe files. Open Source.
Title: Re: Saving icon files with transparent background
Post by: lainz on July 14, 2018, 08:06:03 pm
This saves the icon without the black background, but still doesnt handle alpha blended pixels.

Code: Pascal  [Select][+][-]
  1.   imgIcon.Transparent:=True;

Code: Pascal  [Select][+][-]
  1. var
  2.   IconHandle: THandle;
  3.   ExtractedIcon: Ticon;
  4.   IconDatei: Array[0..255] of char;
  5.   IconSaveFile: String;
  6.   bmp: TBGRABitmap;
  7. begin
  8.   // Create an Icon
  9.   ExtractedIcon := Ticon.create;
  10.  
  11.   // Copy the name + path of the EXE to an Array of Char
  12.   StrPcopy(IconDatei,edtFileName.Text);
  13.  
  14.   // Extract the icon and show it in the Image control
  15.   IconHandle := ExtractIcon(hInstance, IconDatei, 0);
  16.   ExtractedIcon.Handle := IconHandle;
  17.   imgIcon.Transparent:=True;
  18.   imgIcon.Picture.Icon := ExtractedIcon;
  19.  
  20.   bmp := TBGRABitmap.Create(imgIcon.Picture.Bitmap);
  21.   bmp.SaveToFile('icon.png');
  22.   bmp.Free;
  23.  
  24.   ExtractedIcon.Free;  
Title: Re: Saving icon files with transparent background
Post by: valdir.marcos on July 14, 2018, 08:17:03 pm
This is not about openning .ico files but extracting icon from .exe files.
Oh I see. Sorry I missed the point.
There is another Lazarus application that does this, and is called Greenfish Icon Editor Pro, and it can open icons from exe files. Open Source.
Thanks @lainz, I was searching for an easy-to-use open source ready solution for creating icons:
Greenfish Icon Editor Pro 3.6
Jun 14, 2017
http://greenfishsoftware.org/gfie.php
https://www.youtube.com/results?search_query=Greenfish+Icon+Editor+Pro
Title: Re: Saving icon files with transparent background
Post by: RAW on July 15, 2018, 01:18:48 am
GIMP is pretty much very easy to use to create icons with different sized layers and with or without compression...
There is a new version 2.10.4... at least I don't know this version... didn't check the changelog ...
OK... GIMP isn't PASCAL... that's a pity !!!

Let me guess Photoshop still cannot do it ???
Once I saw a x64 plugin for PS, but the quality was very poor and the options too...
Title: Re: Saving icon files with transparent background
Post by: OLLI_S on July 17, 2018, 08:52:38 pm
I know that there are many tools out there in the market like GIMP or IcoFX.
But I want to have this feature in my application, so users select the EXE file, my application shows some details (version information) and users can save the app-icon with one mouse click and send it to us.
I am helping at an update manager (I help to keep the database up to date and so I coded a little tool for users).
Title: Re: Saving icon files with transparent background
Post by: lainz on July 17, 2018, 10:01:49 pm
I know that there are many tools out there in the market like GIMP or IcoFX.
But I want to have this feature in my application, so users select the EXE file, my application shows some details (version information) and users can save the app-icon with one mouse click and send it to us.
I am helping at an update manager (I help to keep the database up to date and so I coded a little tool for users).

Yes, for that I showed you Greenfish Icon Editor, that have source code you can see and use.
Title: Re: Saving icon files with transparent background
Post by: RAW on July 17, 2018, 11:28:56 pm
Quote
Yes, for that I showed you Greenfish Icon Editor, that have source code you can see and use.
Nice tip... I never really recognized that it's programmed in PASCAL, never took a look at the "src"-folder at the bottom...
Last time I needed a new cursor I used REAL WORLD CURSOR EDITOR, but GFIE can handle cursors too. Next time I do a little test...  :)
TinyPortal © 2005-2018