Scale an image nicely in Delphi?

后端 未结 4 400
独厮守ぢ
独厮守ぢ 2020-12-05 03:42

I\'m using Delphi 2009 and I\'d like to scale an image to fit the available space. the image is always displayed smaller than the original. the problem is TImage Stretch p

4条回答
  •  余生分开走
    2020-12-05 03:49

    If you revert to using Win32 API calls, you can use SetStretchBltMode to HALFTONE and use StretchBlt. I'm not sure if this is provided using default Delphi calls, but that's the way I generally solve this issue.

    Update (2014-09) Just now I was in a similar situation (again) and had a TImage in a TScrollBox with lots more going on on the form, and really wanted Image1.Stretch:=true; to do halftone. As Rob points out, TBitmap.Draw uses HALFTONE only when the destination canvas is 8 bits-per-pixel or lower and the source canvas has more... So I 'fixed' it with assigning Image1.Picture.Bitmap to one of these instead:

    TBitmapForceHalftone=class(TBitmap)
    protected
      procedure Draw(ACanvas: TCanvas; const Rect: TRect); override;
    end;
    
    { TBitmapForceHalftone }
    
    procedure TBitmapForceHalftone.Draw(ACanvas: TCanvas; const Rect: TRect);
    var
      p:TPoint;
      dc:HDC;
    begin
      //not calling inherited; here!
      dc:=ACanvas.Handle;
      GetBrushOrgEx(dc,p);
      SetStretchBltMode(dc,HALFTONE);
      SetBrushOrgEx(dc,p.x,p.y,@p);
      StretchBlt(dc,
        Rect.Left,Rect.Top,
        Rect.Right-Rect.Left,Rect.Bottom-Rect.Top,
        Canvas.Handle,0,0,Width,Height,ACanvas.CopyMode);
    end;
    

提交回复
热议问题