unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls,jpeg;
type
TForm1 = class(TForm)
btn3: TButton;
img1: TImage;
procedure btn3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
//png,bmp等图片转jpg 函数
function ConvertPICintoJPG(cPic: TPicture; pWidth: Integer = 0; pHeight: Integer = 0): TJpegImage; stdcall;
implementation
{$R *.dfm}
function ConvertPICintoJPG(cPic: TPicture; pWidth: Integer = 0; pHeight: Integer = 0): TJpegImage; stdcall;
var
tBMP: TBitmap;
begin
Result := TJpegImage.Create;
if (pWidth > 0) or (pHeight > 0) then
begin
try
tBMP := TBitmap.Create; //创建一个过渡性BMP图片,用于更改图片尺寸
if pWidth <= 0 then pWidth := cPic.Width; //若pWidth为有效值则改变tBMP宽度,否则不变
if pHeight <= 0 then pHeight := cPic.Height; //若pHeight为有效值则改变tBMP高度,否则不变
tBMP.Width := pWidth;
tBMP.Height := pHeight;
tBMP.Canvas.StretchDraw(tBMP.Canvas.ClipRect, cPic.Graphic); //按照新尺寸重画图形
Result.Assign(tBMP);
finally
tBMP.Free;
end;
end
else Result.Assign(cPic);
end;
procedure TForm1.btn3Click(Sender: TObject);
var
myjpg: TJPEGImage;
myimg: TImage;
begin
myimg := TImage.Create(self);
myimg.Picture.LoadFromFile('D:\pro\plt_17313.png'); //加载图片
myjpg := ConvertPICintoJPG(myimg.Picture, myimg.Picture.Width, myimg.Picture.Height); //调用转换函数
myjpg.SaveToFile(ExtractFilePath(ParamStr(0)) + 'tmp.jpg'); //保存图片
end;
end.
使用jpg图片 需要引用 jpeg 单元