Saturday, November 23, 2019

Proportionally Resize an Image (TBitmap)

Proportionally Resize an Image (TBitmap) In graphics programming a thumbnail is a reduced-size version of a picture. Heres an idea for your next application: create a form picker to let users easily select and navigate through open forms by displaying thumbnails of them all in a dialog window. Interesting idea? Sounds like the Quick Tabs feature of the IE 7 browser :) Before actually creating such a neat feature for your next Delphi application, you need to know how to grab the image of the form (form-screen shot) and how to proportionally resize it to the desired thumbnail image. Proportional Picture Resizing: Creating Thumbnail Graphics Below you will find a block of code to take the image of a form (Form1) by using the GetFormImage method. The resulting TBitmap is then resized to fit the maximum thumbnail width (200 pixels) and/or height (150 pixels).Resizing maintains the aspect ratio of the image. The resulting image is then displayed in a TImage control, named Image1. const   Ã‚  maxWidth 200;   Ã‚  maxHeight 150; var   Ã‚  thumbnail : TBitmap;   Ã‚  thumbRect : TRect; begin   Ã‚  thumbnail : Form1.GetFormImage;   Ã‚  try   Ã‚  Ã‚  Ã‚  thumbRect.Left : 0;   Ã‚  Ã‚  Ã‚  thumbRect.Top : 0;   Ã‚  Ã‚  Ã‚  //proportional resize   Ã‚  Ã‚  Ã‚  if thumbnail.Width thumbnail.Height then   Ã‚  Ã‚  Ã‚  begin   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  thumbRect.Right : maxWidth;   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  thumbRect.Bottom : (maxWidth * thumbnail.Height) div thumbnail.Width;   Ã‚  Ã‚  Ã‚  end   Ã‚  Ã‚  Ã‚  else   Ã‚  Ã‚  Ã‚  begin   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  thumbRect.Bottom : maxHeight;   Ã‚  Ã‚  Ã‚  Ã‚  Ã‚  thumbRect.Right : (maxHeight * thumbnail.Width) div thumbnail.Height;   Ã‚  Ã‚  Ã‚  end;   Ã‚  Ã‚  Ã‚  thumbnail.Canvas.StretchDraw(thumbRect, thumbnail) ; //resize image   Ã‚  Ã‚  Ã‚  thumbnail.Width : thumbRect.Right;   Ã‚  Ã‚  Ã‚  thumbnail.Height : thumbRect.Bottom;   Ã‚  Ã‚  Ã‚  //display in a TImage control   Ã‚  Ã‚  Ã‚  Image1.Picture.Assign(thumbnail) ;   Ã‚  finally   Ã‚  Ã‚  Ã‚  thumbnail.Free;   Ã‚  end; end; Note: The GetFormImage only copies the form client area - if you need to take the entire screen shot of a form (including its border) youll need a different approach ...more about it next time.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.