ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 How to Crop but keep canvas size
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

AndyColmes

USA
351 Posts

Posted - Sep 22 2012 :  00:31:16  Show Profile  Reply
I would like to crop an image using a selection, but still keep the original canvas size with the cropped image in the middle of the canvas. What would be the most efficient way to do this?

Do I do the crop and copy the cropped image to a new image with the same canvas size as the original? Or is there a better way to do this?

I would appreciate some code snippets, especially how to put the cropped image in the middle of the canvas.

Thanks very much in advance.

Andy

w2m

USA
1990 Posts

Posted - Sep 22 2012 :  10:09:23  Show Profile  Reply
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
Go to Top of Page

AndyColmes

USA
351 Posts

Posted - Sep 23 2012 :  22:48:20  Show Profile  Reply
Thank you William. I will give it a try.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: