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

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 How to Crop but keep canvas size

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

View 
UserName:
Password:
Format  Bold Italicized Underline  Align Left Centered Align Right  Horizontal Rule  Insert Hyperlink   Browse for an image to attach to your post Browse for a zip to attach to your post Insert Code  Insert Quote Insert List
   
Message 

 

Emoji
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Black Eye [B)]
Frown [:(] Shocked [:0] Angry [:(!] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
AndyColmes Posted - Sep 22 2012 : 00:31:16
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
2   L A T E S T    R E P L I E S    (Newest First)
AndyColmes Posted - Sep 23 2012 : 22:48:20
Thank you William. I will give it a try.
w2m Posted - Sep 22 2012 : 10:09:23
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