What is the best way to convert JPG file into PNG? lazarus / 43-Android ---------------------------------------------------------------------- aenek · 2023-09-12 12:59 Hello all, I'm new in Lazarus @ Android, but this is great that it works (mostly  ), thank you. I need to convert jpg file into png file and I hope you can help me What is the best way to do it? For now I use Base64 with jBitmap component B64:=B2.GetBase64StringFromImage(B2.GetImage(),cfPNG); then I have to put it back to jbitmap to get size of PNG stream: B1.SetImage(B2.GetImageFromBase64String(B64));  SizeB:=B1.GetByteArrayFromBitmap(table); is there any simpler way to do it? thanks, kind regards ---------------------------------------------------------------------- mika · 2023-09-12 15:27 You can adapt this code. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  fpreadjpeg, fpimage, fpwritePNG; var  image: TFPCustomImage;  reader: TFPCustomImageReader;  writer: TFPWriterPNG;  AFileName : string; begin  AFileName:='/Downloads/gx280.jpg';   Image := TFPMemoryImage.Create(0, 0);  Reader := TFPReaderJpeg.Create;  Writer := TFPWriterPNG.Create;  Image.LoadFromFile(AFileName, Reader);   Image.SaveToFile('mynewimg.png',Writer);{  Writeln;  writeln(' Width ',Image.Width,'  Height ', Image.Height);  Writeln;}  image.Free;  Reader.Free;  Writer.Free;end. ---------------------------------------------------------------------- aenek · 2023-09-13 06:46 Thank you Mika, I will try to adopt this but I have some doubts. I didn't describe entire case here so this is what I want to achieve: 1. User opens graphics file (jpg/png) - android file picker 2. I show this graphics on screen (jImageView) 3. User can rotate picture by pressing button - it can see proper rotation 4. User enters desired width and height for 600 dpi image 5. When user clicks Print button, I rotate, resize, convert to PNG format and send it as part of printout to Epson printer by jTCPClient This scenario works but it takes ages to send data to printer, I Think it is not too efficient to use this Base64 Note: This is android >=11 so I'm affraid of scooped storage As I understand we can not simply move bitmap/image data between jComponents and standard FCL,freepascal (win64), Am I right? ---------------------------------------------------------------------- mika · 2023-09-13 08:02 Quote from: aenek on September 13, 2023, 08:46:51 amThis scenario works but it takes ages to send data to printer, I Think it is not too efficient to use this Base64 Even base64 conversion takes time, don't put all blame on it. Converting image to PNG is time consuming (a lot calculation involved). If image is photography of real wold then it's very inefficient to use PNG format. For example if jpeg is ~3 Mb then raw bmp data is ~40 Mb and converted to PNG is ~30 Mb. As well you have to take in account speed of communication channel with printer. You might want to explore what other image formats printer accepts. Quote from: aenek on September 13, 2023, 08:46:51 amAs I understand we can not simply move bitmap/image data between jComponents and standard FCL,freepascal (win64), Am I right? You are right. It is all pain to deal with image inner structure in jComponents. ---------------------------------------------------------------------- aenek · 2023-09-13 12:52 Hi Mika, I'm affraid that these Epson printers support only PNG files for color graphics. This is Epson extension of zebra thermal-transfer printer codes (ZPLII). For me personally it is stunning that this LAMW really works, so thank you all members involved in this project and support community. Mika, thank you anyway. ---------------------------------------------------------------------- Mongkey · 2023-09-15 09:37 It is easy if you were know a bit about java, then call your custom function via pascal in LAMW https://stackoverflow.com/questions/15757574/androidsave-captured- image-format-from-jpeg-to-png-in-internal-storage-director ---------------------------------------------------------------------- cc1989 · 2026-09-03 08:45 Hi! 😄 Your Base64 way works but is indeed roundabout. Here are simpler options: 1. Direct in Pascal (no Base64!) Load JPG → save as PNG to stream → get size directly: pascal PNGStream := TMemoryStream.Create; try  B2.GetImage().SaveToStreamWithFormat(PNGStream, cfPNG);   SizeB := PNGStream.Size; // done! finally   PNGStream.Free; end; 2. Call Java (Android built-in) As that StackOverflow post shows — use Android's Bitmap.compress(PNG, ...) natively via LAMW Java bridge, very efficient. 3. Quick conversion tool Need instant PNG files for testing? https://ahaconvert.com/JPG-to-PNG/ — free, lossless, no signup. ---------------------------------------------------------------------- Thaddy · 2026-09-03 09:25 It is enough to load it in a TPicture and choose the png format to save it. Will add example later.Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit unit1; {$mode unleashed} interface uses  classes, sysutils, forms, controls, graphics, dialogs, ExtCtrls, StdCtrls; type  tform1 = class(tform)    button1: tbutton;    button2: tbutton;    image1: timage;    opendialog1: topendialog;    savedialog1: tsavedialog;    procedure button1click(sender: tobject);    procedure button2click(sender: tobject);  private   public   end; var  form1: tform1; implementation {$R *.lfm} procedure tform1.button1click(sender: tobject);beginOpenDialog1.Filter := 'Jpeg file|*.jpg;*.jpg'; with OpenDialog1 do if execute then   Image1.Picture.LoadFromFile(Filename)end; procedure tform1.button2click(sender: tobject);begin  SaveDialog1.filter :='png file|*.png';  with savedialog1 do if execute then image1.Picture.SaveToFile(filename);end; end. ---------------------------------------------------------------------- rvk · 2026-09-03 09:33 And you think TS is still around?  The problem also wasn't strictly the conversion to png, but sending the massive png result to the printer. Quote from: aenek on September 13, 2023, 08:46:51 amThis scenario works but it takes ages to send data to printer, I Think it is not too efficient to use this Base64 ---------------------------------------------------------------------- Thaddy · 2026-09-03 10:03 @rvk You missed it was added to today and with a wrong answer. That needs correction. The question re-occurred and the answer wasn't there....or plain wrong.. @cc1989 !!  I do not need that silly base64 ....or other shenanigans. Look at my example. You can also use the same mechanism off-line, without gui. ---------------------------------------------------------------------- Hobbit00 · 2026-09-03 10:09 In Delphi you can do something like that, in simple way (under Windows) ... but near the same is in FMX (for Android like you need). I think in Lazarus there is near the same ... I dont know for LAMW but if base classes are used the same code should work ... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  Vcl.Imaging.pngimage, Vcl.Imaging.jpeg; // Vcl.Graphics; procedure JpegtoPng;var  ImgPng: TPngImage;  ImgIn: TJPEGImage;begin  ImgIn := TJPEGImage.Create;  ImgPng := TPngImage.Create;  try    ImgIn.LoadFromFile('C:\path\to\image.jpg');     //Do what you want    //.....     ImgPng.Assign(ImgIn);     // Set compression level: 0 = none, 9 = max compression    ImgPng.CompressionLevel := 6;     ImgPng.SaveToFile('C:\path\to\compressed.png');    //or to stream    //ImgPng.SaveToStream(Stream);  finally    ImgIn.Free;    ImgPng.Free;  end;end; ---------------------------------------------------------------------- Thaddy · 2026-09-03 10:12 @Hobbit00 That is overly complex too, not wrong persé, but see my example. Tpicture does automatic conversions.(also in Delphi) ---------------------------------------------------------------------- Hobbit00 · 2026-09-03 10:16 Quote from: Thaddy on September 03, 2026, 12:12:59 pm@Hobbit00 That is overly complex too, not wrong persé, but see my example. Tpicture does automatic conversions.(also in Delphi) Yes, I know that. But if you want to set something (like a specific compression) I think you have to do that ... anyway, the conversions now are implicit in lot of type of file, so it's simple to do that in all platform. ---------------------------------------------------------------------- Thaddy · 2026-09-03 10:24 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i image1.picture.Jpeg.CompressionQuality but that isn't there for png: that takes the jpg compression and converts ad verbatum. ---------------------------------------------------------------------- dseligo · 2026-09-03 11:33 Quote from: Thaddy on September 03, 2026, 11:25:15 amIt is enough to load it in a TPicture and choose the png format to save it. You can't use TPicture in LAMW (you have no LCL). ---------------------------------------------------------------------- Hobbit00 · 2026-09-03 12:20 Quote from: Thaddy on September 03, 2026, 12:24:09 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i image1.picture.Jpeg.CompressionQuality but that isn't there for png: that takes the jpg compression and converts ad verbatum. Compressing to PNG is lossless, while compressing to JPEG is lossy. ---------------------------------------------------------------------- Thaddy · 2026-09-03 12:35 I know. You mentioned compression, so I pointed at that. ---------------------------------------------------------------------- Thaddy · 2026-09-03 13:51 Quote from: dseligo on September 03, 2026, 01:33:41 pmYou can't use TPicture in LAMW (you have no LCL). Actually, you can, but you need to compile it to a .so in fully native code. Depends on the device, of course (all arm code works) . LAMW on its own is more portable, ---------------------------------------------------------------------- rvk · 2026-09-03 14:06 And where do you all convert the png to base64 again? Because the question was for sending it to a Epson ColorWorks printer (ZPLII) which only accepts PNG in base64  and that was slow (sending to the printer). That slowness was the whole problem.  Not the conversion to png. ---------------------------------------------------------------------- Thaddy · 2026-09-03 14:41 @rvk I don't... ---------------------------------------------------------------------- Hobbit00 · 2026-09-03 17:20 I wanted to point the fact to compress the PNG ... after that save the file to a stream and use something like (I don't know in Lazarus / FPC how is the exact function: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i stringresult := EncodeBase64(stream, stream.Size);  BTW, I remember something about CRC when you send data to zebra printer ... vague memories ... ---------------------------------------------------------------------- Thaddy · 2026-09-03 18:51 @Hobbit00 I have lost the plot here, so it is actually about a particular printer that can print Base64 as an image???? Is there a market for that?  Does it exist???? ---------------------------------------------------------------------- rvk · 2026-09-03 19:16 Quote from: Thaddy on September 03, 2026, 08:51:05 pm@Hobbit00 I have lost the plot here, so it is actually about a particular printer that can print Base64 as an image???? Is there a market for that?  Does it exist????  Yes, you could print to those older Epson printers directly using escape codes. If you look at the discussion you see TS using jTCPClient to print on an Android app. But it takes a long time. For example https://stackoverflow.com/questions/68562855/how-to-send-color-images- with-esc-pos Quote from: aenek on September 13, 2023, 08:46:51 amThank you Mika, I will try to adopt this but I have some doubts. I didn't describe entire case here so this is what I want to achieve: 1. User opens graphics file (jpg/png) - android file picker 2. I show this graphics on screen (jImageView) 3. User can rotate picture by pressing button - it can see proper rotation 4. User enters desired width and height for 600 dpi image 5. When user clicks Print button, I rotate, resize, convert to PNG format and send it as part of printout to Epson printer by jTCPClient This scenario works but it takes ages to send data to printer, I Think it is not too efficient to use this Base64 Note: This is android >=11 so I'm affraid of scooped storage As I understand we can not simply move bitmap/image data between jComponents and standard FCL,freepascal (win64), Am I right? ---------------------------------------------------------------------- Hobbit00 · 2026-09-03 19:41 Quote from: Thaddy on September 03, 2026, 08:51:05 pm@Hobbit00 I have lost the plot here, so it is actually about a particular printer that can print Base64 as an image???? Is there a market for that?  Does it exist????  Like @rvk said, Zebra printers are very "special" industrial printers. They are widley used and they can be used by PLC sending ascii text (that is the use of BASE64), via RS232 too  I had not to do with them, but I know my customers that used them ... a lot of    (P.S.: those are about my customers, not about you) ---------------------------------------------------------------------- cc1989 · 2026-09-04 07:33 You don't need to convert the JPEG to Base64 and back to a bitmap. That's an unnecessary step. The simplest way is to load the JPG into a TBitmap and save it as a PNG with TPortableNetworkGraphic. Bitmap.LoadFromFile('image.jpg'); PNG.Assign(Bitmap); PNG.SaveToFile('image.png'); This is faster and avoids the extra Base64 conversion. If you just want to convert a JPG to PNG without coding quickly, a free online converter such as AhaConvert also works in your browser: https://ahaconvert.com/JPG-to-PNG/ ---------------------------------------------------------------------- Thaddy · 2026-09-04 08:16 No, the simplest way is like I showed you....