分享
 
 
 

转换一批.bmp 文件为 .jpg

王朝delphi·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

转换一批.bmp 文件为 .jpg

unit BMP2JPG_Unit;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls ,jpeg, ComCtrls, filectrl, Menus;

type

TForm1 = class(TForm)

SourceB: TButton;

Source: TLabel;

Target: TLabel;

targetB: TButton;

ConvertB: TButton;

CQ: TTrackBar;

CQL: TLabel;

ListBox: TListBox;

BRB: TButton;

NOW: TCheckBox;

Button1: TButton;

USD: TCheckBox;

StatusBar: TStatusBar;

Label1: TLabel;

Label2: TLabel;

PopupMenu1: TPopupMenu;

Addfiles1: TMenuItem;

Remove1: TMenuItem;

Convertthis1: TMenuItem;

Batchrun1: TMenuItem;

Removeall1: TMenuItem;

procedure SourceBClick(Sender: TObject);

procedure targetBClick(Sender: TObject);

procedure ConvertBClick(Sender: TObject);

procedure CQChange(Sender: TObject);

procedure BRBClick(Sender: TObject);

procedure FormCreate(Sender: TObject);

procedure Button1Click(Sender: TObject);

procedure Addfiles1Click(Sender: TObject);

procedure Batchrun1Click(Sender: TObject);

procedure Convertthis1Click(Sender: TObject);

procedure Remove1Click(Sender: TObject);

procedure ListBoxClick(Sender: TObject);

procedure Removeall1Click(Sender: TObject);

private

{ Private declarations }

outputdir:string;

total:word;

public

{ Public declarations }

procedure bmp2jpg(FromBMP,ToJPG:string;Quality:byte);

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.SourceBClick(Sender: TObject);

var op:topendialog; count:integer;

begin

op:=topendialog.Create(nil);

op.Options:=[ofAllowMultiSelect,ofReadOnly,ofPathMustExist,ofFileMustExist];

op.Filter:='*.bmp|*.bmp';

op.Execute;

if op.Files.Count>0 then

begin

listbox.Items.AddStrings(op.Files);

source.Caption:=listbox.Items[0];

total:=listbox.Items.Count;

statusbar.Panels[0].Text:='Total file '+inttostr(total);

end;

op.Free;

end;

procedure TForm1.targetBClick(Sender: TObject);

var op:tsavedialog;

begin

op:=tsavedialog.Create(nil);

op.Options:=[ofReadOnly,ofPathMustExist];

op.DefaultExt:='jpg';

op.Filter:='*.jpg|*.jpg';

op.Execute;

target.Caption:=op.FileName;

op.Free;

end;

procedure TForm1.ConvertBClick(Sender: TObject);

var s:string;

begin

if now.Checked and fileexists(target.Caption) then exit;

statusbar.Panels[0].Text:='Converting...';

statusbar.Panels[1].Text:='Current file :'+extractfilename(source.Caption);

sourceb.Enabled:=false;

targetb.Enabled:=false;

cq.Enabled:=false;

convertb.Enabled:=false;

if not directoryexists(target.Caption) then

begin

s:=source.Caption;

target.Caption:=extractfilepath(s);

s:=extractfilename(s);

s:=copy(s,1,pos('.',s));

target.Caption:=target.Caption+s+'jpg';

end;

bmp2jpg(source.Caption,target.Caption,cq.Position);

sourceb.Enabled:=true;

targetb.Enabled:=true;

cq.Enabled:=true;

convertb.Enabled:=true;

statusbar.Panels[0].Text:='Ready';

statusbar.Panels[1].Text:='';

end;

procedure TForm1.CQChange(Sender: TObject);

begin

cql.Caption:='Compress Qualify '+ inttostr(cq.Position);

end;

procedure TForm1.BRBClick(Sender: TObject);

var count:integer; s:string;

begin

if listbox.Items.Count=0 then exit;

SourceB.Enabled:=false;

TargetB.Enabled:=false;

ConvertB.Enabled:=false;

if BRB.Caption='Cancel' then

begin

BRB.Caption:='Batch Run';

SourceB.Enabled:=true;

TargetB.Enabled:=true;

ConvertB.Enabled:=true;

total:=listbox.Items.Count;

statusbar.Panels[1].Text:='Total file '+inttostr(total);

statusbar.Panels[0].Text:='Total file '+inttostr(total);

end else

BRB.Caption:='Cancel';

for count:=0 to listbox.Items.Count-1 do

begin

S:=listbox.Items[0];

Source.Caption:=s;

if (usd.Checked) or (outputdir='')

then target.Caption:=extractfilepath(s)

else begin

if not directoryexists(outputdir) then exit;

if length(outputdir)=3

then target.Caption:=outputdir

else target.Caption:=outputdir+'\';

end;

s:=extractfilename(s);

s:=copy(s,1,pos('.',s));

target.Caption:=target.Caption+s+'jpg';

if now.Checked and fileexists(target.Caption) then continue;

Application.ProcessMessages;

if BRB.Caption='Batch Run' then exit;

statusbar.Panels[0].Text:='Converting...'+' ('+inttostr(count+1)+'/'+inttostr(total)+')';

statusbar.Panels[1].Text:='Current file :'+extractfilename(source.Caption);

bmp2jpg(source.Caption,target.Caption,cq.Position);

listbox.Items.Delete(0);

end;

SourceB.Enabled:=true;

TargetB.Enabled:=true;

ConvertB.Enabled:=true;

BRB.Caption:='Batch Run';

statusbar.Panels[0].Text:='Ready';

statusbar.Panels[1].Text:='';

end;

procedure TForm1.bmp2jpg(FromBMP, ToJPG: string;Quality:byte);

var jpg:tjpegimage;bmp:tbitmap;

begin

if not fileexists(FromBMP) then exit;

try

bmp:=tbitmap.Create;

bmp.LoadFromFile(FromBMP);

jpg:=tjpegimage.Create;

jpg.Assign(bmp);

jpg.CompressionQuality:=Quality;

jpg.Compress;

jpg.SaveToFile(ToJPG);

finally

bmp.Free;

jpg.Free;

end;

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

cql.Caption:='Compress Qualify '+ inttostr(cq.Position);

outputdir:='';

statusbar.Panels[0].Text:='Ready';

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

selectdirectory('Select a output directory','',outputdir);

target.caption:=outputdir;

if directoryexists(outputdir)

then usd.Checked:=true

else usd.Checked:=false;

end;

procedure TForm1.Addfiles1Click(Sender: TObject);

begin

SourceBClick(nil);

end;

procedure TForm1.Batchrun1Click(Sender: TObject);

begin

BRBClick(nil);

end;

procedure TForm1.Convertthis1Click(Sender: TObject);

begin

ConvertBClick(nil);

end;

procedure TForm1.Remove1Click(Sender: TObject);

begin

Listbox.DeleteSelected;

statusbar.Panels[0].Text:='Total file '+inttostr(total);

end;

procedure TForm1.ListBoxClick(Sender: TObject);

begin

if listbox.SelCount>0 then

begin

Source.Caption:=ListBox.Items[Listbox.ItemIndex];

Statusbar.Panels[1].Text:='Current select file '+Extractfilename(Source.Caption);

end;

end;

procedure TForm1.Removeall1Click(Sender: TObject);

begin

listbox.Clear;

statusbar.Panels[0].Text:='Ready';

end;

end.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有