帮我查查错误,我把一个文件夹下的图片文件,有BMP与JPG ( 积分: 50 )

  • 主题发起人 主题发起人 xsgdhjt
  • 开始时间 开始时间
X

xsgdhjt

Unregistered / Unconfirmed
GUEST, unregistred user!
帮我查查错误,我把一个文件夹下的图片文件,有BMP与JPG两类文件,只把BMP文件导入数据库,导入后删除BMP文件,老是弹出错误框,“Projeect prjsavetoall.exe raised exception class EInvalidGraphic with message 'Bitmap image is not valid',process stopped,use step or run Contiune ”代码如下:
{
unit savetoall;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB;

type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
Edit1: TEdit;
Memo1: TMemo;
ADOConnection1: TADOConnection;
Table_history_data: TADOTable;
ADOQuery1: TADOQuery;
Table_history_datafilename: TStringField;
Table_history_datapic: TBlobField;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure GetAllFileName(SourcePath:String);
end;

var
Form1: TForm1;
OriginalBmp,ThumbBmp:Tbitmap;
implementation

{$R *.dfm}
procedure TForm1.GetAllFileName(SourcePath:String);
var
Found:Integer;
sr: TSearchRec;
begin
Found:=FindFirst(SourcePath+'/*.*',faAnyFile,sr);
while Found=0 do
begin //!!!
if (sr.Name='.') or (sr.Name='..') then //如果是父目录则推出进入下次循环
begin
Found := FindNext(sr);
continue;
end
else
begin
if not(sr.Attr and faDirectory > 0) then //非子目录
begin

try
if (LowerCase(ExtractFileExt(SourcePath+'/'+sr.Name)))='.bmp' then
begin
Memo1.Lines.Add(SourcePath+'/'+sr.Name); //将文件的路径写到Memo中,你可以写到数据库中
OriginalBmp:=Tbitmap.Create;
OriginalBmp.LoadFromFile(SourcePath+'/'+sr.Name);
Table_history_data.Open;
Table_history_data.Last;
Table_history_data.Append;
Table_history_data.FieldByName('pic').Assign(OriginalBmp);
Table_history_data.FieldValues['filename']:=SourcePath+'/'+sr.Name;
end
else
ListBox1.AddItem(SourcePath+'/'+sr.Name,ListBox1);
finally
OriginalBmp.Free;
DeleteFile(SourcePath+'/'+sr.Name);
end;
end
else //是子目录
GetAllFileName(SourcePath+'/'+sr.Name);
end;
Found:= FindNext(sr);
end;
FindClose(sr);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin

Edit1.text:='C:/lmck/bmp';
GetAllFileName(Edit1.text);
end;
}
 
帮我查查错误,我把一个文件夹下的图片文件,有BMP与JPG两类文件,只把BMP文件导入数据库,导入后删除BMP文件,老是弹出错误框,“Projeect prjsavetoall.exe raised exception class EInvalidGraphic with message 'Bitmap image is not valid',process stopped,use step or run Contiune ”代码如下:
{
unit savetoall;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB;

type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
Edit1: TEdit;
Memo1: TMemo;
ADOConnection1: TADOConnection;
Table_history_data: TADOTable;
ADOQuery1: TADOQuery;
Table_history_datafilename: TStringField;
Table_history_datapic: TBlobField;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure GetAllFileName(SourcePath:String);
end;

var
Form1: TForm1;
OriginalBmp,ThumbBmp:Tbitmap;
implementation

{$R *.dfm}
procedure TForm1.GetAllFileName(SourcePath:String);
var
Found:Integer;
sr: TSearchRec;
begin
Found:=FindFirst(SourcePath+'/*.*',faAnyFile,sr);
while Found=0 do
begin //!!!
if (sr.Name='.') or (sr.Name='..') then //如果是父目录则推出进入下次循环
begin
Found := FindNext(sr);
continue;
end
else
begin
if not(sr.Attr and faDirectory > 0) then //非子目录
begin

try
if (LowerCase(ExtractFileExt(SourcePath+'/'+sr.Name)))='.bmp' then
begin
Memo1.Lines.Add(SourcePath+'/'+sr.Name); //将文件的路径写到Memo中,你可以写到数据库中
OriginalBmp:=Tbitmap.Create;
OriginalBmp.LoadFromFile(SourcePath+'/'+sr.Name);
Table_history_data.Open;
Table_history_data.Last;
Table_history_data.Append;
Table_history_data.FieldByName('pic').Assign(OriginalBmp);
Table_history_data.FieldValues['filename']:=SourcePath+'/'+sr.Name;
end
else
ListBox1.AddItem(SourcePath+'/'+sr.Name,ListBox1);
finally
OriginalBmp.Free;
DeleteFile(SourcePath+'/'+sr.Name);
end;
end
else //是子目录
GetAllFileName(SourcePath+'/'+sr.Name);
end;
Found:= FindNext(sr);
end;
FindClose(sr);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin

Edit1.text:='C:/lmck/bmp';
GetAllFileName(Edit1.text);
end;
}
 
你为什么用 Found:=FindFirst(SourcePath+'/*.*',faAnyFile,sr);话呢?
这样FindFirst(SourcePath+'/*.bmp', faAnyFile, Sr);不可以吗?
至少可以提高速度呀!
 
try
OriginalBmp:=Tbitmap.Create;//应该在这里创建
if (LowerCase(ExtractFileExt(SourcePath+'/'+sr.Name)))='.bmp' then
begin
Memo1.Lines.Add(SourcePath+'/'+sr.Name); //将文件的路径写到Memo中,你可以写到数据库中
// OriginalBmp:=Tbitmap.Create;
OriginalBmp.LoadFromFile(SourcePath+'/'+sr.Name);
Table_history_data.Open;
Table_history_data.Last;
Table_history_data.Append;
Table_history_data.FieldByName('pic').Assign(OriginalBmp);
Table_history_data.FieldValues['filename']:=SourcePath+'/'+sr.Name;
end
else
ListBox1.AddItem(SourcePath+'/'+sr.Name,ListBox1);
finally
OriginalBmp.Free;
DeleteFile(SourcePath+'/'+sr.Name);
end;
 
bmp文件有好几种格式是否Tbitmap不支持你的bmp文件呢,先编个例程确认一下吧。另外你的create和free是否可以放到循环外?
 
出错是因为这一句
OriginalBmp.LoadFromFile(SourcePath+'/'+sr.Name);
如果这个文件扩展名是.bmp
但其实不是BMP文件就产生错误'Bitmap image is not valid'
 
太谢谢了,我也正要问呢?
 
后退
顶部