// try this one
unit Unit4;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure FromBigToSmall(strSourceBitmapFile:string; smallWidth,smallHeight:integer);
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
FromBigToSmall('testprint.bmp',300,300);
end;
procedure TForm1.FromBigToSmall(strSourceBitmapFile:string; smallWidth,smallHeight:integer);
var
srcBitmap, destBitmap:TBitmap;
nHorz,nVert : integer;
i,j : integer;
rectDraw : TRect;
begin
// 读取原始位图到srcBitmap, 建立小的位图对象
srcBitmap := TBitmap.Create;
srcBitmap.LoadFromFile(strSourceBitmapFile);
destBitmap := TBitmap.Create;
destBitmap.Width := smallWidth;
destBitmap.Height := smallHeight;
with rectDraw do
begin
left := 0;
top := 0;
right := smallWidth;
bottom := smallheight;
end;
// 计算横向和纵向分割的块数
nHorz := (srcBitmap.Width-1) div smallWidth;
nVert := (srcBitmap.Height-1) div smallHeight;
// 将原始bitmap分割的小块重新绘制在destBitmap,并加以保存
try
for i:= 0 to nHorz do
for j:= 0 to nVert do
begin
destBitmap.Canvas.FillRect(rectDraw);
destBitmap.Canvas.Draw(i*(-1)*smallWidth,j*(-1)*smallHeight,srcBitmap);
destBitmap.SaveToFile('temp'+inttostr(i*(nVert+1)+j)+'.bmp');
end;
finally
srcBitmap.Free;
destBitmap.Free;
end;
end;
end.