大功告成!我相信这就是你想要的:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Jpeg, ExtDlgs;
type
TForm1 = class(TForm)
Button1: TButton;
Image1: TImage;
OpenPictureDialog1: TOpenPictureDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
DrawRect: TRect;
FPicture: TPicture;
begin
if OpenPictureDialog1.Execute then
begin
FPicture := TPicture.Create;
FPicture.LoadFromFile(OpenPictureDialog1.FileName);
with Image1 do
begin
Canvas.Brush.Color := Color;
DrawRect := ClientRect;
Canvas.FillRect(DrawRect);
if FPicture.Width > 0 then
begin
with DrawRect do
if (FPicture.Width > Right - Left) or (FPicture.Height > Bottom - Top) then
begin
if FPicture.Width > FPicture.Height then
Bottom := Top + MulDiv(FPicture.Height, Right - Left, FPicture.Width)
else
Right := Left + MulDiv(FPicture.Width, Bottom - Top, FPicture.Height);
Canvas.StretchDraw(DrawRect, FPicture.Graphic);
end
else
with DrawRect do
Canvas.Draw(Left + (Right - Left - FPicture.Width) div 2, Top + (Bottom - Top -
FPicture.Height) div 2, FPicture.Graphic);
end;
end;
FPicture.Free;
end;
end;
end.