How to Strech an image correctly lazarus / 31-LCL ---------------------------------------------------------------------- Weitentaaal · 2023-07-12 12:01 Hello, i have problems loading a png File into an image. I use this Code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TVGraph.LoadPNG(Path: string);var   BitMap: TBitmap;begin   fPath:= Path;   if FileExists(Path) then begin      BitMap:= TBitmap.Create;    VKurve.Picture.LoadFromFile(Path);       Bitmap.SetSize(VKurve.Width, VKurve.Height);      Bitmap.Canvas.StretchDraw(Rect(0,0,VKurve.Width, VKurve.Height), VKurve.Picture.Bitmap);       VKurve.Picture.Bitmap.Assign(BitMap);    BitMap.Free;   end else begin      CustomMessage('', 'No File found !', cmError); //Failed to load File, File not found   end;end;  the Picture will load into the image and is streched correctly but it looks awful, see attached Example. how do i have to change the Code for the image to look good ? thanks in advance. ---------------------------------------------------------------------- KodeZwerg · 2023-07-12 12:50 You can do... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses ...BGRABitmap, BGRABitmapTypes... function Resample(const ASource: TBGRABitmap; const AWidth, AHeight: Integer): TBGRABitmap;begin  ASource.ResampleFilter := rfLanczos2;  Result := ASource.Resample(AWidth, AHeight);end; procedure TForm1.Button1Click(Sender: TObject);var  bmp1, bmp2: TBGRABitmap;begin  if FileExists('.\HowItShouldLook.png') then    begin      bmp1 := TBGRABitmap.Create;      bmp2 := TBGRABitmap.Create;      try        bmp1.LoadFromFile('.\HowItShouldLook.png');        bmp2 := Resample(bmp1, Image1.Width, Image1.Height);        Image1.Proportional := True;        Image1.Stretch := True;        Image1.Picture.Assign(bmp2);      finally        bmp2.Free;        bmp1.Free;      end;    end;end;At least the final content does look better that way. ---------------------------------------------------------------------- Weitentaaal · 2023-07-12 12:58 that looks a lot better thank you  !! ---------------------------------------------------------------------- wp · 2023-07-12 12:59 The TCanvas.StretchDraw has a very crispy output which not necessarily corresponds with high quality. You get much better quality if you apply the StretchDraw method of the TFPCustomCanvas; (like in BGRABitmap) you can select among several interpolation algorithms. There is a wiki article about what to do: https://wiki.freepascal.org/Developing_with_Graphics#Using_the_non- native_StretchDraw_from_LazCanvas, and since Laz 2.2+ this procedure has been included in a modified version in unit GraphUtil. I copied the "AntiAliasedStretchDraw" into the attached project in which you can scale the input image to any size and compare with the output of TCanvas.StretchDraw. ---------------------------------------------------------------------- Weitentaaal · 2023-07-12 13:39 Both solutions do work very well for me, can't realy see any Difference in quality. thank you both ! ---------------------------------------------------------------------- cdbc · 2023-07-12 18:03 Hi @KodeZwerg: I think your code is leaking a bitmap:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses ...BGRABitmap, BGRABitmapTypes... function Resample(const ASource: TBGRABitmap; const AWidth, AHeight: Integer): TBGRABitmap;begin  ASource.ResampleFilter := rfLanczos2;  //1) the line below creates a new bitmap from resampling in Result...  Result := ASource.Resample(AWidth, AHeight);end; procedure TForm1.Button1Click(Sender: TObject);var  bmp1, bmp2: TBGRABitmap;begin  if FileExists('.\HowItShouldLook.png') then    begin      bmp1 := TBGRABitmap.Create;      //2) bmp2 should not be created here, it will be overwritten...// leak      bmp2 := TBGRABitmap.Create;      try        bmp1.LoadFromFile('.\HowItShouldLook.png');        //3) in the next line by the result from function         bmp2 := Resample(bmp1, Image1.Width, Image1.Height);        Image1.Proportional := True;      Image1.Stretch := True;        Image1.Picture.Assign(bmp2);      finally        bmp2.Free;        bmp1.Free;      end;    end;end; Just skip the line "leak", namely: bmp2 := TBGRABitmap.Create; Regards Benny ---------------------------------------------------------------------- KodeZwerg · 2023-07-12 19:08 Quote from: cdbc on July 12, 2023, 08:03:26 pm@KodeZwerg: I think your code is leaking a bitmap:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i [/quote]Ahhh by watching BGRA source you are right, I hate such when methods not named "Create" are "creating" things.(CreateResample would be a better choice of name) ---------------------------------------------------------------------- jamie · 2023-07-12 20:48 https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi- setstretchbltmode ---------------------------------------------------------------------- KodeZwerg · 2023-07-12 22:07 Quote from: jamie on July 12, 2023, 10:48:04 pmhttps://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf- wingdi-setstretchbltmode Here's an example that includes 3 methods, BGRA, wp's and jamie's suggestion as pascal code. My eyes are not the best, to me all looking "okay" and somehow "same"  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Windows,  BGRABitmap, BGRABitmapTypes,  fpCanvas, IntfGraphics, LazCanvas,  Classes , SysUtils , Forms , Controls , Graphics , Dialogs , ExtCtrls ,  StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Image1: TImage;    Image2: TImage;    Image3: TImage;    procedure Button1Click(Sender: TObject);    procedure FormResize(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} procedure CreateRescaleBitmap(const Source: TBitmap; const Width, Height: Integer; out Rescaled: TBitmap);var  DC: HDC;  MemDC: HDC;  Bitmap: HBITMAP;  OldBitmap: HBITMAP;begin  DC := GetDC(0);  try    MemDC := CreateCompatibleDC(DC);    try      Bitmap := CreateCompatibleBitmap(DC, Width, Height);      OldBitmap := SelectObject(MemDC, Bitmap);       SetStretchBltMode(MemDC, HALFTONE);      SetBrushOrgEx(MemDC, 0, 0, nil);       StretchBlt(MemDC, 0, 0, Width, Height, Source.Canvas.Handle, 0, 0, Source.Width, Source.Height, SRCCOPY);       Rescaled := TBitmap.Create;      Rescaled.Assign(Source);      Rescaled.Width := Width;      Rescaled.Height := Height;      BitBlt(Rescaled.Canvas.Handle, 0, 0, Width, Height, MemDC, 0, 0, SRCCOPY);       SelectObject(MemDC, OldBitmap);      DeleteObject(Bitmap);    finally      DeleteDC(MemDC);    end;  finally    ReleaseDC(0, DC);  end;end; procedure AntiAliasedStretchDrawBitmap(SourceBitmap, DestBitmap: TCustomBitmap);var  DestIntfImage, SourceIntfImage: TLazIntfImage;  DestWidth, DestHeight: Integer;  DestCanvas: TLazCanvas;begin  DestWidth := DestBitmap.Width;  DestHeight := DestBitmap.Height;  DestIntfImage := TLazIntfImage.Create(0, 0);  try    DestIntfImage.LoadFromBitmap(DestBitmap.Handle, DestBitmap.MaskHandle);    DestCanvas := TLazCanvas.Create(DestIntfImage);    try      SourceIntfImage := SourceBitmap.CreateIntfImage;      try        DestCanvas.Interpolation := TFPBaseInterpolation.Create;        try          DestCanvas.StretchDraw(0, 0, DestWidth, DestHeight, SourceIntfImage);          DestBitmap.LoadFromIntfImage(DestIntfImage);        finally        DestCanvas.Interpolation.Free;        end;      finally        SourceIntfImage.Free;      end;    finally      DestCanvas.Free;    end;  finally    DestIntfImage.Free;  end;end;  function Resample(const ASource: TBGRABitmap; const AWidth, AHeight: Integer): TBGRABitmap;begin  ASource.ResampleFilter := rfBestQuality;  Result := ASource.Resample(AWidth, AHeight);end; { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  bgr1, bgr2: TBGRABitmap;  png1, png2: TPortableNetworkGraphic;  bmp1, bmp2: TBitmap;begin  if FileExists('.\HowItShouldLook.png') then    begin      // Image1 @ GDI+      png1 := TPortableNetworkGraphic.Create;      bmp1 := TBitmap.Create;      try        png1.LoadFromFile('.\HowItShouldLook.png');        bmp1.Assign(png1);        CreateRescaleBitmap(bmp1, Image1.Width, Image1.Height, bmp2);      Image1.Proportional := True;        Image1.Stretch := True;      Image1.Picture.Assign(bmp2);      finally        bmp2.Free;        bmp1.Free;        png1.Free;      end;       // Image2 @ BGRA      bgr1 := TBGRABitmap.Create;      bgr2 := TBGRABitmap.Create;      try        bgr1.LoadFromFile('.\HowItShouldLook.png');        bgr2 := Resample(bgr1, Image2.Width, Image2.Height);        Image2.Proportional := True;        Image2.Stretch := True;        Image2.Picture.Assign(bgr2);      finally        bgr2.Free;        bgr1.Free;      end;       // Image3 @ Canvas      png1 := TPortableNetworkGraphic.Create;      bmp1 := TBitmap.Create;      bmp2 := TBitmap.Create;      try        png1.LoadFromFile('.\HowItShouldLook.png');        bmp1.Assign(png1);        bmp2.SetSize(Image3.Width, Image3.Height);        AntiAliasedStretchDrawBitmap(bmp1, bmp2);        Image3.Proportional := True;        Image3.Stretch := True;        Image3.Picture.Assign(bmp2);      finally        bmp2.Free;        bmp1.Free;        png1.Free;      end; (*      // classical not pretty windows method      png1 := TPortableNetworkGraphic.Create;      png2 := TPortableNetworkGraphic.Create;      try        png1.LoadFromFile('.\HowItShouldLook.png');        png2.SetSize(Image1.Width, Image1.Height);        StretchBlt(png2.Canvas.Handle, //destination HDC          0, 0, Image1.Width, Image1.Height, // destination size          png1.Canvas.Handle, //source HDC          0, 0, png1.Width, png1.Height, // source size          SrcCopy);        Image1.Picture.Assign(png2);      finally        png2.Free;        png1.Free;      end;*)    end;end; procedure TForm1.FormResize(Sender: TObject);begin  Image1.Width := Form1.ClientWidth div 3;  Image2.Width := Form1.ClientWidth div 3;end; end.