保存图片到数据库,从数据库里提取图片

1、如何实现在Microsoft Access数据库中的图像存储: 

这里是利用TStream的子类TMemoryStream向Microsoft Access数据库中存储图像的。 

下面的这段代码是在按了“保存”按钮之后所触发的事件处理程序: 

procedure TForm1.Button1Click(Sender: TObject); 

Var 

MyJPEG : TJPEGImage; 

MS: TMemoryStream; 

Begin 

MyJPEG := TJPEGImage.Create; 

Try 

With MyJPEG do 

Begin 

Assign(Image.Picture.Graphic); 

MS:=TMemoryStream.create; 

SaveToStream(MS); 

MS.Position := 0; 

Table1.Edit; 

TBlobField(Table1.FieldbyName('Image')).LoadFromStream(MS); 

Table1.Post; 

Messagebox(Getactivewindow(), '图像保存完毕!', '保存', MB_OK); 

End; 

Finally 

MyJPEG.Free; 

End; 

End; 

在这段代码里TStream的子类TMemoryStream利用内存流起到了将BMP格式转化为JPEG格式 

的中间桥梁的作用。 



2、如何将图像从Microsoft Access数据库中取出并显示出来: 

下面的这段代码是在按了“查看图像”按钮之后所触发的事件处理程序: 

procedure TForm1.Button1Click(Sender: TObject); 

Var 

MyJPEG : TJPEGImage; 

MS : TMemoryStream; 

Begin 

Try 

MS := TMemoryStream.Create; 

TBlobField(Query1.FieldByName('Image')).SaveToStream(MS); 

MS.Position := 0; 

MyJPEG := TJPEGImage.Create; 

MyJPEG.LoadFromStream(MS); 

DBImage1.Picture.Bitmap.Assign(MyJPEG); 

//或是Sender.Picture.Assign(MyJPEG); 

Finally 

MS.Free; 

MyJPEG.Free; 

End; 

End; 



==========================================================================



图片保存到数据库的例子:   

    

  //保存     

  procedure   TForm1.ImageIntoDBBitBtnClick(Sender:   TObject);     

  begin     

      try     

          with   Query1   do     

              begin     

                  close;     

                  sql.clear;     

                  sql.add('insert   into   img   (imga)   values(:imag)');     

              end;     

          try     

              if   FileName<>''   then     

                  ParamByName('Img').LoadfromFile(FileName,ftGraphic)     

              else     

                  ParamByName('Img')   .asBlob:='';     

              Query1.ExecSQL;     

          except     

              ShowMessage('图片保存出错!');     

              exit;     

          end;     

      except     

      end;     

  end;     

      

  //读取     

  procedure   TForm1.ImageFromDBBitBtnClick(Sender:   TObject);     

  var     

      m_jpegstream:tmemorystream;     

  begin     

      with   Query1   do     

      begin     

          close;     

          sql.clear;     

          sql.Add('select   imga   from   img');     

          try     

              Open;     

          except     

              exit;     

          end;     

      end;     

      if   (Query1.FieldByName('imga')   as   tblobfield).asstring=''   then     

      begin     

          exit;     

      end     

      else     

      begin     

          try     

          try     

              m_jpegStream:=TMemoryStream.Create;     

              (Query1.fieldbyname('imga')   as   TBlobField).SaveToStream(m_JpegStream);     

              m_JpegStream.Position:=0;     

              try     

                  image2.Picture.Graphic:=nil;     

                  image2.Picture.Graphic:=TJpegImage.Create;     

                  image2.Picture.Graphic.LoadFromStream(m_JpegStream);     //读Jpeg     

              except     

                  Image2.Picture.Bitmap.LoadFromStream(m_JpegStream);       //读Bmp     

              end;     

          except     

          end;     

          finally     

              m_JpegStream.Free;     

              Query1.Close;     

              Query1.UnPrepare;     

          end;     

      end;     

  end;     

      

  //BMP转换为JPG     

  procedure   TStuXXSLForm.LoadImageBitBtnClick(Sender:   TObject);     

  var     

      MyJpeg:TJpegImage;     

  begin     

      inherited;     

      if   OpenPictureDialog1.Execute   then     

      begin     

          FileName:=OpenPictureDialog1.FileName;     

          Image1.Picture.LoadFromFile(FileName);     

          if   ExtractFileExt(FileName)='.Bmp'   then     

          begin     

              MyJpeg:=   TJpegImage.Create;     

              MyJpeg.Assign(Image1.Picture.Bitmap);     

              FileName:='Photo.Jpg';     

              MyJpeg.SaveToFile(FileName);     

              MyJpeg.Free;     

          end;     

      end;     

  end;



用到TBlobField的时候要在接口包含DB,用到TJPEGImage时要在接口包含Jpeg 



你可能感兴趣的:(数据库)