How to resize a picture?

后端 未结 6 1690
鱼传尺愫
鱼传尺愫 2020-12-30 04:30

I have image (500x500) but I need to resize it to 200x200 and paint it on TImage. How to achieve such result?

Note
I know about Stretch

6条回答
  •  情书的邮戳
    2020-12-30 04:44

    Please see this simple example on how to resize an image using two TBitmap32 objects. The TBitmap32 is the best in terms of speed/image quality ratio.

    It requires the https://github.com/graphics32 library.

    uses 
      GR32, GR32_Resamplers;
    
    procedure Resize(InputPicture: TBitmap; OutputImage: TImage; const DstWidth, DstHeigth: Integer);
    var
      Src, Dst: TBitmap32;
    begin
      Dst := nil;
      try
        Src := TBitmap32.Create;
        try
          Src.Assign(InputPicture);
          SetHighQualityStretchFilter(Src);
          Dst := TBitmap32.Create;
          Dst.SetSize(DstWidth, DstHeigth);
          Src.DrawTo(Dst, Rect(0, 0, DstWidth, DstHeigth), Rect(0, 0, Src.Width, Src.Height));
        finally
          FreeAndNil(Src);
        end;
        OutputImage.Assign(Dst);
      finally
        FreeAndNil(Dst);
      end;
    end;
    
    // If you need to set a highest quality resampler, use this helper routine to configure it
    procedure SetHighQualityStretchFilter(B: TBitmap32);
    var
      KR: TKernelResampler;
    begin
      if not (B.Resampler is TKernelResampler) then
      begin
        KR := TKernelResampler.Create(B);
        KR.Kernel := TLanczosKernel.Create;
      end
      else
      begin
        KR := B.Resampler as TKernelResampler;
        if not (KR.Kernel is TLanczosKernel) then
        begin
          KR.Kernel.Free;
          KR.Kernel := TLanczosKernel.Create;
        end;
      end;
    end;
    

提交回复
热议问题