看看这段代码
procedure TForm1.TiltBitmap(const InBitmap, OutBitmap: TBitmap;
const WidthTop, WidthBottom: integer);
const
clBackColor = clBlack;
BestQuality = True;
var
y, xWidthDiff, xWidthCurrentLine: Integer;
d: Real;
begin
OutBitmap.PixelFormat := InBitmap.PixelFormat;
if WidthTop > WidthBottom then
OutBitmap.Width := WidthTop
else
OutBitmap.Width := WidthBottom;
OutBitmap.Height := InBitmap.Height;
OutBitmap.Canvas.Brush.Color := clblack;
OutBitmap.Canvas.FillRect(OutBitmap.Canvas.ClipRect);
OutBitmap.Canvas.CopyMode := cmSrcCopy;
if BestQuality then
begin
{slower but better quality with color images}
SetStretchBltMode(OutBitmap.Canvas.Handle, HALFTONE);
SetBrushOrgEx(OutBitmap.Canvas.Handle, 0, 0, nil);
end
else
{quicker but slightly lower quality}
SetStretchBltMode(OutBitmap.Canvas.Handle, HALFTONE);
OutBitmap.Canvas.CopyMode := cmSrcCopy;
d := (WidthBottom - WidthTop) / OutBitmap.Height;
for y := 0 to OutBitmap.Height - 1 do
begin
xWidthCurrentLine := Trunc(WidthTop + d * y);
xWidthDiff := (OutBitmap.Width - xWidthCurrentLine) div 2;
OutBitmap.Canvas.CopyRect(Rect(xWidthDiff, y, xWidthDiff +
xWidthCurrentLine, y + 1),
InBitmap.Canvas, Rect(0, y, InBitmap.Width, y + 1));
end;
end;
var
InBitmap, outbitmap: Tbitmap;
begin
if Image1.Picture.Bitmap.Empty then
begin
ShowMessage('请加载图片');
exit;
end
else
InBitmap := TBitmap.Create;
outbitmap := TBitmap.Create;
InBitmap.Assign(image1.Picture.Bitmap);
TiltBitmap(InBitmap, OutBitmap, 300, 600);
image1.Picture.Bitmap.Assign(outbitmap);
image1.Invalidate;
InBitmap.Free;
outbitmap.Free;
end;