Delphi png、bmp、gifなどのピクチャフォーマットをjpgに変換

1582 ワード

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