dmitrok2006 2 Posted February 8, 2023 Hello! Need help on this topic. In general, I will describe in more detail. I throw Image1 on the form. Manually (just manually) I load a Png image there, say 600x700, with transparency. Using the code, I need to resize this image loaded in Image1 and then save it to disk in new sizes. The Stretch property doesn't help, because on the contrary, Image1 is adjusted to fit the image and as a result, when saved, Png of the same sizes. I tried Image1.Picture.graphic.setsize, but then a piece of the picture is saved Help. pliz...... Share this post Link to post
programmerdelphi2k 237 Posted February 9, 2023 (edited) I think that is VCL, not? Image1.Picture.Graphic.SetSize(...) ... procedure TForm1.Button1Click(Sender: TObject); var LPictureSource: TPicture; // load a PNG LPictureTarget: TPicture; // to resize like a Bitmap LPNGresulted : TPngImage; // to save PNG resulted begin LPictureSource := TPicture.Create; LPictureTarget := TPicture.Create; LPNGresulted := TPngImage.Create; try LPictureSource.LoadFromFile('Spiderman.png'); // with transparent background on png LPictureSource.Graphic.Transparent := True; // Memo1.Text := 'Spiderman.png = ' + LPictureSource.Width.ToString + 'x' + LPictureSource.Height.ToString; // LPictureTarget.Bitmap.Create(1, 1); // just for create internal values to canvas, etc... LPictureTarget.Graphic.Transparent := True; // // new size LPictureTarget.Bitmap.Width := Trunc(LPictureSource.Width * 4.0); // size x 4 LPictureTarget.Bitmap.Height := Trunc(LPictureSource.Height * 4.0); // // 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; end; end; Edited February 9, 2023 by programmerdelphi2k 1 Share this post Link to post
dmitrok2006 2 Posted February 9, 2023 programmerdelphi2k, thank you very much. I will try. )))) Share this post Link to post
dmitrok2006 2 Posted February 9, 2023 programmerdelphi2k, I want to ask .... Maybe I don't understand something... But the code uses LoadFromFile, and I need to load the image manually, without using LoadFromFile. Share this post Link to post
programmerdelphi2k 237 Posted February 9, 2023 @dmitrok2006 you need "load" your image using any way... file, stream, resource, etc...! you decide how to? Share this post Link to post
dmitrok2006 2 Posted February 9, 2023 I'm fine with any boot method that doesn't access the disk while the exe file is running... For example, in my project, I manually loaded PNG into Image1.... Share this post Link to post
programmerdelphi2k 237 Posted February 9, 2023 @dmitrok2006 when loading in "design-time" your image is "embedded" in you exe! 1 Share this post Link to post
programmerdelphi2k 237 Posted February 9, 2023 11 hours ago, programmerdelphi2k said: // draw on new canvas resized LPictureTarget.Bitmap.Canvas.StretchDraw(LPictureTarget.Bitmap.Canvas.ClipRect, LPictureSource.Graphic); after this line, "LPictureSource" it's not necessary anymore, then, you can " LPictureSource.Graphic := nil" 1 Share this post Link to post
dmitrok2006 2 Posted February 9, 2023 58 minutes ago, programmerdelphi2k said: @dmitrok2006 you need "load" your image using any way... file, stream, resource, etc...! you decide how to? On Form1 I don't need to put Image1 ? Share this post Link to post
programmerdelphi2k 237 Posted February 9, 2023 (edited) To tell the truth, you would only need to embed a "resource" (image, sound, etc...) in strictly necessary case, where to load it... would be something very time consuming, or if the resource doesn't cost much for your system (memory, performance, etc...)! Overall you could just load it "on-demand" Using a component is just to make the job easier, in fact you can create it at run-time. Or, if you don't need a component, you can just create the component class to do your background work... in code! Edited February 9, 2023 by programmerdelphi2k 1 Share this post Link to post
dmitrok2006 2 Posted February 9, 2023 Thank you very much, I'll look into it )))) 1 Share this post Link to post
programmerdelphi2k 237 Posted February 9, 2023 (edited) var MyImage: TImage; begin MyImage := TImage.Create(nil); try MyImage.Left := 0; // any values ... MyImage.Top := 0; MyImage.Width := 0; // ... MyImage.Picture.LoadFromFile('....'); // MyImage.Parent := self; // self = form1 -> "parent" to appears on screen of form area!! finally MyImage.Free; end; Edited February 9, 2023 by programmerdelphi2k 1 Share this post Link to post
dmitrok2006 2 Posted February 9, 2023 That is, in the last code at the development stage, the image will be loaded and the file on disk can be deleted? 1 Share this post Link to post
programmerdelphi2k 237 Posted February 9, 2023 not necessary! just if you want and be sure about this! 1 Share this post Link to post
programmerdelphi2k 237 Posted February 9, 2023 thanks for all "like", , :))) 1 Share this post Link to post
dmitrok2006 2 Posted February 9, 2023 It's a bit hard to understand, but I'll try. Thank you ))))) Share this post Link to post
dmitrok2006 2 Posted February 9, 2023 Programmerdelphi2k... One more question.... I made this code, but the transparency seems to have disappeared in the saved PNG. Why? var Form1: TForm1; PNGSource: TPicture; PNGTarget: TPicture; PNGresult: TPngImage; implementation {$R *.dfm} uses Unit2; procedure TForm1.a0Click(Sender: TObject); begin PNGSource:= TPicture.Create; PNGTarget:= TPicture.Create; PNGresult:= TPngImage.Create; PNGSource:=form2.Aa.Picture; PNGSource.Graphic.Transparent := True; PNGTarget.Bitmap.Width := Trunc(PNGSource.Width / 4.0); PNGTarget.Bitmap.Height := Trunc(PNGSource.Height / 4.0); PNGTarget.Bitmap.Canvas.StretchDraw(PNGTarget.Bitmap.Canvas.ClipRect, PNGSource.Graphic); PNGresult.Assign(PNGTarget.Bitmap); PNGresult.SaveToFile('c:\1DU\As.png'); Share this post Link to post
programmerdelphi2k 237 Posted February 9, 2023 form2.Aa.Picture = "AA" have a "transparence" activated or have a transparence? 1 Share this post Link to post
programmerdelphi2k 237 Posted February 9, 2023 (edited) https://www.pngegg.com/en/png-zhwea <-- IronMan PNG images!!! Here my TImage2 components is using a "Iron men PNG" in design-time, then I just assign it to my "var LPictureSource" look the code... the rest is the same as above Edited February 9, 2023 by programmerdelphi2k 1 Share this post Link to post
dmitrok2006 2 Posted February 9, 2023 Can I email the project for you to fix? Please) Share this post Link to post
dmitrok2006 2 Posted February 9, 2023 There I forgot to write in Var .... Share this post Link to post
programmerdelphi2k 237 Posted February 9, 2023 a little fix to avoid a "memory leaks"... Quote replace this LPictureTarget.Bitmap.Create(1, 1); // just for create internal values to canvas, etc... = memory leaks on end-app!!! with a new obj TBitmap as usual Quote BmpTmp := TBitmap.Create(1,1); ... LPictureTarget.Bitmap.Assign(BmpTmp); ... BmpTmp.free; // when it's not more necessary! 1 Share this post Link to post
dmitrok2006 2 Posted February 9, 2023 Programmerdelphi2k... Wow, such a big code)))) I need a few hours to understand. So I'll write the answer tomorrow, ok? Thank you, you are a good person ) Share this post Link to post