主窗体
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ComCtrls, ShellCtrls, ImgList, commctrl, jpeg,imagefilelist,filectrl,Unit_readImage;
type
TForm1 = class(TForm)
ScrollBox1: TScrollBox;
ShellTreeView1: TShellTreeView;
Splitter1: TSplitter;
Panel1: TPanel;
Splitter2: TSplitter;
Panel2: TPanel;
Image1: TImage;
ImageList1: TImageList;
ListView1: TListView;
StatusBar1: TStatusBar;
procedure FormCreate(Sender: TObject);
procedure ShellTreeView1Change(Sender: TObject;
Node: TTreeNode);
procedure FormDestroy(Sender: TObject);
private
PreviewBmp,originalbmp,Thumbbmp:Tbitmap;
PreviewJpg,Thumbjpg:Tjpegimage;
IsRefreshImageFinshi:boolean;
{ Private declarations }
public
progressbar_one:Tprogressbar;
procedure ShellTreeView1Change2(dir:string);
procedure RefreshImage;
{ Public declarations}
end;
const
RaisedPanel=1;
LoweredPanel=2;
var
Form1: TForm1;
imagefilelist:Timagefilelist;
readImage:TreadImage;
implementation
{$R *.dfm}
//在canvas上画一个Panel
procedure DrawPanel(canvas:TCanvas;Left,Top,Width,Height:integer;PanelType:integer);
var
Right,Bottom:integer;
LeftTopColor,RightBottomColor:TColor;
begin
//凸起的panel
if PanelType=RaisedPanel then
begin
LeftTopColor:=clwhite;
RightBottomColor:=clgray;
end
else
//凹下去的panel
begin
LeftTopColor:=clgray;
RightBottomColor:=clwhite;
end;
Right:=Left+width;
Bottom:=Top+Height;
Canvas.Pen.Width:=1;
Canvas.Pen.Color:=LeftTopColor;
Canvas.MoveTo(Right,Top);
Canvas.lineTo(Left,Top);
Canvas.LineTo(Left,bottom);
Canvas.Pen.Color:=RightBottomColor;
Canvas.lineTo(Right,Bottom);
Canvas.lineTo(Right,Top);
end;
//转换jpg到bmp
procedure JpgToBmp(const JpgFileName:string;AJpg:TJpegImage;Abmp:Tbitmap);
begin
try
AJpg.LoadFromFile(JpgFileName);
Abmp.Assign(AJpg);
finally
end;
end;
//仅从扩展名上来判断是否是jpg格式的文件
function IsJpgFile(const FileName:string):boolean;
begin
result:=(LowerCase( ExtractFileExt(FileName))='.jpg') or (LowerCase( ExtractFileExt(FileName))='.jpeg');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
listview_seticonspacing(listview1.Handle,90,120);
//设置缩略图的间距
originalbmp:=Tbitmap.Create;
Thumbjpg:=Tjpegimage.Create;
previewbmp:=Tbitmap.Create;
previewjpg:=Tjpegimage.Create;
Thumbbmp:=Tbitmap.Create;
THumbbmp.Height:=80;
Thumbbmp.Width:=80;
Thumbbmp.PixelFormat:=pf24bit;
imagelist1.Height:=80;
imagelist1.Width:=80;
listview1.LargeImages:=imagelist1;
listview1.ViewStyle:=vsicon;
imagefilelist:=Timagefilelist.Create;
imagefilelist.Clear;
IsRefreshImageFinshi:=true;
progressbar_one:=Tprogressbar.Create(self);
progressbar_one.Parent:=statusbar1;
progressbar_one.Width:=200;
progressbar_one.Height:=statusbar1.Height-4;
progressbar_one.Left:=statusbar1.Width-progressbar_one.Width;
progressbar_one.Top:=2;
end;
procedure TForm1.ShellTreeView1Change(Sender: TObject;
Node: TTreeNode);
begin
readImage:=Treadimage.Create(false);
end;
procedure TForm1.ShellTreeView1Change2(dir:string);
begin
if not IsRefreshImageFinshi then
exit;
if not (directoryexists(dir)) then
exit;
if dir[length(dir)]='/' then
delete(dir,length(dir),1);
imagefilelist.Clear;
imagefilelist.ChangeDir(dir);
statusbar1.Panels[0].Text:='当前文件夹共有图片:'+inttostr(imagefilelist.GetFileCount)+'张';
screen.Cursor:=crHourGlass;
self.Enabled:=false;
RefreshImage;
self.Enabled:=true;
screen.Cursor:=crdefault;
end;
procedure TForm1.RefreshImage;
var
i:integer;
ImageFileName:string;
ThumbBmpLeft:integer;
ThumbBmpTop:integer;
ThumbBmpHeight:integer;
ThumbBmpWidth:integer;
IsErrorImage:boolean;
begin
screen.Cursor:=crHourGlass;
ProgressBar_one.Max:=ImageFileList.GetFileCount;
ProgressBar_one.Visible:=true;
listview1.Clear;
imagelist1.Clear;
try
listview1.Items.begin
Update;
for i:=0 to ImageFileList.GetFileCount-1do
begin
ImageFileList.SetIndex(i);
ImageFileName:=ImageFileList.GetCurFullFileName;
if IsJpgFile(ImageFileName) then
try
jpgtobmp(ImageFileList.GetCurFullFileName,ThumbJpg,OriginalBmp);
if OriginalBmp.Height>=OriginalBmp.Width then
begin
ThumbBmpWidth:=64*OriginalBmp.Width div OriginalBmp.Height;
ThumbBmpLeft:=(64-ThumbBmpWidth ) div 2;
ThumbBmp.Canvas.Brush.Color :=clBtnFace;
ThumbBmp.Canvas.FillRect(ThumbBmp.Canvas.ClipRect);
DrawPanel(ThumbBmp.Canvas,0,0,79,79,RaisedPanel);
DrawPanel(ThumbBmp.Canvas,7+ThumbBmpLeft,7,ThumbBmpWidth+1,64,LoweredPanel);
ThumbBmp.Canvas.StretchDraw(Rect(8+ThumbBmpLeft,8,8+ThumbBmpLeft+ThumbBmpWidth,71),OriginalBmp);
imagelist1.Add(ThumbBmp,nil);
end
else
begin
ThumbBmpHeight:=64*OriginalBmp.Height div OriginalBmp.Width;
ThumbBmpTop:=(64-ThumbBmpHeight ) div 2;
ThumbBmp.Canvas.Brush.Color :=clBtnFace;
ThumbBmp.Canvas.FillRect(ThumbBmp.Canvas.ClipRect);
DrawPanel(ThumbBmp.Canvas,0,0,79,79,RaisedPanel);
DrawPanel(ThumbBmp.Canvas,7,7+ThumbBmpTop,64,ThumbBmpHeight+1,LoweredPanel);
ThumbBmp.Canvas.StretchDraw(Rect(8,8+ThumbBmpTop,71,8+ThumbBmpTop+ThumbBmpHeight),OriginalBmp);
imagelist1.Add(ThumbBmp,nil);
end;
with ListView1.Items.Add do
begin
ImageIndex:=imagelist1.Count-1;
caption:=ImageFileList.GetCurFileName;
end;
except
MessageDlg('读取文件:'+ImageFileList.GetCurFullFileName+'时发生错误!'+#13
+'该文件不是标准格式的文件,或者该文件已经被损坏!',mtError,[mbYes],0);
IsErrorImage:=true;
end
else
try
OriginalBmp.LoadFromFile(ImageFileList.GetCurFullFileName);
if OriginalBmp.Height>=OriginalBmp.Width then
begin
ThumbBmpWidth:=64*OriginalBmp.Width div OriginalBmp.Height;
ThumbBmpLeft:=(64-ThumbBmpWidth ) div 2;
ThumbBmp.Canvas.Brush.Color :=clBtnFace;
ThumbBmp.Canvas.FillRect(ThumbBmp.Canvas.ClipRect);
DrawPanel(ThumbBmp.Canvas,0,0,79,79,RaisedPanel);
DrawPanel(ThumbBmp.Canvas,7+ThumbBmpLeft,7,ThumbBmpWidth+1,64,LoweredPanel);
ThumbBmp.Canvas.StretchDraw(Rect(8+ThumbBmpLeft,8,8+ThumbBmpLeft+ThumbBmpWidth,71),OriginalBmp);
imagelist1.Add(ThumbBmp,nil);
end
else
begin
ThumbBmpHeight:=64*OriginalBmp.Height div OriginalBmp.Width;
ThumbBmpTop:=(64-ThumbBmpHeight ) div 2;
ThumbBmp.Canvas.Brush.Color :=clBtnFace;
ThumbBmp.Canvas.FillRect(ThumbBmp.Canvas.ClipRect);
DrawPanel(ThumbBmp.Canvas,0,0,79,79,RaisedPanel);
DrawPanel(ThumbBmp.Canvas,7,7+ThumbBmpTop,64,ThumbBmpHeight+1,LoweredPanel);
ThumbBmp.Canvas.StretchDraw(Rect(8,8+ThumbBmpTop,71,8+ThumbBmpTop+ThumbBmpHeight),OriginalBmp);
imagelist1.Add(ThumbBmp,nil);
end;
with ListView1.Items.Add do
begin
ImageIndex:=imagelist1.Count-1;
caption:=ImageFileList.GetCurFileName;
end;
except
MessageDlg('读取文件:'+ImageFileList.GetCurFullFileName+'时发生错误!'+#13
+'该文件不是标准格式的文件,或者该文件已经被损坏!',mtError,[mbYes],0);
IsErrorImage:=true;
end;
ProgressBar_one.Position:=i;
application.ProcessMessages;
end;
finally
listview1.Items.EndUpdate;
ProgressBar_one.Visible:=false;
end;
screen.Cursor:= crDefault;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
PreviewBmp.Free;
originalbmp.free;
Thumbbmp.Free ;
PreviewJpg.Free;
Thumbjpg.free;
imagefilelist.Free;
end;
end.