Try this, it is fast and you do not have to create a new bitmap:
procedure TForm1.CropAndFit1Click(Sender: TObject);
var
iWidth: integer;
iHeight: integer;
iSelWidth: integer;
iSelHeight: integer;
iX1: integer;
iY1: integer;
iX2: integer;
iY2: integer;
begin
if ImageEnView1.Selected then
begin
// get selection rect
iX1 := ImageEnView1.SelX1;
iY1 := ImageEnView1.SelY1;
iX2 := ImageEnView1.SelX2;
iY2 := ImageEnView1.SelY2;
iSelWidth := iX2 - iX1;
iSelHeight := iY2 - iY1;
// get the canvas dimensions
iWidth := ImageEnView1.IEBitmap.Width;
iHeight := ImageEnView1.IEBitmap.Height;
ImageEnView1.Proc.SaveUndoCaptioned('CropAndFit ' + IntToStr(ImageEnView1.Proc.UndoCount));
Undo1.Hint := 'CropAndFit ' + IntToStr(ImageEnView1.Proc.UndoCount);
// copy selection to the clipboard
ImageEnView1.Proc.SelCopyToClip(True);
// remove the selection
ImageEnView1.DeSelect;
// fill the bitmap with white
ImageEnView1.Proc.Fill(clWhite);
// create a selection in the center of the image
ImageEnView1.Select((iWidth div 2) - (iSelWidth div 2), (iHeight div 2) - (iSelHeight div 2),
(iWidth div 2) - (iSelWidth div 2) + iSelWidth, (iHeight div 2) - (iSelHeight div 2) + iSelHeight);
// paste the clipboard to the selection
ImageEnView1.Proc.SelPasteFromClip(true, true);
ImageEnView1.Update;
// remove the selection
ImageEnView1.DeSelect;
Undo1.Enabled := ImageEnView1.Proc.CanUndo;
ImageEnView1.Bitmap.Modified := True;
end
else
begin
MessageBox(0, 'There is no selection. Please make a selection and try again.', 'No Selection', MB_ICONWARNING or MB_OK);
end;
end;
William Miller
Adirondack Software & Graphics
Email: w2m@frontiernet.net
EBook: http://www.imageen.com/ebook/
Apprehend: http://www.frontiernet.net/~w2m/index.html