1 unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, ExtCtrls, StdCtrls,jpeg;
8
9 type
10 TForm1 = class(TForm)
11 btn3: TButton;
12 img1: TImage;
13 procedure btn3Click(Sender: TObject);
14 private
15 { Private declarations }
16 public
17 { Public declarations }
18
19
20 end;
21
22 var
23 Form1: TForm1;
24
25 //png,bmp等图片转jpg 函数
26 function ConvertPICintoJPG(cPic: TPicture; pWidth: Integer = 0; pHeight: Integer = 0): TJpegImage; stdcall;
27
28 implementation
29
30 {$R *.dfm}
31 function ConvertPICintoJPG(cPic: TPicture; pWidth: Integer = 0; pHeight: Integer = 0): TJpegImage; stdcall;
32 var
33 tBMP: TBitmap;
34 begin
35 Result := TJpegImage.Create;
36
37 if (pWidth > 0) or (pHeight > 0) then
38 begin
39 try
40 tBMP := TBitmap.Create; //创建一个过渡性BMP图片,用于更改图片尺寸
41 if pWidth <= 0 then pWidth := cPic.Width; //若pWidth为有效值则改变tBMP宽度,否则不变
42 if pHeight <= 0 then pHeight := cPic.Height; //若pHeight为有效值则改变tBMP高度,否则不变
43 tBMP.Width := pWidth;
44 tBMP.Height := pHeight;
45 tBMP.Canvas.StretchDraw(tBMP.Canvas.ClipRect, cPic.Graphic); //按照新尺寸重画图形
46 Result.Assign(tBMP);
47 finally
48 tBMP.Free;
49 end;
50 end
51 else Result.Assign(cPic);
52 end;
53
54
55 procedure TForm1.btn3Click(Sender: TObject);
56 var
57 myjpg: TJPEGImage;
58 myimg: TImage;
59 begin
60 myimg := TImage.Create(self);
61 myimg.Picture.LoadFromFile(\'D:\pro\plt_17313.png\'); //加载图片
62 myjpg := ConvertPICintoJPG(myimg.Picture, myimg.Picture.Width, myimg.Picture.Height); //调用转换函数
63 myjpg.SaveToFile(ExtractFilePath(ParamStr(0)) + \'tmp.jpg\'); //保存图片
64 end;
65
66 end.
使用jpg图片 需要引用 jpeg 单元