我用了下面一段代码,可是发生错误,不知什么原因,救命啊……
procedure TAddEquipFrm.AddPicPMClick(Sender: TObject);
var
SourceFile : String ;
Picture:TPicture;
BlobStream:TBlobStream;
MemStream:TMemoryStream;
Header:TGraphicHeader;
begin
if AddPicOpenDlg.Execute then
SourceFile := Trim(AddPicOpenDlg.FileName)
else Exit;
try
Picture:=TPicture.Create;
try
Picture.LoadFromFile(SourceFile);
if Picture.Graphic is TBitmap then
begin
DBImage1.Picture.LoadFromFile(SourceFile);
end else if Picture.Graphic is TJPEGImage then
//JPG图片.
begin
MemStream:=TMemoryStream.Create;
try
TJPEGImage(Picture.Graphic).SaveToStream(MemStream);
//存入临时流.
Header.Count:=1;
Header.HType:=$0101;
//JPG数据的Header.HType:=$0101当然也可以定为其他不等于$0100的值.
Header.Size:=MemStream.Size;
BlobStream:=TBlobStream.Create(TBlobField(StrDMod.EquipTbl.FieldByName('Picture')),bmWrite);
//创建Blob流.
try
MemStream.Seek(0,0);
BlobStream.Write(Header,sizeof(Header));
//先写入流头.
BlobStream.CopyFrom(MemStream,MemStream.Size);
//写入图片数据.
finally
BlobStream.Free; //////好象错误发生在这句
StrDMod.EquipTbl.Post;
end;
finally
MemStream.Free;
end;
end;
finally
Picture.Free;
end;
//////////////////////////////////////////////
Except
MessageBox(Application.Handle,'上载照片没有成功!','错误',MB_OK+MB_ICONERROR);
Exit;
end;
end;
错误提示为:---------------------------
Debugger Exception Notification
---------------------------
Project TraDev.exe raised exception class EInvalidGraphic with message 'Bitmap image is not valid'. Process stopped. Use Step or Run to continue.
---------------------------
OK Help
---------------------------