programmerdelphi2k 237 Posted February 9, 2023 (edited) {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var LPictureSource: TPicture; // load a PNG LPictureTarget: TPicture; // to resize like a Bitmap LPNGresulted : TPngImage; // to save PNG resulted LBtmPointerTmp: TBitmap; begin LBtmPointerTmp := TBitmap.Create(1, 1); LPictureSource := TPicture.Create; LPictureTarget := TPicture.Create; LPNGresulted := TPngImage.Create; try LPictureSource.Assign(Image2.Picture); // LPictureSource.LoadFromFile('..\..\IronMan.png'); LPictureSource.Graphic.Transparent := True; // Memo1.Text := 'IronMan.png = ' + LPictureSource.Width.ToString + 'x' + LPictureSource.Height.ToString; // LPictureTarget.Bitmap.Assign(LBtmPointerTmp); // Create(1, 1); // just for create intervalues values to canvas, etc... LPictureTarget.Graphic.Transparent := True; // // new size LPictureTarget.Bitmap.SetSize( Trunc(LPictureSource.Width * 0.4), Trunc(LPictureSource.Height * 0.4) ); // // draw on new canvas resized LPictureTarget.Bitmap.Canvas.StretchDraw(LPictureTarget.Bitmap.Canvas.ClipRect, LPictureSource.Graphic); // Memo1.Lines.Add(LPictureTarget.Width.ToString + 'x' + LPictureTarget.Height.ToString); // LPNGresulted.Assign(LPictureTarget.Bitmap); // Memo1.Lines.Add('PNG = ' + LPNGresulted.Width.ToString + 'x' + LPNGresulted.Height.ToString); // LPNGresulted.SaveToFile('resulted.png'); // just for test load file... // Image1.Picture.LoadFromFile('resulted.png'); Image1.Proportional := True; finally LPNGresulted.Free; LPictureTarget.Free; LPictureSource.Free; LBtmPointerTmp.Free; end; end; initialization ReportMemoryLeaksOnShutdown := True; finalization end. Edited February 9, 2023 by programmerdelphi2k Share this post Link to post
dmitrok2006 2 Posted February 10, 2023 Strange... of course... But when I add the saved picture to photoshop, the picture with a white background... Share this post Link to post
Anders Melander 1770 Posted February 10, 2023 Just so you know, while all the code in this thread might be fine for "I'm a hobbyist and I don't know what the hell I'm doing"-level programming, it's nowhere near the best or correct way to solve your problem. With that said, the reason you're losing transparency is that you are operating on the rendered visual representation of the image. Therefore the transparency has been replaced with a background color. In order to resize a PNG without losing transparency you will need to use a method that supports alpha transparency. This means: Convert the PNG to a 32-bit RGBA bitmap. Resample (resize) this bitmap to the desired size. Convert the bitmap to PNG. There are various libraries that can do these steps for you. For example Graphics32 and probably also Image32. I'm guessing Image32 will probably be the easiest for you to understand. 2 1 Share this post Link to post
programmerdelphi2k 237 Posted February 10, 2023 (edited) sorry Melander, as you know my knowledge is not near your, but it's the life!!! Luck for us "have someone like you" for fix our error! go ahead... in my test, the "Ironman - result.png" have background transparent Edited February 10, 2023 by programmerdelphi2k 1 Share this post Link to post
dmitrok2006 2 Posted February 10, 2023 In any case, thanks for the help. ))) Share this post Link to post