将bmp文件转换为jpg文件

时间:2022-09-02 16:32:56

procedure TForm1.Button1Click(Sender: TObject);
(*压缩MBP为JPEG;但是没有提供压缩比可选项
凑合用吧,大概1/3 ^_^:
Note:必须加上JPEG到Uses单元
*)
var
MyJPEG : TJPEGImage;
MyBMP : TBitmap;
begin
MyBMP := TBitmap.Create;
with MyBMP do
try
LoadFromFile('e:\lm.BMP'); //你的图片位置
MyJPEG := TJPEGImage.Create;
with MyJPEG do begin
Assign(MyBMP);
CompressionQuality:=10; //压缩比例
Compress;
SaveToFile('e:\lm01.JPEG');//保存路径……
Free;
end;
finally
Free;
end;
end;


By Luiz Vaz - luiz.vaz@arinso.com.br

function Bmp2Jpg(Bmp: TBitmap; Quality: Integer = 100): TJpegImage;

begin

Result := nil;

if Assigned(Bmp)

then begin

Result := TJpegImage.Create;

Result.Assign(Bmp); {Its all folks...}

Result.CompressionQuality := Quality;

Result.JPEGNeeded; {Key method...}

Result.Compress;

end;

end;

function Jpg2Bmp(Jpg: TJpegImage): TBitmap;

begin

Result := nil;

if Assigned(Jpg)

then begin

Result := TBitmap.Create;

Jpg.DIBNeeded; {Key method...}

Result.Assign(Jpg); {Its all folks...}

end;

end;

注意:JPEGNeeded过程转换并存储图像数据到内部的JPEG数据。我们无法直接存取JPEG 内部数据,因为D5并没有提供jpeg.pas单元的源码。但是可以通过JPEGNeeded和DIBNeeded过程来操作它。