在工作中有用到缩略图展示,这个时候就需要对图片进行伸缩处理,也有时候会遇到需要裁剪部分区域,比如正中间的部分,通过在网上查阅资料,发现通过Windows的两个API就可以很方便的搞定。
先贴下代码:
procedure FitBitmap(const Source,Dest:string;const x,y:integer;const ColorBit:TPixelFormat);
var
abmp,bbmp:tbitmap;
scalex,scaley:real;
begin
abmp:=tbitmap.Create;
bbmp:=tbitmap.Create;
try
abmp.LoadFromFile(Source);
scaley := abmp.Height / y;
scalex := abmp.Width / x;
bbmp.Width := round(abmp.Width / scalex);
bbmp.Height := round(abmp.Height / scaley);
bbmp.PixelFormat := pf8bit;
SetStretchBltMode(bbmp.Canvas.Handle, COLORONCOLOR);
stretchblt(bbmp.Canvas.Handle, 0, 0, bbmp.Width, bbmp.Height, abmp.Canvas.Handle, 0, 0, abmp.Width, abmp.Height, srccopy);
bbmp.SaveToFile(Dest);
finally
abmp.Free;
bbmp.Free;
end;
end;
从上面的代码可以看到,主要用到两个API:
S