Link to home
Start Free TrialLog in
Avatar of yarek
yarekFlag for France

asked on

delphi good quality resizing BITMAP

I am looking for a good BITMAP resizing library (I need to resize BITMAP picture and then have to save it).
I found some poor quality results, and I am looking for something like Photoshop result (good quality interpolation).
Any clues ?
ASKER CERTIFIED SOLUTION
Avatar of MerijnB
MerijnB
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I snippet I made a while ago, as an example how to use GR32.
In this case Lanczos algorithm is used (the line where StretchTransfer() is called).
But you case use other algorithms aswell: type TStretchFilter = (sfNearest, sfLinear, sfSpline, sfLanczos, sfMitchell);

asaik Spline is used often by PhotoShop


procedure TMainForm.ResizeImage(Image: TBitmap; OutImage: TJPEGImage);
const OUT_WIDTH = 480;
      OUT_HEIGHT = 360;
      BACK_COLOR = clWhite;
var SourceBMP: TBitmap32;
    DestBMP: TBitmap32;
    StretchFactor: real;
    StretchRect: TRect;
    Temp: TBitmap;
begin
 SourceBMP := TBitmap32.Create();
 SourceBMP.Assign(Image);

 DestBMP := TBitmap32.Create();
 DestBMP.SetSize(OUT_WIDTH, OUT_HEIGHT);
 DestBMP.Canvas.Brush.Color := BACK_COLOR;
 DestBMP.Canvas.FillRect(DestBMP.BoundsRect);
 DestBMP.StretchFilter := sfLanczos;

 StretchRect.Left := 0;
 StretchRect.Top := 0;
 StretchRect.Right := Image.Width;
 StretchRect.Bottom := Image.Height;

 if (Image.Width > OUT_WIDTH) or (Image.Height > OUT_HEIGHT) then
 begin
  if (Image.Width div 4) > (Image.Height div 3) then
  begin
   StretchFactor := Image.Width / OUT_WIDTH;

   StretchRect.Right := OUT_WIDTH;
   StretchRect.Bottom := round(Image.Height / StretchFactor);
  end
  else
  begin
   StretchFactor := Image.Height / OUT_HEIGHT;

   StretchRect.Bottom := OUT_HEIGHT;
   StretchRect.Right := round(Image.Width / StretchFactor);
  end;
 end;

 OffsetRect(StretchRect, (OUT_WIDTH - StretchRect.Right) div 2, (OUT_HEIGHT - StretchRect.Bottom) div 2);

 StretchTransfer(DestBMP, StretchRect, DestBMP.BoundsRect, SourceBMP, SourceBMP.BoundsRect, sfLanczos, dmOpaque);

 Temp := TBitmap.Create();
 Temp.Width := DestBMP.Width;
 Temp.Height := DestBMP.Height;

 DestBMP.DrawTo(Temp.Canvas.Handle, 0, 0);

 OutImage.Assign(Temp);

 Temp.Free();      
 SourceBMP.Free();
 DestBMP.Free();
end;