读书人

怎么在sql server2000中存储image类型

发布时间: 2012-02-21 16:26:23 作者: rapoo

如何在sql server2000中存储image类型图片
数据库:sql server2000
字段:照片 image
如何将图片存入数据库中,并在Delphi中显示出来,我在我的窗体中有组件Timage,如何在其中显示啊、?

[解决办法]
procedure TForm1.savetodbClick(Sender: TObject); //保存图像
var
strm:tmemorystream;
ext:string;
begin
if image1.picture.Graphic <> nil then //避免image1中无图像保存出错
begin
ext:=extractfileext(openpicturedialog1.FileName ); //取出文件的扩展名
strm := tmemorystream.Create ;
try
image1.Picture.Graphic.SaveToStream(strm);
adotable1.Edit ;
strm.Position :=0; 
tblobfield(adotable1.FieldByName('myimage')).LoadFromStream(strm);
//如需直接由文件保存可采用如下注释行
//TBlobField(adotable1.FieldByName('myimage')).LoadFromFile(OpenPictureDialog1.FileName);
//以下记录保存到数据库的图像格式
if uppercase(ext) = '.BMP' then
adotable1.FieldByName('isbmp').Value := 1 //BMP型图像数据
else if (uppercase(ext) = '.JPG') OR ( uppercase(ext) = '.JPEG') Then
adotable1.FieldByName('isbmp').Value := 0; //JPEG型图像数据
adotable1.Post ;
finally
strm.Free ; //如strm采用tblobstream类,程序运行到该语句会出现问题
end;
end;
end;


[解决办法]
procedure TForm1.adoTable1AfterScroll(DataSet: TDataSet); //显示图像
var
strm:tadoblobstream;
jpegimage:tjpegimage;
bitmap:tbitmap;
begin
strm := tadoblobstream.Create(tblobfield(adotable1.fieldbyname('MYIMAGE')),bmread);
try //try1
strm.position :=0;
image1.Picture.Graphic := nil; //清除图像
// BMP、JPEG两种图像数据必需分别处理
if adotable1.fieldbyname('isbmp').asstring ='1' then //BMP型图像数据
begin //begin11
bitmap := tbitmap.Create ;
try //try11
bitmap.LoadFromStream(strm);
image1.Picture.Graphic := bitmap;
finally
bitmap.Free;
end; //end try11
end //end begin11
else if adotable1.fieldbyname('isbmp').asstring ='0' then //JPEG型图像数据
begin //begin12
jpegimage := tjpegimage.Create ;
try //try12
jpegimage.LoadFromStream(strm);
image1.Picture.Graphic := jpegimage;
finally
jpegimage.Free ;
end; //end try12
end; //end begin12
finally
strm.Free ;
end; //end try1
end;

读书人网 >.NET

热点推荐