Recent

Author Topic: [SOLVED] Copying canvas rect  (Read 1517 times)

stormfinger

  • New member
  • *
  • Posts: 7
[SOLVED] Copying canvas rect
« on: April 12, 2020, 07:42:12 pm »
hi
in the folowing code i try to copy certain Rectangle from png.. this code results in bad unrecognized picture format ..but it runs without error. if i skip png2  and use png to assign to pict2 it works ok ...rectangle values have been also changed to 100 100 200 200 ..stil unrecognizable format.

 
Code: Pascal  [Select][+][-]
  1.  var png, png2  : TPortableNetworkGraphic;
  2.    myPict2: TPicture;
  3.    sourceR: TRect;
  4. begin
  5.  
  6.   sourceR := TRect.Create(0, 0, 700, 700);;
  7.  
  8.   myPict2 := TPicture.create();
  9.   png := TPortableNetworkGraphic.Create();
  10.   png2 := TPortableNetworkGraphic.Create();
  11.  
  12.   ScreenDC := GetDC(0);
  13.   png.LoadFromDevice(ScreenDC);
  14.  
  15.   png2.Canvas.CopyRect(sourceR,png.Canvas,sourceR);
  16.  
  17.   myPict2.PNG.Assign(png2);
  18.   myPict2.PNG.SaveToFile('test.png');
  19. end;
  20.  

thank you for your time
« Last Edit: April 29, 2020, 01:23:21 pm by stormfinger »

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Copying canvas rect
« Reply #1 on: April 12, 2020, 08:06:37 pm »
you are not initializing png2 properly. It has no bit depth, canvas size, or any other image property set and you assume that the size of the image in both pngs is identical you might need to differentiate sourceR and DestR in the final solution.

stormfinger

  • New member
  • *
  • Posts: 7
Re: Copying canvas rect
« Reply #2 on: April 29, 2020, 01:22:25 pm »
thank you for your hint ..i added Canvas.Create and set its size now its working :)

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Copying canvas rect
« Reply #3 on: April 29, 2020, 08:24:31 pm »
thank you for your hint ..i added Canvas.Create and set its size now its working :)
remove canvas.create. Its only used when the canvas object is created and that is called automatically from the TPortableNetworkGraphic.Create(); line of code. You just created a memory leak (best case).

 

TinyPortal © 2005-2018