Recent

Author Topic: SOLVED: Converting Icon to PNG  (Read 1924 times)

SaraT

  • Full Member
  • ***
  • Posts: 124
  • A little student
SOLVED: Converting Icon to PNG
« on: February 15, 2020, 11:27:20 pm »
Hi friends :)

How can I convert/save the loaded icon from Image1 into PNG with transparency?
I will be so grateful for any code provided... Many, many thanks ;)

Code: Pascal  [Select][+][-]
  1. Image1.Picture.LoadFromFile('C:\MyFolder\MyIcon.ico');

« Last Edit: February 16, 2020, 02:41:26 am by SaraT »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Converting TIcon to PNG
« Reply #1 on: February 15, 2020, 11:51:34 pm »
Hi !

Try this:


Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. var PNG : TPortableNetworkGraphic;
  3.     R : TREct;
  4. begin
  5.  Image2.Picture.LoadFromFile('TuxMon.ico');
  6.  PNG := TPortableNetworkGraphic.create;
  7.  PNG.SetSize (Image2.Width, Image2.Height);
  8.  R := Rect(0,0,Image2.Width, Image2.Height);
  9.  PNG.Canvas.CopyRect(R,Image2.Picture.Icon.Canvas, R);
  10.  PNG. SavetoFile('TuxTest.png');
  11.  PNG.Free;
  12. end;                          

And when do you sleep? All day on the forum and coding ....

Winni

SaraT

  • Full Member
  • ***
  • Posts: 124
  • A little student
Re: Converting TIcon to PNG
« Reply #2 on: February 16, 2020, 12:07:59 am »
Hi !

Try this:


Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. var PNG : TPortableNetworkGraphic;
  3.     R : TREct;
  4. begin
  5.  Image2.Picture.LoadFromFile('TuxMon.ico');
  6.  PNG := TPortableNetworkGraphic.create;
  7.  PNG.SetSize (Image2.Width, Image2.Height);
  8.  R := Rect(0,0,Image2.Width, Image2.Height);
  9.  PNG.Canvas.CopyRect(R,Image2.Picture.Icon.Canvas, R);
  10.  PNG. SavetoFile('TuxTest.png');
  11.  PNG.Free;
  12. end;                          

And when do you sleep? All day on the forum and coding ....

Winni

Thanks Winni... I see you in every post too :)
I love comp. programming and like to help others schoolmates with some ideas for projects.
(I'm not expert in Lazarus)

Love this forum, I was about to stop using Lazarus, I was gonna use CodeTyphon instead but its forum is dead xD

I will try your code soon.
Best regards Winni

SaraT

  • Full Member
  • ***
  • Posts: 124
  • A little student
Re: Converting TIcon to PNG
« Reply #3 on: February 16, 2020, 01:06:57 am »
Winni... I got a error :/

Cannot assign a TIcon to a TIcon

I haven't changed anything. What could be?
Thanks again.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Converting TIcon to PNG
« Reply #4 on: February 16, 2020, 01:23:09 am »
 Hi!

?????
* The error message is nonsense
*  have tested the code - no problem.

Does the debugger show  a linenumber where it crashed?

Project --> Options --> Debugging -- > Display line number in run-time errors backtraces

Enable this option if it is not done by default.


The second trick is to insert bevor or after critical code a "showmessage" so that you can be shure that the app in this line is still alive.

Mystery, mystery .....

Winni


SaraT

  • Full Member
  • ***
  • Posts: 124
  • A little student
Re: Converting TIcon to PNG
« Reply #5 on: February 16, 2020, 01:31:02 am »
Hi!

?????
* The error message is nonsense
*  have tested the code - no problem.

Does the debugger show  a linenumber where it crashed?

Project --> Options --> Debugging -- > Display line number in run-time errors backtraces

Enable this option if it is not done by default.


The second trick is to insert bevor or after critical code a "showmessage" so that you can be shure that the app in this line is still alive.

Mystery, mystery .....

Winni

Sorry Winni, now it works (I know what happened)...
I created an empty project to tried it but the PNG has black background, thats all :/

Thanks Winni

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Converting TIcon to PNG
« Reply #6 on: February 16, 2020, 01:57:24 am »
Hi!

Insert this lines at the beginning of the procedure:

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

If the icon in the image has a black background we have to look for another solution.

The problem is that Microsoft forgot the transparency. All tricks to make an image transparent are only lousy hacks. I know there is a way to achieve this, but I forgot because I use now graphic libraries like BGRA where it is no problem.

Winni

SaraT

  • Full Member
  • ***
  • Posts: 124
  • A little student
Re: Converting TIcon to PNG
« Reply #7 on: February 16, 2020, 02:07:25 am »
Still png in black :/
Can we save the PNG from a TPaintBox control? Not a TImage.

If not we will have to try the hack with BGRA... I already have it installed.
Thanks.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Converting TIcon to PNG
« Reply #8 on: February 16, 2020, 02:28:27 am »
Hi!

Got it again !

You have to define a not used color for transparency.
By default this is fuchsia - because it's so ugly, that nobody uses it.


The code is now:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2.  
  3. begin
  4.  Image2.Picture.Icon.TransparentColor:= clFuchsia;
  5.  Image2.Picture.Icon.LoadFromFile('TuxMonTransparent.ico');
  6.  Image2.Picture.PNG.SaveToFile('TuxTest.png');
  7. end;
  8.  
Works with Linux without a probleml. Don't know about Windows.

If not we will do it with BGRA.

As proof: A beer drinking Tux with a  transparent hole!

Winni




SaraT

  • Full Member
  • ***
  • Posts: 124
  • A little student
Re: Converting TIcon to PNG
« Reply #9 on: February 16, 2020, 02:37:09 am »
Hi!

Got it again !

You have to define a not used color for transparency.
By default this is fuchsia - because it's so ugly, that nobody uses it.


The code is now:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2.  
  3. begin
  4.  Image2.Picture.Icon.TransparentColor:= clFuchsia;
  5.  Image2.Picture.Icon.LoadFromFile('TuxMonTransparent.ico');
  6.  Image2.Picture.PNG.SaveToFile('TuxTest.png');
  7. end;
  8.  
Works with Linux without a probleml. Don't know about Windows.

If not we will do it with BGRA.

As proof: A beer drinking Tux with a  transparent hole!

Winni

Many, many thanks Winni. Worked in Windows 10 with transparency  ;)
Many thanks for your time... I owe you a beer.

See you in next post xD
« Last Edit: February 16, 2020, 02:40:56 am by SaraT »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Converting TIcon to PNG
« Reply #10 on: February 16, 2020, 03:28:35 am »

 

TinyPortal © 2005-2018