Delphi png、bmp等图片格式转换成jpg

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 单元

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值