Delphi png、bmp、gif等圖片格式轉換成jpg

 

 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 單元
相關文章
相關標籤/搜索