Search code examples
delphicanvasbitmapdelphi-xe4

Stretching a Bitmap in Delphi using StretchDraw method


I have a bitmap image of dimensions(width = 250, height = 200). I am stretching it with following code snippet, but the quality is deteriorated. Is there a way to retain the quality of bitmap image while stretching it?

with Bitmap do
begin
  Width := 200;
  Height := 150;
  PixelFormat := pf1bit;    
  TransparentMode := tmAuto;
  Canvas.CopyMode := cmSrcCopy;
  Canvas.FillRect(Rect(0, 0, Width, Height));
  Canvas.StretchDraw(Rect(0, 0, Width, Height), MyBitMap);
end; 

Here, MyBitMap is of type TBitMap in which I have loaded the original bitmap image.

AND, Is this the right way of doing this?


Solution

  • Here is a very nice procedure called SmoothResize(abmp:TBitmap; NuWidth,NuHeight:integer);. It is able to resize a TBitmap width great quality. Don't forget to add the two type declarations.

    The StretchDraw method is very fast and is a part of canvas. The quality is as you described not much brilliant. If you want to use StretchDraw change PixelFormat to pf24bit and delete the FillRect-line.

    hope this helps :)