在Delphi中很好地缩放图像? [英] Scale an image nicely in Delphi?

查看:218
本文介绍了在Delphi中很好地缩放图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Delphi 2009,我想缩放图像以适应可用空间。图像始终显示为小于原始图像。问题是TImage Stretch属性不会做得很好,并且损害了图片的可读性。

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 property doesn't do a nice job and harms the picture's readability.

丑陋的方式http://xrw.bc.ca/download/so/TImageStretch.gif

我想看到它变成如此:

更好的方式http ://xrw.bc.ca/download/so/NicerTImageStretch.png

任何建议如何最好地做到这一点?试过JVCL,但似乎没有这种能力。一个免费的图书馆会很好,但也许有一个低成本的图书馆,只有这也是好的。

Any suggestions how best to do this? Tried JVCL, but it doesn't seem to have this ability. A free library would be nice but maybe there's a low cost library that does "only" this would be good as well.

推荐答案

如果您恢复使用Win32 API调用,可以使用 SetStretchBltMode 到HALFTONE,然后使用 StretchBlt 。我不知道这是否使用默认的Delphi调用提供,但这是我通常解决这个问题的方式。

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.

更新(2014-09) strong>刚才我处于类似的情况(再次),在TScrollBox中有一个TImage,在表单上还有更多的事情,真的想要 Image1.Stretch:= true; 做半色调正如Rob指出的那样,当目标画布是每像素8位或更低,而源画布更多时, TBitmap.Draw 仅使用HALFTONE ...所以我用'code'将Image1.Picture.Bitmap 分配给其中之一:

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;

这篇关于在Delphi中很好地缩放图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆