转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
转换一批.bmp 文件为 .jpg         ★★★★

转换一批.bmp 文件为 .jpg

作者:闵涛 文章来源:闵涛的学习笔记 点击数:783 更新时间:2009/4/23 18:42:48

 

转换一批.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.


[常用软件]谁是王者?微软WDP格式比拼标准JPG格式  [Delphi程序]用delphi批量导入某子目录下所有JPG图片文件到数据…
[Web开发]Microsoft Windows GDI+ JPG_Exp  
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台