如何进行JPG图片的存入及读取

来源:互联网 发布:ie11 js缓存清除不掉 编辑:程序博客网 时间:2024/06/05 00:43

1、将JPG文件存入数据库

procedure TForm1.jpgsave(wj:string);

        var bmp:tbitmap;jpg:TJPEGImage;ms:tmemorystream;
begin
         Image1.Picture.LoadFromFile(wj);
         jpg:=TJPEGImage.Create;
         Jpg.Assign(Image1.Picture);

         ms:=Tmemorystream.Create;

        Jpg.SaveToStream(MS);
     with Clientdataset1  do
     begin
         if active then close;
         commandtext:='select phot from table1';
         open;
         edit;
         TBlobfield(cds.fieldbyname('phot')).LoadFromStream(ms);
         post;
         ApplyUpdates(0);
     end;
end;


2、将JPG文件从数据库读出显示

procedure jpgread(wj:string);
    var ms:TMemoryStream;jpg:TJPEGimage;
begin
   Jpg := TJPEGImage.Create;
   MS := TMemoryStream.Create;
     with Clientdataset1  do
     begin
       if active then close;
       commandtext:='select phot from table1';
       open;
       TBLobfield(fieldbyname('phot')).SaveToStream(MS);
       MS.Seek(0, soFromBeginning);
       with Jpg do
                  begin
                     PixelFormat := jf24Bit;
                     Scale := jsFullSize;
                     Grayscale := False;
                     Performance := jpBestQuality;
                     ProgressiveDisplay := True;
                     ProgressiveEncoding := True;
                     LoadFromStream(MS);
                  end;
         Image1.Picture.Assign(Jpg);
    end;
end;
0 0