Here is an example of how to do.
Add a component TImage
, define an image in the property Picture
, of course, you can use a charging system for the image as you prefer, in the case of example let’s start the component already with a picture defined!
Declaration of Global Variables (I prefer to always do so):
num,
StartX,
StartY,
OldStartX,
OldStartY,
OldEndX,
OldEndY : Integer;
IsDown : Boolean;
JPG: TJpegImage;
Bmp,
Bmp1,
Bmp2 : TBitmap;
At the event MouseDown
of the component TImage
Let’s get the coordinates X and Y from Mouse:
procedure TForm5.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
IsDown := True;
StartX := X;
StartY := Y;
OldStartX := X;
OldStartY := Y;
OldEndX := X;
OldEndY := Y;
end;
Now at the event MouseMove
of the component TImage
Let’s draw the selection area:
procedure TForm5.Image1MouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
begin
if IsDown then
begin
Canvas.Pen.Style := psDot;
Canvas.Pen.Mode := pmNotXor;
Canvas.Rectangle(OldStartX, OldStartY, OldEndX, OldEndY);
OldEndX := X;
OldEndY := Y;
Canvas.Rectangle(StartX, StartY, X, Y);
end;
end;
Okay, we come to the final part, let’s drop the mouse button and save the image.
At the event MouseUp
of the component TImage
:
procedure TForm5.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
IsDown := False;
Canvas.Pen.Style := psDot;
Canvas.Pen.Mode := pmNotXor;
Canvas.Rectangle(OldStartX, OldStartY, OldEndX, OldEndY);
Bmp := TBitmap.Create;
JPG := TJpegImage.Create;
JPG.Assign(Bmp);
num := 90;
Image1.Picture.LoadFromFile('D:\imagem_original.bmp');
image1.Picture.Bitmap.Canvas.Brush.Style := bsClear;
if not (Image1.Picture.Graphic is TBitmap) then
raise Exception.Create('A imagem não é um Bitmap');
Bmp2 := TBitmap(Image1.Picture.Graphic);
Bmp1 := TBitmap.Create;
try
Bmp1.Width := Abs(OldEndX - OldStartX);
Bmp1.Height := Abs(OldEndY - OldStartY);
Bmp1.Canvas.CopyRect(Rect(0, 0, Bmp1.Width, Bmp1.Height), Bmp2.Canvas, Rect(OldStartX, OldStartY, OldEndX, OldEndY));
Bmp1.SaveToFile('D:\imagem_recortada.bmp');
finally
Bmp1.Free;
end;
end;
Note that on this line I am saving the image Automatically when you release the mouse button, you can customize this easily, do the test:
1 - Comment the line that contains, Bmp1.SaveToFile('D:\imagem_recortada.bmp');
2 - Remove from the section finally
the Bmp1.Free;
3 - Add a Tbutton component to the Click event:
procedure TForm5.Button1Click(Sender: TObject);
begin
Bmp1.SaveToFile('D:\imagem_recortada.bmp');
Bmp1.Free;
end;
And of course, you can use one SavePictureDialog
to become more professional!
Ready!
Junior Moreira, perfect guy, worked out, I just had to take this line that was giving dick: Image1.Picture.Loadfromfile('e: imagem_original.bmp');, in the case of your example I can only use image . bmp, if I want . jpg inside IMAGE1, which I need to change ?
– user7605
Yes, and to use JPEG you should not use this line 'if not (Image1.Picture.Graphic is Tbitmap) then' and the uses above Declare JPEG.
– Junior Moreira
Junior Moreira, these options I need to change also Bmp1 := Tbitmap.Create; ? in case to use JPG ?
– user7605