Recent

Author Topic: How to resize Timage (jpeg) to fit in a cell drawgrid  (Read 3982 times)

WimVan

  • Jr. Member
  • **
  • Posts: 85
How to resize Timage (jpeg) to fit in a cell drawgrid
« on: December 19, 2018, 02:18:51 pm »
Hi,
I have next code:

Code: Pascal  [Select][+][-]
  1. procedure TMASTERform.fotogridDrawCell(Sender: TObject; aCol, aRow: Integer;
  2.   aRect: TRect; aState: TGridDrawState);
  3. var
  4.   image : TImage;
  5.   foto : string;
  6.   ntop, nleft, nfirst : integer;
  7.   lfotook : boolean;
  8.   cellWidth, cellHeight: LongInt;
  9.   cvs: TCanvas;
  10.   Rect: TRect;
  11. begin
  12.   if RASTERQuery.Active then
  13.   begin
  14.     if Sender is TDrawGrid
  15.     then cvs := TDrawGrid(Sender).Canvas
  16.     else cvs := fotogrid.Canvas;
  17.     Rect := aRect;
  18.     lfotook := true;
  19.     if (aCol = 0) and (aRow = 0)
  20.     then RASTERQuery.First
  21.     else
  22.     begin
  23.       if not RasterQuery.EOF
  24.       then RasterQuery.Next
  25.       else lfotook := false;
  26.     end;
  27.   end
  28.   else lfotook := false;
  29.   if lfotook then
  30.   begin
  31.     if RASTERQuery.FieldByName('ty').AsString = 'D'
  32.     then foto := DIR_SLIDE+RASTERQuery.FieldByName('foto_dir').AsString+'\'+RASTERQuery.FieldByName('foto').AsString
  33.     else foto := 'avi-mjpg-flv.jpg';
  34.  
  35.     cellWidth := aRect.Right-aRect.Left;
  36.     cellHeight := aRect.Bottom-aRect.Top;
  37.  
  38.     image := TImage.Create(Self);
  39.     try
  40.       image.Picture.LoadFromFile(foto);
  41.       image.Width        := cellWidth - 10;
  42.       image.Height       := cellHeight - 10;
  43.       image.AutoSize     := false;
  44.       image.Proportional := true;
  45.       image.Stretch      := false;
  46.       image.Cursor       := crHandPoint;
  47.       ntop  := Rect.Top + 5;
  48.       nleft := Rect.Left + 5;
  49.       cvs.Draw(nleft, nTop, image.Picture.Graphic);
  50.     finally
  51.       image.Free;
  52.     end;
  53.   end;
  54. //  else fotogrid.DefaultDrawCell(aCol, aRow, aRect, aState);
  55.  
  56. end;    


This codes works as it should, except, the images are NOT scaled at all.
1) I want to scale all images to a defined value. Decreasing size, increasing size.
2) that image should be shown in a cell of the drawgrid.
3) Ratio between vertical and widht must be respected.

The code is functional and works except, no resizing at all !
doe somebody sees why not ?

When this is working I want to add a small panel, label beneath the image so I can put a title in ?  Some body an idea for this ?

Thanks
« Last Edit: December 19, 2018, 02:20:25 pm by WimVan »

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: How to resize Timage (jpeg) to fit in a cell drawgrid
« Reply #1 on: December 19, 2018, 03:46:22 pm »
The images are not scaled because you paint the image.Picture.Graphic onto the canvas - this is the unscaled image, no matter whether you change the properties Stretch, Proportional etc or not.

I don't know whether a TImage works at all if it does not have a parent. But I would not use a TImage component at all. A simpler TGraphic will do as well:

Here are the essential steps in the OnDrawCell event of the grid (untested, high risk of typos here and there...):

Load the image in a TPicture:
Code: Pascal  [Select][+][-]
  1. var
  2.   pic: TPicture;
  3.   R: TRect;
  4.   scaledHeight, scaledWidth: Integer;
  5. begin
  6. ...
  7.   pic := TPicture.Create;
  8.   try
  9.     pic.LoadFromFile(<filename from DB>);

Calculate  image bounds so that the image is centered and fits into the cell but keeps its aspect ratio (i.e. emulate what Image.Stretch, .Proportional and .Center do).
Code: Pascal  [Select][+][-]
  1.   if pic.Width/pic.Height > cellWidth/cellHeight then begin
  2.     // image is "wide" --> fit image width into cell
  3.     scaledHeight := round(pic.Width / pic.Height * cellHeight);
  4.     R.Left := aRect.Left;
  5.     R.Right := aRect.Right;
  6.     R.Top := (aRect.Top + aRect.Bottom - scaledHeight) div 2;
  7.     R.Bottom := R.Top + scaledHeight;
  8.   end else begin
  9.     // image is "high" --> fit image height into cell
  10.     scaledWidth := round(pic.Height / pic.Width * cellWidth);
  11.     R.Top := aRect.Top;
  12.     R.Bottom := aRect.Bottom;
  13.     R.Left := (aRect.Left + aRect.Right - scaledWidth) div 2;
  14.     R.Right := R.Left + scaledWidth;
  15.   end;

StretchDraw the image in Pic.Graphic into the rectangle R
Code: Pascal  [Select][+][-]
  1.   cvs.StretchDraw(R, pic.Graphic);

Don't forget to clean up
Code: Pascal  [Select][+][-]
  1.   finally
  2.     pic.Free;
  3.   end;

WimVan

  • Jr. Member
  • **
  • Posts: 85
Re: How to resize Timage (jpeg) to fit in a cell drawgrid
« Reply #2 on: December 20, 2018, 09:22:23 am »
@wp

Thanks, now I understand the component TImage.  It is just a kind of layer.
I changed Timage in my sources and used tPicture.  Saves a lot of memory, saves a lot of time in run ..
I had to change some little logic to cange the images as it must, but now it runs like a germ.
Pitty that the info about all graphics is not always clear.

 

TinyPortal © 2005-2018