Delphi中怎样转存数据库中BLOB字段(其中存储JPEG图片)?(100分)

  • 主题发起人 主题发起人 hello8
  • 开始时间 开始时间
H

hello8

Unregistered / Unconfirmed
GUEST, unregistred user!
delphi中怎样处理oracle数据库中的BLOB字段,字段中存储的是JPEG的图片,

怎么把DBGrid中的BLOB字段的JPEG图片取出来,然后存放在指定的文件中?

我用BLOBStream读取后再用Jpeg.SaveToFile存为.JPG的图片,报错JPEG error #52

刚学Delphi的新手
 
procedure TForm1.DBSaveToClick(Sender: TObject);
var
Str : String;
Jpeg : TJPEGIMAGE;
bmp :TBitmap;
begin
if Adotable1.FieldByName('pic').AsString ='' then
exit
else begin
bmp:=TBitmap.Create;
bmp.Assign(DBImage1.Picture.Bitmap);
end;
if savePicturedialog1.Execute then
begin
Str := ExtractFileExt(savePictureDialog1.filename);
Str := Copy(Str,2,3);
//仅支持bmp和jpg格式
if Str = 'bmp' then bmp.SaveToFile(savePicturedialog1.FileName)
else if Str = 'jpg' then
begin
Jpeg := TJPEGIMAGE.Create;
Jpeg.Assign(bmp);
Jpeg.SaveToFile(savePictureDialog1.FileName);
end
else
ShowMessage('不支持其它格式!');
end;
end;
 
无论怎样,最关键的两句是:
TBlobField(DataSet.fieldByName('Picture')).SaveToFile(FileName);
TBlobField(DataSet.fieldByName('Picture')).LoadFromFile(FileName);


 
不是这样的,我的数据库中存储的就是JPEG的文件,不是BMP的
try
BLStream := TBLOBStream.Create(Table1.FieldByName('PIC') as TBLOBField,bmread );
try
JpegImage := TJpegImage.Create ;
JpegImage.LoadFromStream(BLStream);
finally
JpegImage.Free ;
end;
finally
BLStream.Free ;
end;
// Save to File;
JpegImage.SaveToFile(FullFileName);
JpegImage.Free ;
运行后报错
 
写入
procedure TfrmQuality.btnTestClick(Sender: TObject);
var
msMyFile: TMemoryStream;
MaxNo: string;
begin
msMyFile := TMemoryStream.Create;
if FileExists('f:/line.bmp') then
msMyFile.LoadFromFile('f:/line.bmp');
MaxNo := IntToStr(LoginInf.GetTableNextNo('mytable'));
with LoginInf.MySQLPlus do
begin
Close;
SQL.Clear;
SQL.Add('insert into mytable (no) values ('
+ MaxNo + ')');
ExecSQL;
LoginInf.CommitData;
Close;
end;

with LoginInf.MySQLPlus do
begin
Close;
SQL.Clear;
SQL.Add('select mytable.rowid, mytable.myfields from mytable '
+ 'where no = ' + MaxNo);
ShowMessage(SQL.Text);
Open;
Edit;
TBlobField(Fields[0]).LoadFromStream(msMyFile);
LoginInf.CommitData;
Post;
LoginInf.RollBackData;
end;
msMyFile.Free;
end;
 
谢谢lixijie老大,搞定
 
可以说详细些吗,我也遇到这样的问题,不过我用的是ACCESS
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
后退
顶部