Skip to content

Replace SpDPIResizeBitmap with a higher quality one. #122

@pyscripter

Description

@pyscripter

The following code does a much better job at scaling bitmaps than SpDPIResizeBitmap.

procedure ResizeBitmap(Bitmap: TBitmap; const NewWidth, NewHeight: Integer);
var
  Factory: IWICImagingFactory;
  Scaler: IWICBitmapScaler;
  Source : TWICImage;
  IBitmap: IWICBitmap;
begin
  Source := TWICImage.Create;
  try
    Factory := TWICImage.ImagingFactory;
    Factory.CreateBitmapFromHBITMAP(Bitmap.Handle, Bitmap.Palette,
      WICBitmapUsePremultipliedAlpha, IBitmap);
    Factory.CreateBitmapScaler(Scaler);
    try
      Scaler.Initialize(IBitmap, NewWidth, NewHeight,
        WICBitmapInterpolationModeHighQualityCubic);
      Source.Handle := IWICBitmap(Scaler);
    finally
      Scaler := nil;
      Factory := nil;
    end;
    Bitmap.Assign(Source);
  finally
    Source.Free;
  end;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions