你非常非常想要使用Graphics32。
procedure DrawSrcToDst(Src, Dst: TBitmap32);
var
R: TKernelResampler;
begin
R := TKernelResampler.Create(Src);
R.Kernel := TLanczosKernel.Create;
Dst.Draw(Dst.BoundsRect, Src.BoundsRect, Src);
end;
在对图像进行重采样时,有多种方法和滤波器可供选择。上面的示例使用了一种基于卷积核的重采样方法(有点慢,但效果很好),以及Lanczos滤波器作为重构核心。上面的示例应该适用于您的需求。
如果您恢复使用Win32 API调用,可以使用SetStretchBltMode函数并将其设置为HALFTONE,然后使用StretchBlt函数。我不确定默认的Delphi调用是否提供此功能,但这是我通常解决此问题的方法。
更新(2014年9月)刚才我也遇到了类似的情况,并且在TScrollBox中有一个TImage,同时窗体上还有很多其他内容,我希望Image1.Stretch:=true;
可以半色调化。正如Rob指出的那样,当目标画布为每像素8位或更低并且源画布具有更高位深度时,TBitmap.Draw
仅使用HALFTONE...因此,我使用其中之一“修复”它:Image1.Picture.Bitmap
。
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;
您可以尝试使用GraphUtil中内置的Delphi ScaleImage功能
我使用GDIPOB.pas中的TGPGraphics类。
如果Canvas是TGPGraphics,Bounds是TGPRectF和NewImage是TGPImage实例:
Canvas.SetInterpolationMode(InterpolationModeHighQualityBicubic);
Canvas.SetSmoothingMode(SmoothingModeHighQuality);
Canvas.DrawImage(NewImage, Bounds, 0, 0, NewImage.GetWidth, NewImage.GetHeight, UnitPixel);
InterpolationModeDefault = QualityModeDefault;
InterpolationModeLowQuality = QualityModeLow;
InterpolationModeHighQuality = QualityModeHigh;
InterpolationModeBilinear = 3;
InterpolationModeBicubic = 4;
InterpolationModeNearestNeighbor = 5;
InterpolationModeHighQualityBilinear = 6;
InterpolationModeHighQualityBicubic = 7;
平滑模式:
SmoothingModeDefault = QualityModeDefault;
SmoothingModeHighSpeed = QualityModeLow;
SmoothingModeHighQuality = QualityModeHigh;
SmoothingModeNone = 3;
SmoothingModeAntiAlias = 4;
注意:这需要XP或更高版本,或者在安装程序中打包gdiplus.dll。