Recent

Author Topic: (SOLVED) Convert PNG transparency in white color  (Read 2358 times)

AsleyCruz

  • Jr. Member
  • **
  • Posts: 99
    • Graphic and web designer
(SOLVED) Convert PNG transparency in white color
« on: March 24, 2020, 07:26:33 pm »
Hi coders,

I have an PNG file with transparency in a TImage. I save it in JPG format but the transparency has black background.
Is there a function to convert the transparency in white color?

Many thanks a lot.
« Last Edit: March 27, 2020, 10:08:25 pm by AsleyCruz »
Graphic & web designer

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Convert PNG transparency in white color
« Reply #1 on: March 24, 2020, 08:14:23 pm »
Hi!

Why do you want to do that??

JPG has no transparency, it is a lossy format and with high compression it has a lousy quality.

Anyway:

Code: Pascal  [Select][+][-]
  1. Uses .... BGRAbitmap, BGRAdefaultBitmap;
  2.  
  3. ....
  4.  
  5. var BGRA: TBGRAbitmap;
  6.  
  7. begin
  8. BGRA := TBGRAbitmap.create (Image1.Picture.Bitmap);
  9. BGRA.ReplaceColor (BGRAPixelTransparent, clWhite);
  10. BGRA.Draw (Image1.Canvas,0,0);
  11. BGRA.Free;
  12. .....

Winni

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Convert PNG transparency in white color
« Reply #2 on: March 24, 2020, 08:24:39 pm »
One other, maybe more simple, way (without resorting to 3rd party libs) would be to create a white TBitmap of the proper dimensions, draw the PNG over it and convert that to JPEG.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

AsleyCruz

  • Jr. Member
  • **
  • Posts: 99
    • Graphic and web designer
Re: Convert PNG transparency in white color
« Reply #3 on: March 24, 2020, 08:45:47 pm »
Hi!

Why do you want to do that??

JPG has no transparency, it is a lossy format and with high compression it has a lousy quality.

Anyway:

Code: Pascal  [Select][+][-]
  1. .....

Winni

Many thanks winni but I dont know what I am doing wrong.
Please see attached image. Tested in a blank document.
Graphic & web designer

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Convert PNG transparency in white color
« Reply #4 on: March 24, 2020, 09:32:29 pm »
Hi!

Sorry - I forgot in the uses

BGRAbitmapTypes;

That's the whole secret.

Winni

AsleyCruz

  • Jr. Member
  • **
  • Posts: 99
    • Graphic and web designer
Re: Convert PNG transparency in white color
« Reply #5 on: March 24, 2020, 11:49:43 pm »
Hi!

Sorry - I forgot in the uses

BGRAbitmapTypes;

That's the whole secret.

Winni

Thanks Winni, it works and replaces transparent color of any BMP image only.
Unable to replace transparent color on PNG files.

Thanks

Graphic & web designer

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: Convert PNG transparency in white color
« Reply #6 on: March 25, 2020, 12:05:19 am »
Create a white bgrabitmap of the same size of the image. Then draw on top of it your transparent image.

AsleyCruz

  • Jr. Member
  • **
  • Posts: 99
    • Graphic and web designer
Re: Convert PNG transparency in white color
« Reply #7 on: March 25, 2020, 12:24:50 am »
Create a white bgrabitmap of the same size of the image. Then draw on top of it your transparent image.

Hi Lainz

What I tried, it replace all red background :/
Any idea to drop the PNG file?

Thanks a lot

Code: Pascal  [Select][+][-]
  1. BGRA := TBGRABitmap.Create(256,256,clred);
Graphic & web designer

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: Convert PNG transparency in white color
« Reply #8 on: March 25, 2020, 12:49:15 am »
Code: Pascal  [Select][+][-]
  1. var
  2.   bgBitmap, bgImage: TBGRABitmap;
  3.  
  4. bgImage := TBGRABitmap.Create('image.png');
  5.  
  6. bgBitmap := TBGRABitmap.Create(bgImage.Width, bgImage.Height, BGRAWhite);
  7.  
  8. bgBitmap.PutImage(0,0,bgImage, dmDrawWithTransparency);
  9.  
  10. bgImage.Free;
  11.  
  12. bgBitmap.SaveToFile('image.jpg');
  13.  
  14. bgBitmap.Free;

AsleyCruz

  • Jr. Member
  • **
  • Posts: 99
    • Graphic and web designer
Re: Convert PNG transparency in white color
« Reply #9 on: March 25, 2020, 01:25:13 am »
Code: Pascal  [Select][+][-]
  1. var
  2.   bgBitmap, bgImage: TBGRABitmap;
  3.  
  4. bgImage := TBGRABitmap.Create('image.png');
  5.  
  6. bgBitmap := TBGRABitmap.Create(bgImage.Width, bgImage.Height, BGRAWhite);
  7.  
  8. bgBitmap.PutImage(0,0,bgImage, dmDrawWithTransparency);
  9.  
  10. bgImage.Free;
  11.  
  12. bgBitmap.SaveToFile('image.jpg');
  13.  
  14. bgBitmap.Free;

Oye, tu componente es excelente, lástima no conocer bien sus capacidades.
Apenas en 2 proyectos míos lo he usado, pocas líneas y grandes resultados.
Mil gracias bro.
Graphic & web designer

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: Convert PNG transparency in white color
« Reply #10 on: March 25, 2020, 02:46:04 am »
Isn't mine. The author is circular
I only contribute in bgracontrols

 

TinyPortal © 2005-2018