请问如何用zlib解压保存在数据库中的流 ( 积分: 50 )

  • 主题发起人 主题发起人 piaoping
  • 开始时间 开始时间
P

piaoping

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure&nbsp;TForm1.Button1Click(Sender:&nbsp;TObject);<br>var<br>&nbsp;&nbsp;i:Integer;<br>&nbsp;&nbsp;Strm:TmemoryStream;<br>&nbsp;&nbsp;JPG:TJPEGImage;<br>begin<br>&nbsp;&nbsp;if&nbsp;OpenPictureDialog1.Execute&nbsp;then<br>&nbsp;&nbsp;begin<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;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;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;CompressBitmap(Strm,clmax);&nbsp;//&nbsp;调用压缩函数进行压缩<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;<br><br>procedure&nbsp;TForm1.CompressBitmap(var&nbsp;CompressedStream:&nbsp;TMemoryStream;const&nbsp;CompressionLevel:&nbsp;TCompressionLevel);<br>var&nbsp;SourceStream:&nbsp;TCompressionStream;<br>&nbsp;&nbsp;&nbsp;&nbsp;DestStream:&nbsp;TMemoryStream;<br>&nbsp;&nbsp;&nbsp;&nbsp;Count:&nbsp;Integer;<br>Begin<br>&nbsp;&nbsp;//获得图像流的原始尺寸<br>&nbsp;&nbsp;Count&nbsp;:=&nbsp;CompressedStream.Size;<br>&nbsp;&nbsp;DestStream&nbsp;:=&nbsp;TMemoryStream.Create;<br>&nbsp;&nbsp;SourceStream:=TCompressionStream.Create(CompressionLevel,&nbsp;DestStream);<br>&nbsp;&nbsp;Try<br>&nbsp;&nbsp;&nbsp;&nbsp;//SourceStream中保存着原始的图像流<br>&nbsp;&nbsp;&nbsp;&nbsp;CompressedStream.SaveToStream(SourceStream);<br>&nbsp;&nbsp;&nbsp;&nbsp;//将原始图像流进行压缩,&nbsp;DestStream中保存着压缩后的图像流<br>&nbsp;&nbsp;&nbsp;&nbsp;SourceStream.Free;<br>&nbsp;&nbsp;&nbsp;&nbsp;CompressedStream.Clear;<br>&nbsp;&nbsp;&nbsp;&nbsp;//写入原始图像的尺寸<br>&nbsp;&nbsp;&nbsp;&nbsp;CompressedStream.WriteBuffer(Count,&nbsp;SizeOf(Count));<br>&nbsp;&nbsp;&nbsp;&nbsp;//写入经过压缩的图像流<br>&nbsp;&nbsp;&nbsp;&nbsp;CompressedStream.CopyFrom(DestStream,&nbsp;0);<br>&nbsp;&nbsp;finally<br>&nbsp;&nbsp;&nbsp;&nbsp;DestStream.Free;<br>&nbsp;&nbsp;end;<br>end;<br>我用上面的代码把jpg图片用流压缩后存进了数据库,下面的代码是显示图片,<br>但只能显示未压缩的流,希望兄弟帮忙改一下,可以把压缩过的流从数据库中读出来,<br>然后解压,然后再显示<br>procedure&nbsp;TForm1.DBGrid1CellClick(Column:&nbsp;TColumn);<br>var<br>&nbsp;&nbsp;Stream&nbsp;:TMemorystream;<br>&nbsp;&nbsp;JPG:TJPEGImage;<br>begin<br>&nbsp;&nbsp;Stream:=&nbsp;TMemorystream.Create;<br>&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;TBlobField(ADOQuery1.FieldByName('Pic')).SaveToStream(Stream);<br>&nbsp;&nbsp;&nbsp;&nbsp;Stream.Position:=0;<br>&nbsp;&nbsp;&nbsp;&nbsp;JPG:=TJPEGImage.Create;<br>&nbsp;&nbsp;&nbsp;&nbsp;JPG.LoadFromStream(Stream);<br>&nbsp;&nbsp;&nbsp;&nbsp;Image1.Picture.Assign(JPG);<br>&nbsp;&nbsp;finally<br>&nbsp;&nbsp;&nbsp;JPG.Free;<br>&nbsp;&nbsp;&nbsp;&nbsp;Stream.Free;<br>&nbsp;&nbsp;end;<br>end;
 
procedure&nbsp;DecompressStream(Source,&nbsp;Target:&nbsp;TStream);<br>var<br>&nbsp;&nbsp;Stream:&nbsp;TDecompressionStream;<br>&nbsp;&nbsp;nRead:&nbsp;Integer;<br>&nbsp;&nbsp;Buffer:&nbsp;array&nbsp;[0..1023]&nbsp;of&nbsp;Char;<br>begin<br>&nbsp;&nbsp;Stream&nbsp;:=&nbsp;TDecompressionStream.Create(Source);<br>&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;repeat<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nRead&nbsp;:=&nbsp;Stream.Read(Buffer,&nbsp;1024);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Target.Write(Buffer,&nbsp;nRead);<br>&nbsp;&nbsp;&nbsp;&nbsp;until&nbsp;nRead&nbsp;=&nbsp;0;<br>&nbsp;&nbsp;finally<br>&nbsp;&nbsp;&nbsp;&nbsp;Stream.Free;<br>&nbsp;&nbsp;end;<br>end;<br><br>procedure&nbsp;TForm1.DBGrid1CellClick(Column:&nbsp;TColumn);<br>var<br>&nbsp;&nbsp;Stream,&nbsp;Target:TMemorystream;<br>&nbsp;&nbsp;JPG:TJPEGImage;<br>begin<br>&nbsp;&nbsp;Stream:=&nbsp;TMemorystream.Create;<br>&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;TBlobField(ADOQuery1.FieldByName('Pic')).SaveToStream(Stream);<br>&nbsp;&nbsp;&nbsp;&nbsp;Stream.Position:=0;<br>&nbsp;&nbsp;&nbsp;&nbsp;Target&nbsp;:=&nbsp;TMemoryStream.Create;<br>&nbsp;&nbsp;&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DecompressStream(Stream,&nbsp;Target);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JPG:=TJPEGImage.Create;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JPG.LoadFromStream(Target);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image1.Picture.Assign(JPG);<br>&nbsp;&nbsp;&nbsp;&nbsp;finally<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Target.Free;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;finally<br>&nbsp;&nbsp;&nbsp;JPG.Free;<br>&nbsp;&nbsp;&nbsp;&nbsp;Stream.Free;<br>&nbsp;&nbsp;end;<br>end;
 
非常感谢ANiDelphi,不知您调试过没有,在DBGrid1CellClick事件中总提示<br>project&nbsp;project.exe&nbsp;raised&nbsp;exception&nbsp;class&nbsp;ECompressionError&nbsp;with&nbsp;message<br>'Error'.&nbsp;process&nbsp;stopped.&nbsp;Use&nbsp;step&nbsp;or&nbsp;Run&nbsp;to&nbsp;contionue.<br>然后就停在&nbsp;JPG:=TJPEGImage.Create;这一行
 
until&nbsp;nRead&nbsp;&lt;1024;
 
//才没几天用过的,好象JPG,TIF,BMP经过压缩的大小都差不了多少<br>procedure&nbsp;TFrmKXT.SpeedButton1Click(Sender:&nbsp;TObject);<br>var<br>&nbsp;&nbsp;ext:&nbsp;string;<br>begin<br>&nbsp;&nbsp;GifFile.Clear;<br>&nbsp;&nbsp;if&nbsp;not&nbsp;picopen.Execute&nbsp;then&nbsp;exit;<br>&nbsp;&nbsp;ext:=ExtractFileExt(picopen.FileName);<br>&nbsp;&nbsp;image1.Picture.LoadFromFile(picopen.FileName);<br>&nbsp;&nbsp;PicFBN(Image1);<br>&nbsp;&nbsp;if&nbsp;ext&lt;&gt;'.gif'&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;GifFile:=pictogif(image1.Picture);<br>&nbsp;&nbsp;end&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;GifFile.LoadFromFile(picopen.FileName);<br>end;<br><br>procedure&nbsp;TFrmKXT.SpeedButton2Click(Sender:&nbsp;TObject);<br>var<br>&nbsp;&nbsp;MS:&nbsp;TMemoryStream;<br>begin<br>&nbsp;&nbsp;if&nbsp;(edit1.Text='')&nbsp;or&nbsp;(memo1.Text='')&nbsp;then&nbsp;exit;<br>&nbsp;&nbsp;MS:=TMemoryStream.Create;<br>&nbsp;&nbsp;GifFile.SaveToStream(MS);<br>&nbsp;&nbsp;ZipPicStream(MS,clMax);<br>&nbsp;&nbsp;WITH&nbsp;TABLE1&nbsp;DO<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;locate('名称',trim(edit1.Text),[loPartialKey])&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;showmessage('该编号已存在!!!');<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;append;<br>&nbsp;&nbsp;&nbsp;&nbsp;fieldbyname('名称').AsString:=trim(edit1.Text);<br>&nbsp;&nbsp;&nbsp;&nbsp;fieldbyname('说明').asstring:=memo1.Text;<br>&nbsp;&nbsp;&nbsp;&nbsp;TBlobField(Table1.fieldbyname('图片')).LoadFromStream(MS);<br>&nbsp;&nbsp;&nbsp;&nbsp;post;<br>&nbsp;&nbsp;&nbsp;&nbsp;messagebox(getactivewindow(),'图像保存完毕!','保存',mb_ok);<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;MS.Free;<br>end;<br><br>procedure&nbsp;TFrmKXT.DBGrid1CellClick(Column:&nbsp;TColumn);<br>var<br>&nbsp;&nbsp;Stream:TMemoryStream;<br>&nbsp;&nbsp;b:&nbsp;Tbitmap;<br>begin<br>&nbsp;&nbsp;b:=tbitmap.Create;<br>&nbsp;&nbsp;if&nbsp;isbegin&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;Stream:=TMemoryStream.Create;<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;Table1.FieldByName('图片').IsNull&nbsp;then&nbsp;exit;<br>&nbsp;&nbsp;&nbsp;&nbsp;TBlobField(Table1.FieldByName('图片')).SaveToStream(Stream);<br>&nbsp;&nbsp;&nbsp;&nbsp;Stream.Position:=0;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;UnPic(Stream,b);<br>&nbsp;&nbsp;&nbsp;&nbsp;image1.Picture.Bitmap:=b;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;PicFBN(image1);<br>&nbsp;&nbsp;&nbsp;&nbsp;Stream.Free;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b.Free;<br>&nbsp;&nbsp;&nbsp;&nbsp;memo1.Text:=table1.FieldValues['说明'];<br>&nbsp;&nbsp;&nbsp;&nbsp;edit1.Text:=table1.FieldValues['名称'];<br>&nbsp;&nbsp;end;<br>end;
 
to:SP229兄,把你写的函数UnPic(Stream,b);PicFBN(image1);ZipPicStream(MS,clMax);发出来
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
640
import
I
后退
顶部