在delphi里怎样把BMP图像格式转换成jpg格式???请高手指点……,在线急等……(10分)

  • 主题发起人 主题发起人 lcmlhs
  • 开始时间 开始时间
L

lcmlhs

Unregistered / Unconfirmed
GUEST, unregistred user!
如题:
在delphi里怎样把BMP图像格式转换成jpg格式??请高手指点……,在线急等……
请问代码怎么写????????????????????
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2608886
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ExtCtrls,jpeg, StdCtrls;

type
TForm1 = class(TForm)
Button2: TButton;
ScrollBox1: TScrollBox;
Image1: TImage;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
ScrollBox2: TScrollBox;
StaticText1: TStaticText;
Image2: TImage;
ComboBox1: TComboBox;
StaticText2: TStaticText;
Button3: TButton;
StaticText3: TStaticText;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure ComboBox1Enter(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
compqui:integer;
bmp:tbitmap;
jpg:tjpegimage;

implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
begin
bmp:=tbitmap.Create;
jpg:=tjpegimage.Create;
if opendialog1.Execute then
begin
image1.Picture.LoadFromFile(opendialog1.FileName);
bmp:=image1.Picture.Bitmap;
jpg.CompressionQuality:=100;
jpg.Compress;
jpg.Assign(bmp);
jpg.SaveToFile('c:/a.jpg');
image2.Picture.LoadFromFile('c:/a.jpg');

end;
end;
procedure TForm1.Button1Click(Sender: TObject);


begin
bmp:=tbitmap.Create;
jpg:=tjpegimage.Create;


bmp:=image1.Picture.Bitmap;
jpg.CompressionQuality:=compqui;
jpg.Compress;
jpg.Assign(bmp);





end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
compqui:=strtoint(combobox1.Text);
if compqui>100 then
begin
compqui:=100;
combobox1.Text:=inttostr(100);
end;
jpg.CompressionQuality:=compqui;
jpg.Compress;
jpg.Assign(bmp);

jpg.SaveToFile('c:/a.jpg');
image2.Picture.LoadFromFile('c:/a.jpg');




end;

procedure TForm1.Button3Click(Sender: TObject);
begin
if opendialog1.FileName='' then
exit;
showmessage('请加上完整的文件名,包括扩展名');
if savedialog1.Execute then
begin

jpg.SaveToFile(savedialog1.FileName);
deletefile('c:/a.jpg');

end;
end;

procedure TForm1.ComboBox1Enter(Sender: TObject);
begin
compqui:=strtoint(combobox1.Text);
if compqui>100 then
begin
compqui:=100;
combobox1.Text:=inttostr(100);
end;
end;

end.

///
刚学 delphi 时做的
 
user
jpeg


Tjpeg.Assign(TBitMap);
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Jpeg, ExtDlgs;

type
TfrmMain = class(TForm)
rbBMP2JPG: TRadioButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
rbJPG2BMP: TRadioButton;
btOpen: TButton;
btStart: TButton;
SavePictureDialog1: TSavePictureDialog;
procedure btOpenClick(Sender: TObject);
procedure btStartClick(Sender: TObject);
//function Bmp2Jpg(FileName:string; Quality:Integer):TJpegImage;
private
{ Private declarations }
public
{ Public declarations }
end;

var
frmMain: TfrmMain;
FileName:String;//存放源文件名
j2b:boolean;//是否是jpeg到bmp的转换

implementation

{$R *.dfm}

//bmp转换为jpeg
function Bmp2Jpg(FileName: string; Quality: Integer = 100): TJpegImage;
var
Bmp:Tbitmap;
begin
Bmp:=Tbitmap.Create;
Bmp.LoadFromFile(FileName); //创建Bmp并调入Bitmap图像文件
Result:=nil;
if Assigned(Bmp) then //如果Bmp不为nil
begin
Result:=TJpegImage.Create;
Result.Assign(Bmp); //将bmp指定给TJpegImage对象
Result.CompressionQuality:=Quality; //压缩比例
Result.JPEGNeeded; //转换
Result.Compress; //压缩
end;
Bmp.Free;
Bmp:=nil;
end;

//jpeg到bmp
function Jpg2Bmp(FileName:string):TBitMap;
var
Jpg:TJpegImage;
begin
Jpg:=TJpegImage.Create;
Jpg.LoadFromFile(FileName);
Result:=nil;
if Assigned(Jpg) then //如果Jpg不为空
begin
Result:=TBitMap.Create;
Result.Assign(Jpg); //将Jpg指定给TBitMap对象
Jpg.DIBNeeded; //转换
end;
Jpg.Free;
Jpg:=nil;
end;

procedure TfrmMain.btOpenClick(Sender: TObject);
begin

if rbbmp2jpg.Checked then
j2b:=false
else
j2b:=true;
//设置OpenDialog和SaveDialog的Filter属性
if not j2b then //如果是bmp到jpeg的转换
begin
OpenDialog1.Filter:='bmp files(*.bmp)|*.bmp';
SaveDialog1.Filter:='jpeg files(*.jpg)|*.jpg';
end
else
begin
OpenDialog1.Filter:='jpeg files(*.jpg)|*.jpg';
SaveDialog1.Filter:='bmp files(*.bmp)|*.bmp';
end;
//将取得的源文件名保存到全局变量FileName中
if OpenDialog1.Execute then
FileName:=OpenDialog1.FileName;
end;

procedure TfrmMain.btStartClick(Sender: TObject);
begin

if SavePictureDialog1.Execute then
begin
if j2b then
Jpg2Bmp(FileName).SaveToFile(SavePictureDialog1.FileName)
else
Bmp2Jpg(FileName).SaveToFile(SavePictureDialog1.FileName);
ShowMessage('转换成功!');
end;
{
if SaveDialog1.Execute then
begin
//转换,并将转换后的TBitMap或TJpegIamge对象写入相应的文件
if j2b then
Jpg2Bmp(FileName).SaveToFile(SaveDialog1.FileName)
else
Bmp2Jpg(FileName).SaveToFile(SaveDialog1.FileName);
ShowMessage('转换成功!');
end;
}
end;

end.
 
还不散分啊[:D]就10分。。。呵呵
 
在程序的uses中加入Jpeg单元,使用TImage中的Picture.LoadFromFile 方法装入JPEG格式图象文件。注意,不要使用Picture.Bitmap属性,Picture.Graphic属性却可以使用。TBitmap只能适合Bitmap格式的图象,而TGraphic是所有graphic objects的父类。
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls, ExtCtrls, Jpeg;
//显示JPEG图片
procedure TForm1.Button1Click(Sender: TObject);
Var
AppPath : String ;
begin
AppPath := ExtractFilePath(Application.Exename);
Image1.Picture.LoadFromFile(AppPath+'Img/Beautiful.jpg') ;
end;
 
寫那麼多?另存為︿
 
不好意思,大家都有分啊
 
老大你就不能下一个超级猛料或者DFW的离线?
这问题还要问?????
 
不好意思,我是新手,在哪儿下啊??请指点
 
后退
顶部