unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses
  GDIPAPI,GDIPOBJ;
procedure TForm1.Button1Click(Sender: TObject);
var
  bit:TBitmap;
  r:TRect;
begin
  bit:=TBitmap.Create;
  bit.LoadFromFile('d:\bmp\1.bmp');
  r:=Bounds(0,0,bit.Width,bit.Height);
  Self.Canvas.CopyRect(Self.ClientRect,bit.Canvas,r);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  bit:TBitmap;
  r:TRect;
begin
  bit:=TBitmap.Create;
  bit.LoadFromFile('d:\bmp\1.bmp');
  Self.Canvas.StretchDraw(Self.ClientRect,bit);
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  g:TGPGraphics;
  bit:TGPBitmap;
begin
  g:=TGPGraphics.Create(Self.Canvas.Handle);
  bit:=TGPBitmap.Create('d:\bmp\1.bmp');
  g.DrawImage(bit,0,0,Self.ClientWidth,Self.ClientHeight);
  bit.Free;
  g.Free;
end;

end.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, StdCtrls,FileCtrl;

type
  TForm1 = class(TForm)
    DrawGrid1: TDrawGrid;
    Button1: TButton;
    procedure DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
var
  list:TStringList;
  bit: array of TBitmap;
procedure TForm1.Button1Click(Sender: TObject);
var
  sr: TSearchRec;
  Dir: string;
  TP:string;
begin
  TP:= '.bmp.BMP'; //这里还可以加入其它图片后缀以过滤图片
  Dir := '';
  //选择目录对话框,需要在Uses中加入对FileCtrl单元的引用声明
  if SelectDirectory('选择图片目录', '', Dir) then
  begin
    List.Clear;
    //搜索该目录下的所有文件,如果是BMP文件就加入到列表
    if FindFirst(Dir + '\*.*', faReadOnly, sr) = 0 then
    begin
      if Pos(ExtractFileExt(sr.Name),TP) > 0 then
      begin
        List.Add(Dir + '\' + Sr.Name);
      end;
      while FindNext(sr) = 0 do
      begin
        if Pos(ExtractFileExt(sr.Name),TP) > 0 then
        begin
          List.Add(Dir + '\' + Sr.Name);
        end;
      end;
      FindClose(sr);
    end;
  end;
  DrawGrid1.ColCount := list.Count;
end;

procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  i: Integer;
begin
  SetLength(bit,list.Count);
  ChDir('d:\');
  for i := Low(bit) to High(bit) do
  begin
    bit[i] := TBitmap.Create;
    bit[i].LoadFromFile(list.Strings[i]);
  end;
  if (ARow = 0) and (ACol >= Low(bit)) and (ACol <= High(bit)) then
  begin
    TDrawGrid(Sender).Canvas.StretchDraw(Rect,bit[ACol]);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  list:=TStringList.Create;
  DrawGrid1.FixedRows := 0;
  DrawGrid1.FixedCols := 0;
  DrawGrid1.RowCount := 1;
  DrawGrid1.ColCount := 1;
  DrawGrid1.DefaultRowHeight := 200;  //设置网格的高
  DrawGrid1.DefaultColWidth := 200; //设置网格的宽
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  i:Integer;
begin
  list.Free;
  if Length(bit) > 0 then
  begin
    for i := Low(bit) to High(bit) do
    begin
      bit[i].Free;
    end;
  end;
end;

end.
posted on 2011-02-09 18:42  巅枫  阅读(446)  评论(0编辑  收藏  举报