Bookstore

Recent

Author Topic: (SOLVED) Draw like selection on TImage  (Read 544 times)

AsleyCruz

  • Jr. Member
  • **
  • Posts: 55
    • Graphic and apps designer
(SOLVED) Draw like selection on TImage
« on: March 21, 2020, 02:41:19 am »
Hi there,

I would like you to help me draw a selection on a TImage like the sample, attached gif.
and show the selection on a new TImage. If it is possible, share please a sample.

Thanks a lot guys... Asley.

« Last Edit: March 23, 2020, 06:43:08 pm by AsleyCruz »
AsleyCruz - Graphic and apps designer
-
http://www.asleycruz.com/

winni

  • Hero Member
  • *****
  • Posts: 1112
Re: Draw like selection on TImage
« Reply #1 on: March 21, 2020, 03:06:23 am »
Hi!

Put a second Image on your Form.
I assume that the original is Image1 and the copy is Image2.
Here we go:
Code: Pascal  [Select]
  1. var P1, P2 : TPoint;
  2.       RSource, RDest: TRect;
  3.  
  4. Procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  5.      Shift: TShiftState; X, Y: Integer);    
  6. begin
  7. P1 := Point (x,y);
  8. end;
  9.  
  10. Procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  11.      Shift: TShiftState; X, Y: Integer);    
  12. begin
  13. if ssleft in Shift then
  14.   begin
  15.   P2 := Point(x,y);
  16.   RSource := Rect(P1.x,P1.y,P2.x,P2.y);
  17.   RSource.Normalize;
  18.   Image2.Width := RSource.Width;
  19.   Image2.Height := RSource.Height;
  20.   RDest := Rect (0,0,Image2.Width, Image2.Height);
  21.   Image2.Canvas.CopyRect(RDest, Image1.Canvas,RSource);
  22.    end; // ssleft
  23. end;
  24.  
Winni

AsleyCruz

  • Jr. Member
  • **
  • Posts: 55
    • Graphic and apps designer
Re: Draw like selection on TImage
« Reply #2 on: March 21, 2020, 03:59:14 am »

Many thanks winni but I got an error. Does it need a special unit?
I tried in a blank document and the same error.

Please, see attached image.

Error: identifier idents no member "Normalize"

AsleyCruz - Graphic and apps designer
-
http://www.asleycruz.com/

eljo

  • Full Member
  • ***
  • Posts: 159
Re: Draw like selection on TImage
« Reply #3 on: March 21, 2020, 04:28:26 am »
Code: Pascal  [Select]
  1. function normalize(const Rec:TRect):Trect;
  2. begin
  3.   Result.Left := min(Rec.Left,Rec.Right);
  4.   Result.Right := Max(Rec.Left,Rec.Right);
  5.   Result.Top := Min(Rec.Top,Rec.Bottom);
  6.   Result.Bottom := Max(Rec.Top,Rec.Bottom);
  7. end;
  8.  
Thats all the normalize does.

PS. This was typed directly in the browser and I do not recall if the field names are correct or not.

AsleyCruz

  • Jr. Member
  • **
  • Posts: 55
    • Graphic and apps designer
Re: Draw like selection on TImage
« Reply #4 on: March 21, 2020, 04:35:53 am »
Code: Pascal  [Select]
  1. function normalize(const Rec:TRect):Trect;
  2. begin
  3.   Result.Left := min(Rec.Left,Rec.Right);
  4.   Result.Right := Max(Rec.Left,Rec.Right);
  5.   Result.Top := Min(Rec.Top,Rec.Bottom);
  6.   Result.Bottom := Max(Rec.Top,Rec.Bottom);
  7. end;
  8.  
Thats all the normalize does.

PS. This was typed directly in the browser and I do not recall if the field names are correct or not.

Thanks a lot eljo, for your support, works greate now
What if I wanted to draw a rectangle/shape while mouse is down?
Like attached image or gif below.

Regards.
« Last Edit: March 21, 2020, 04:37:31 am by AsleyCruz »
AsleyCruz - Graphic and apps designer
-
http://www.asleycruz.com/

eljo

  • Full Member
  • ***
  • Posts: 159
Re: Draw like selection on TImage
« Reply #5 on: March 21, 2020, 05:32:54 am »
Code: Pascal  [Select]
  1. function normalize(const Rec:TRect):Trect;
  2. begin
  3.   Result.Left := min(Rec.Left,Rec.Right);
  4.   Result.Right := Max(Rec.Left,Rec.Right);
  5.   Result.Top := Min(Rec.Top,Rec.Bottom);
  6.   Result.Bottom := Max(Rec.Top,Rec.Bottom);
  7. end;
  8.  
Thats all the normalize does.

PS. This was typed directly in the browser and I do not recall if the field names are correct or not.

Thanks a lot eljo, for your support, works greate now
What if I wanted to draw a rectangle/shape while mouse is down?
Like attached image or gif below.

Regards.
use image1MouseMove instead of mouseup eg
Code: Pascal  [Select]
  1. var Btn : Integer = -1;
  2.       P1, P2 : TPoint;
  3.       RSource, RDest: TRect;
  4.  
  5. Procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  6.      Shift: TShiftState; X, Y: Integer);    
  7. begin
  8.   P1 := Point (x,y);
  9.   Btn := integer(Button);
  10. end;
  11.  
  12. procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  13. begin
  14.   if (btn <> -1) and (ssleft in Shift) then
  15.   begin
  16.     P2 := Point(x,y);
  17.     RSource := Rect(P1.x,P1.y,P2.x,P2.y);
  18.     RSource.Normalize;
  19.     Image2.Width := RSource.Width;
  20.     Image2.Height := RSource.Height;
  21.     RDest := Rect (0,0,Image2.Width, Image2.Height);
  22.     Image2.Canvas.CopyRect(RDest, Image1.Canvas,RSource);
  23.   end; // ssleft
  24. end;
  25.  
  26.  
  27. Procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  28.      Shift: TShiftState; X, Y: Integer);    
  29. begin
  30.   Btn := -1;
  31. end;
  32.  
Keep in mind that the declaration of Btn is shown with the initial  required value, which is valid for local variables (ee inside a procedure/function) but not for global (ee classes and variables in the unit) as far as I remember.

AsleyCruz

  • Jr. Member
  • **
  • Posts: 55
    • Graphic and apps designer
Re: (SOLVED) Draw like selection on TImage
« Reply #6 on: March 23, 2020, 06:57:22 pm »
Hi coders,
Here you go, the final project with an attached gif to show how it works.

Many thanks to Winni for his great help.
AsleyCruz - Graphic and apps designer
-
http://www.asleycruz.com/

AsleyCruz

  • Jr. Member
  • **
  • Posts: 55
    • Graphic and apps designer
Re: (SOLVED) Draw like selection on TImage
« Reply #7 on: March 23, 2020, 06:58:53 pm »
The project...
AsleyCruz - Graphic and apps designer
-
http://www.asleycruz.com/

krolikbest

  • Full Member
  • ***
  • Posts: 112
Re: (SOLVED) Draw like selection on TImage
« Reply #8 on: March 26, 2020, 06:38:12 pm »
Because sometimes people ask how to do transparent selection (not to use additional techniques) simple example in attachment.