图片的存储 ( 积分: 50 )

  • 主题发起人 主题发起人 wtl
  • 开始时间 开始时间
W

wtl

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么往数据库里插入图片,然后怎么用image组件读取图片
 
网上一搜就是一大对<br>提示你:用blob字段存取图片
 
with&nbsp;self.adoQuery1&nbsp;do<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;close;<br>&nbsp;&nbsp;&nbsp;&nbsp;sql.Clear;<br>&nbsp;&nbsp;&nbsp;&nbsp;sql.Add('insert&nbsp;into&nbsp;sfis1.USER_SELECTPRG&nbsp;(user_id,modulecode,pic,PIC_TYPE)&nbsp;values&nbsp;(:userid,''007'',:PIC,:PICTYPE)');<br>&nbsp;&nbsp;&nbsp;&nbsp;//parameters.ParamByName('pic').LoadFromStream(self.funLoadPic(path),&nbsp;ftgraphic);<br>&nbsp;&nbsp;&nbsp;&nbsp;parameters.ParamByName('userid').Value:=sUserID;<br>&nbsp;&nbsp;&nbsp;&nbsp;parameters.ParamByName('pic').Assign(image1.Picture.Graphic);<br>&nbsp;&nbsp;&nbsp;&nbsp;parameters.ParamByName('PICTYPE').Value:=sPICType;<br>&nbsp;&nbsp;&nbsp;&nbsp;execsql;<br>&nbsp;&nbsp;end;<br><br><br>procedure&nbsp;TForm1.DBGrid1CellClick(Column:&nbsp;TColumn);<br>var<br>&nbsp;&nbsp;stream1:&nbsp;TMemoryStream;<br>&nbsp;&nbsp;sd:&nbsp;TStringStream;<br>&nbsp;&nbsp;str:string;<br>&nbsp;&nbsp;path:string;<br><br>&nbsp;&nbsp;strm:Tadoblobstream;<br>&nbsp;&nbsp;bitmap:Tbitmap;<br>&nbsp;&nbsp;jpegimage&nbsp;:&nbsp;tjpegimage;<br>begin<br>&nbsp;&nbsp;if&nbsp;self.ADOQuery1.FieldByName('PIC_TYPE').AsString='BMP'&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;strm&nbsp;:=&nbsp;tadoblobstream.Create(tblobfield(adoQuery1.FieldByName('pic')),bmread);<br>&nbsp;&nbsp;&nbsp;&nbsp;strm.position&nbsp;:=0;<br>&nbsp;&nbsp;&nbsp;&nbsp;image1.Picture.Graphic&nbsp;:=&nbsp;nil;<br>&nbsp;&nbsp;&nbsp;&nbsp;bitmap&nbsp;:=&nbsp;tbitmap.Create&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bitmap.LoadFromStream(strm);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image1.Picture.Graphic&nbsp;:=&nbsp;bitmap;<br>&nbsp;&nbsp;&nbsp;&nbsp;finally<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bitmap.Free;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;end<br>&nbsp;&nbsp;else&nbsp;if&nbsp;self.ADOQuery1.FieldByName('PIC_TYPE').AsString='JPG'&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;jpegimage&nbsp;:=&nbsp;tjpegimage.Create&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jpegimage.Assign(adoQuery1.FieldByName('pic'));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image1.Picture.Graphic&nbsp;:=jpegimage;<br>&nbsp;&nbsp;&nbsp;&nbsp;finally<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jpegimage.Free;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;end<br>&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;showmessage('&amp;frac12;&amp;ETH;&amp;frac12;T&amp;raquo;{¨&amp;auml;&amp;sup1;&amp;Iuml;¤ù&amp;Atilde;&amp;thorn;&amp;laquo;&amp;not;');<br>&nbsp;&nbsp;<br>end;
 
这是我用的代码<br>procedure&nbsp;TForm1.Button1Click(Sender:&nbsp;TObject);<br>var<br>&nbsp;&nbsp;i:Integer;<br>&nbsp;&nbsp;Ext:String;<br>&nbsp;&nbsp;Strm:TmemoryStream;<br>&nbsp;&nbsp;JPG:TJPEGImage;<br>&nbsp;&nbsp;PicName:String;<br>begin<br>&nbsp;&nbsp;if&nbsp;OpenPictureDialog1.Execute&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;//检查图片格式是否是JPG<br>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;i:=0&nbsp;to&nbsp;OpenPictureDialog1.Files.Count-1&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;application.ProcessMessages;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PicName:=UpperCase(ExtractFileName(OpenPictureDialog1.Files.Strings));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PicName:=Copy(PicName,1,length(PicName)-4);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ext:=ExtractFileExt(OpenPictureDialog1.Files.Strings);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;Not&nbsp;(UpperCase(Ext)='.JPG')&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;showmessage(PicName+'文件格式不对,请选择JPG格式!');<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exit;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;end&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;//保存图片及数据<br>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;i:=0&nbsp;to&nbsp;OpenPictureDialog1.Files.Count-1&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;application.ProcessMessages;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.Close;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.Sql.Clear;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.Sql.Add('select&nbsp;*&nbsp;from&nbsp;tPCStylePicData');<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.Open;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.Append;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PicName:=UpperCase(ExtractFileName(OpenPictureDialog1.Files.Strings));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PicName:=Copy(PicName,1,length(PicName)-4);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.FieldByName('StyleNO').AsString:=PicName;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JPG:=TJPEGImage.Create;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JPG.CompressionQuality&nbsp;:=&nbsp;30;//压缩率<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JPG.Compress;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JPG.JPEGNeeded;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Strm:=TmemoryStream.Create&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JPG.LoadFromFile(OpenPictureDialog1.Files.Strings);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JPG.SaveToStream(Strm);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Strm.Position:=0;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TBlobField(ADOQuery1.FieldByName('Pic')).LoadFromStream(Strm);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.Post;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;finally<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JPG.Free;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Strm.Free;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;end&nbsp;;<br>end;
 
//显示数据库JPG图片字段<br>procedure&nbsp;THC_DM.showimage(image1:timage;&nbsp;imagers:tadoquery;&nbsp;imagefield:&nbsp;string);<br>Var<br>&nbsp;&nbsp;Ms:TStringStream;<br>&nbsp;&nbsp;jpg:Tjpegimage;<br>begin<br>&nbsp;&nbsp;if&nbsp;imagers.IsEmpty&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;image1.Picture.Graphic&nbsp;:=nil;<br>&nbsp;&nbsp;&nbsp;&nbsp;exit;<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;if&nbsp;TBlobField(imagers.FieldByName(imagefield)).BlobSize&nbsp;&gt;0&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;Ms:=TstringStream.Create('');<br>&nbsp;&nbsp;&nbsp;&nbsp;TBlobField(imagers.FieldByName(imagefield)).SaveToStream(Ms);<br>&nbsp;&nbsp;&nbsp;&nbsp;Ms.Position&nbsp;:=0;<br>&nbsp;&nbsp;&nbsp;&nbsp;jpg:=tjpegimage.Create&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;Jpg.LoadFromStream(Ms);<br>&nbsp;&nbsp;&nbsp;&nbsp;image1.Picture.Assign(Jpg);<br>&nbsp;&nbsp;&nbsp;&nbsp;Ms.Free;<br>&nbsp;&nbsp;&nbsp;&nbsp;jpg.free;<br><br>&nbsp;&nbsp;end<br>&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;image1.Picture.Graphic&nbsp;:=nil;<br><br>end;<br><br>//JPG图像保存到数据库<br>procedure&nbsp;thc_dm.saveimage(filename:string;&nbsp;imagers:tadoquery;&nbsp;imagefield:&nbsp;string);<br>var<br>&nbsp;&nbsp;Ms:TmemoryStream;<br>&nbsp;&nbsp;jpg:Tjpegimage;<br>begin<br>&nbsp;&nbsp;ms:=TmemoryStream.Create;<br>&nbsp;&nbsp;jpg:=tjpegimage.Create&nbsp;;<br>&nbsp;&nbsp;Jpg.LoadFromFile(appdir+filename);<br>&nbsp;&nbsp;Jpg.SaveToStream(Ms)&nbsp;;<br>&nbsp;&nbsp;Ms.Position&nbsp;:=0;<br>&nbsp;&nbsp;imagers.edit;<br>&nbsp;&nbsp;TBlobField(imagers.FieldByName(imagefield)).LoadFromStream(Ms);<br>&nbsp;&nbsp;imagers.Post;<br>&nbsp;&nbsp;Ms.Free&nbsp;;<br>&nbsp;&nbsp;jpg.free;<br>end;
 
例如:<br>&nbsp;&nbsp;&nbsp;要将图片保存到数据库,图片大小不变<br>1、设计界面如图所示&nbsp;SQLSERVER里的图片的字段为:image,access里的图片的字段为:OLE&nbsp;对象<br>2、快捷菜单的具体代码如下:<br>&nbsp;&nbsp;载入图片:<br>procedure&nbsp;TForm1.N4Click(Sender:&nbsp;TObject);<br>begin<br>if&nbsp;ADOQUERY1.State&nbsp;in&nbsp;[dsedit,dsinsert]&nbsp;then<br>begin<br>if&nbsp;OpenPictureDialog1.Execute&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;DBIMAGE1.Picture.LoadFromFile(OpenPictureDialog1.FileName);<br>&nbsp;&nbsp;&nbsp;&nbsp;DBIMAGE1.CopyToClipboard;<br>&nbsp;&nbsp;&nbsp;&nbsp;ADOQUERY1.Edit;<br>&nbsp;&nbsp;&nbsp;&nbsp;DBIMAGE1.PasteFromClipboard;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>end&nbsp;else&nbsp;messagedlg('记录集不可写!',mtError,[mbOk],0);<br>end;<br><br>&nbsp;&nbsp;复制图片:<br>procedure&nbsp;TForm1.N1Click(Sender:&nbsp;TObject);<br>begin<br>DBIMAGE1.CopyToClipboard;<br>end;<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;粘贴图片:<br>procedure&nbsp;TForm1.N2Click(Sender:&nbsp;TObject);<br>begin<br>DBIMAGE1.PasteFromClipboard;<br>end;<br>&nbsp;&nbsp;&nbsp;删除图片:&nbsp;&nbsp;//要在adoquery1里选中字段&nbsp;注意ADOQUERY1DSDesigner2&nbsp;的位置<br>procedure&nbsp;TForm1.N3Click(Sender:&nbsp;TObject);<br>begin<br>if&nbsp;messagedlg('是否要清除该图片!',mtConfirmation,[mbYes,mbNo],0)=mrYes&nbsp;then<br>&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;if&nbsp;ADOQUERY1.State&nbsp;in&nbsp;[dsedit,dsinsert]&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ADOQUERY1DSDesigner2.Clear<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;messagedlg('该记录处于非可写状态!请置于编辑状态再重试!',mtError,[mbOk],0);<br>&nbsp;&nbsp;&nbsp;end;<br>end;
 
后退
顶部