Bookstore

Recent

Author Topic: Thumbnails  (Read 3014 times)

TRNelson

  • Jr. Member
  • **
  • Posts: 64
Thumbnails
« on: March 25, 2016, 07:31:50 pm »
I want to create a small function will generate a thumbnail from an image file.  It seems very straightforward.  The following code seems to work well for JPEG files.

Code: Pascal  [Select]
  1. var
  2.   MyPicture                            : TJpegImage;
  3.   Thumbnail                            : TJpegImage;
  4. begin
  5.   MyPicture := TJpegImage.Create;
  6.   Thumbnail := TJpegImage.Create;
  7.   try
  8.     MyPicture.LoadFromFile(some jpeg file);
  9.     ThumbNail.Width  := 100;
  10.     ThumbNail.Height := 100;
  11.     ThumbNail.Canvas.StretchDraw(Rect(0, 0, 100, 100), MyPicture);
  12.     Thumbnail.SaveToFile(thumbnail jpeg file);
  13.   finally
  14.     MyPicture.Free;
  15.     Thumbnail.Free;
  16.   end;
  17. end;

And that would pretty much be it.  However, I want to make it a little more general so that it can do its stuff with different graphic formats (eg: PNG, TIFF, BMP and other supported formats).  It was suggested in some online documentation that you could use TPicture.  The thumbnail would be a JPEG file.  A slight modification to the code would be required.
Code: Pascal  [Select]
  1. var
  2.   MyPicture                            : TPicture;
  3.   Thumbnail                            : TJpegImage;
  4. begin
  5.   MyPicture := TPicture.Create;
  6.   Thumbnail := TJpegImage.Create;
  7.   try
  8.     MyPicture.LoadFromFile(some jpeg file);
  9.     ThumbNail.Width  := 100;
  10.     ThumbNail.Height := 100;
  11.     ThumbNail.Canvas.StretchDraw(Rect(0, 0, 100, 100), MyPicture);
  12.     Thumbnail.SaveToFile(thumbnail jpeg file);
  13.   finally
  14.     MyPicture.Free;
  15.     Thumbnail.Free;
  16.   end;
  17. end;

The compiler complains about incompatible types for StretchDraw. 

Probably missing something very fundamental here but I can't see it.

Mando

  • Full Member
  • ***
  • Posts: 181
Re: Thumbnails
« Reply #1 on: March 25, 2016, 08:14:46 pm »
Hi:

Try this:

Code: Pascal  [Select]
  1.  ThumbNail.Canvas.StretchDraw(Rect(0, 0, 100, 100), MyPicture.graphic);
  2.  


However, your code obtains square images (100x100), regardless of  the aspect of the original image.


I had this code:

Code: Pascal  [Select]
  1. var
  2.   escX, escY: Extended;
  3.   img:TJPEGImage;
  4.   min:TJPEGImage;
  5.     r: TRect;
  6. begin
  7.    img:=TJPEGImage.Create; min:=TJPEGImage.Create;
  8.  
  9.   img.LoadFromFile((fichero));                  
  10.  
  11.   escX:=400/img.width; escY:=300/img.height; if escX>escY then escX:=escY;
  12.  
  13.   min.SetSize(round(img.width*escX),round(img.Height*escX));
  14.   r:=Rect(0,0,min.Width,min.height);
  15.   min.Canvas.AntialiasingMode:=amON;
  16.   min.Canvas.StretchDraw(r,img);
  17. ...
  18.  



regards.

TRNelson

  • Jr. Member
  • **
  • Posts: 64
Re: Thumbnails
« Reply #2 on: March 25, 2016, 08:51:40 pm »
Well there you go!    :)

Thanks for that.  Not sure how I missed that.

As far as the squareness of the thumbnails, I was just setting up an example.  The final program will maintain the aspect ratios.

Thanks again!