Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Lazarus Pascal - Better image scaling for TImage

1 Posts
1 Users
0 Reactions
267 Views
 Hans
(@hans)
Famed Member Admin
Joined: 13 years ago
Posts: 3060
Topic starter  

Specifically with Windows, I noticed how poorly a TImage scales.
With a little trick you can redraw the image using a different type of interpolation like so:

procedure BetterScaleImage(AWidth, AHeight: Integer; sourceImage:Timage);
var
  srcImg, destImg: TLazIntfImage;
  cnv: TLazCanvas;
  interp: TFPBaseInterpolation;
begin
  if sourceImage.Picture.Bitmap = nil then Exit;

  srcImg := sourceImage.Picture.Bitmap.CreateIntfImage;
  destImg := TLazIntfImage.CreateCompatible(srcImg, AWidth, AHeight);

  try
    cnv := TLazCanvas.Create(destImg);
    try
      interp := TFPBaseInterpolation.Create;
      try
        cnv.Interpolation := interp;
        cnv.StretchDraw(0, 0, AWidth, AHeight, srcImg);

        sourceImage.Picture.Bitmap.LoadFromIntfImage(destImg);

      finally
        interp.Free;
      end;
    finally
      cnv.Free;
    end;
  finally
    destImg.Free;
    srcImg.Free;
  end;
end;                        

After loading an image in your TImage, simply call BetterScaleImage(width,height,TImage) to scale it better.
Under Windows this will be a world of difference. 

Enjoy!



   
ReplyQuote
Share: