delphi Image 处理

procedure ResizeBmp(Src,Dst:String);
var SrcBM,DstBM:TBitMap;
Rect:TRect;
NewW,NewH,PicW,PicH:Integer;
HDivW:Double;
begin
SrcBM:=TBitMap.Create;
SrcBM.LoadFromFile(Src);
PicW:=SrcBM.Width;
PicH:=SrcBM.Height;
NewH:=768;
NewW:=1024;
DstBM:=TBitMap.Create;
with DstBM do begin
Width:=NewW;
Height:=NewH;
Rect.TopLeft:=Point(0,0);
Rect.BottomRight:=Point(NewW,NewH);
Canvas.Rectangle(0,0,Width,Height);
Canvas.StretchDraw(Rect,TGraphic(SrcBM));
SaveToFile(Dst);
FreeImage;
Free;
end;
end;

unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, System.Math;

type
TForm1 = class(TForm)
img1: TImage;
btn1: TButton;
btn2: TButton;
grp1: TGroupBox;
img2: TImage;
btn3: TButton;
btn4: TButton;
img3: TImage;
btn5: TButton;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
procedure btn5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
var
bmp: TBitmap;
oRect: TRect;
begin
bmp := TBitmap.Create;

bmp.LoadFromFile('D:12.bmp');
// img1.Picture.LoadFromFile('D:2.bmp');
// img1.Width := img1.Picture.Width;
// img1.Height := img1.Picture.Height;
// img1.Width := img1.Picture.Width + 50;
// img1.Height := img1.Picture.Height + 50;
// img1.ClientWidth := 200;
// img1.ClientHeight := 200;
oRect.Left := Round((img1.ClientRect.Width - bmp.Width)/2);
oRect.Top := Round((img1.ClientRect.Height - bmp.Height)/2);
oRect.Width := bmp.Width;
oRect.Height:= bmp.Height;
img1.Canvas.StretchDraw(oRect, bmp);
img1.Center := true;
end;

procedure TForm1.btn2Click(Sender: TObject);
begin
img1.Canvas.TextOut(0,0, 'haha');
end;

procedure TForm1.btn3Click(Sender: TObject);
var SrcBM,DstBM:TBitMap;
Rect:TRect;
NewW,NewH,PicW,PicH:Integer;
HDivW:Double;
scale_X, scale_Y: double;
zoom: double;
izoom: Integer;
begin
SrcBM:=TBitMap.Create;
SrcBM.LoadFromFile('D:12.bmp');
PicW:=SrcBM.Width;
PicH:=SrcBM.Height;
zoom := img2.Width/SrcBM.Width;
NewH:=Round(SrcBM.Height * zoom);
NewW:=Round(SrcBM.Width * zoom);
DstBM:=TBitMap.Create;
with DstBM do
begin
Width:=NewW;
Height:=NewH;
Rect.TopLeft:=Point(0,0);
Rect.BottomRight:=Point(NewW,NewH);
Canvas.Rectangle(0,0,Width,Height);
Canvas.StretchDraw(Rect,TGraphic(SrcBM));
img2.Picture.Bitmap.Assign(DstBM);
img2.Canvas.TextOut(0, 0, '111');
// FreeImage;
// Free;
end;
SrcBM.Free;
DstBM.Free;
end;

{
procedure TForm1.btn3Click(Sender: TObject);
var SrcBM,DstBM:TBitMap;
Rect:TRect;
NewW,NewH,PicW,PicH:Integer;
HDivW:Double;
begin
SrcBM:=TBitMap.Create;
SrcBM.LoadFromFile('D:12.bmp');
PicW:=SrcBM.Width;
PicH:=SrcBM.Height;
NewH:=100;
NewW:=80;
DstBM:=TBitMap.Create;
with DstBM do
begin
Width:=NewW;
Height:=NewH;
Rect.TopLeft:=Point(0,0);
Rect.BottomRight:=Point(NewW,NewH);
Canvas.Rectangle(0,0,Width,Height);
Canvas.StretchDraw(Rect,TGraphic(SrcBM));
img2.Picture.Bitmap.Assign(DstBM);
img2.Canvas.TextOut(0, 0, '111');
// FreeImage;
// Free;
end;
SrcBM.Free;
DstBM.Free;
end;
}

procedure TForm1.btn5Click(Sender: TObject);
begin
img3.Picture.LoadFromFile('D:12.bmp');

end;

end.

object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 810
ClientWidth = 1035
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object img1: TImage
Left = 32
Top = 24
Width = 105
Height = 105
end
object btn1: TButton
Left = 143
Top = 40
Width = 75
Height = 25
Caption = 'btn1'
TabOrder = 0
OnClick = btn1Click
end
object btn2: TButton
Left = 143
Top = 104
Width = 75
Height = 25
Caption = 'btn2'
TabOrder = 1
OnClick = btn2Click
end
object grp1: TGroupBox
Left = 256
Top = 24
Width = 633
Height = 721
Caption = 'grp1'
TabOrder = 2
object img2: TImage
Left = 80
Top = 48
Width = 105
Height = 105
end
object img3: TImage
Left = 72
Top = 232
Width = 289
Height = 273
end
object btn3: TButton
Left = 264
Top = 40
Width = 75
Height = 25
Caption = 'btn3'
TabOrder = 0
OnClick = btn3Click
end
object btn4: TButton
Left = 264
Top = 104
Width = 75
Height = 25
Caption = 'btn4'
TabOrder = 1
end
object btn5: TButton
Left = 416
Top = 264
Width = 75
Height = 25
Caption = 'btn5'
TabOrder = 2
OnClick = btn5Click
end
end
end

原文地址:https://www.cnblogs.com/zhangzhifeng/p/7810859.html