一个关于Listview 显示缩略图的问题 见鬼了 (50分)

  • 主题发起人 主题发起人 travis
  • 开始时间 开始时间
T

travis

Unregistered / Unconfirmed
GUEST, unregistred user!
好久不用delphi了。弱智了 大家不要笑。 hehe...

先看看代码

unit Unit1;

interface

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

type
TForm1 = class(TForm)
ListView1: TListView;
Button1: TButton;
ImageList1: TImageList;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
FileList: TStringList;

implementation

{$R *.dfm}
procedure ChangeDir(dir: string);
var
SearchRec : TSearchRec;
Attr : integer;
Found : integer;
ExtFileName:string;
temstr:string;
begin
FileList.Clear;
temstr:=dir+'/*.*';
Attr := faAnyFile;
Found := FindFirst(temstr, Attr, SearchRec);
while Found = 0 do
begin
ExtFileName:=LowerCase(ExtractFileExt(SearchRec.Name));
if (ExtFileName='.bmp') or (ExtFileName='.jpg') or ((ExtFileName='.jpeg')) then
FileList.Add(dir+'/'+SearchRec.Name) ;
Found := FindNext(SearchRec);
end;
FindClose(SearchRec);
end;

procedure JpgToBmp(const JpgFileName:string;AJpg:TJpegImage;Abmp:Tbitmap);
begin
try
AJpg.LoadFromFile(JpgFileName);
Abmp.Assign(AJpg);
finally
end;
end;

procedure DrawPanel(canvas:TCanvas; Left,Top,Width,Height: integer; PanelType:integer);
var
Right,Bottom:integer;
LeftTopColor,RightBottomColor:TColor;
begin
if PanelType=1 then begin
LeftTopColor:=clwhite;
RightBottomColor:=clgray;
end
else 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;

procedure AddThumbFromFile(FullFileName: string; ImageList: TImageList ) ;
var
OriginalBmp,ThumbBmp:TBitmap;
OrignalJpg : TJpegImage ;
ThumbBmpHeight,
ThumbBmpTop,
ThumbBmpWidth,
ThumbBmpLeft: integer;
begin
OrignalJpg := TJpegImage.Create ;
OriginalBmp:= TBitmap.Create ;
ThumbBmp:= Tbitmap.Create;

JpgToBmp(FullFileName,OrignalJpg,OriginalBmp);

if OriginalBmp.Height>=OriginalBmp.Width then
begin
ThumbBmpWidth:= 120*OriginalBmp.Width div OriginalBmp.Height;
ThumbBmpLeft:= (120-ThumbBmpWidth) div 2 -1;

ThumbBmp.Canvas.Brush.Color :=clBtnFace;
ThumbBmp.Canvas.FillRect(ThumbBmp.Canvas.ClipRect);

DrawPanel(ThumbBmp.Canvas,0,0,150-1,150-1,1);
DrawPanel(ThumbBmp.Canvas,9,9,120-1,120-1,2);
ThumbBmp.Canvas.StretchDraw(Rect(ThumbBmpLeft+9,9,ThumbBmpLeft+ThumbBmpWidth+9,119),
OriginalBmp);
ImageList.Add(ThumbBmp,nil); //这里出错 invaide image szie 什么回事?
end;

OrignalJpg.Free;
OriginalBmp.Free;
ThumbBmp.Free;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
I : integer ;
begin
FileList := TStringList.Create ;
ImageList1.Height := 150 ;
ImageList1.Width := 150 ;
ChangeDir('c:/1/');
ListView1.Items.BeginUpdate;
for I:=0 to FileList.Count-1 do begin
AddThumbFromFile(FileList,ImageList1);
with ListView1.Items.Add do begin
ImageIndex:=imagelist1.Count-1;
caption:=ExtractFileName(FileList);
end;
end;
ListView1.Items.EndUpdate;
FileList.Free ;
end;

end.
 
有什么问题?
 
这是个实验的东西,一个form 一个listview 一个 button
c:/1 下有两个 jpg 文件

读这两个文件 显示缩略图在 listview里面

现在运行到 imagelist.add(ThumdBmp,nil); 的时候报错 invaide image size
你可以试一下啊 谢谢了 真见鬼。
 
ThumbBmp:= Tbitmap.Create;
ThumbBmp.Width:= ImageList.Width;
ThumbBmp.Heigth:= ImageList.Heigth;
....
 
完了 忘记了.....吐血 呵呵 兄弟 谢谢你啊
 
后退
顶部