P
piaoping
Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.Button1Click(Sender: TObject);<br>var<br> i:Integer;<br> Strm:TmemoryStream;<br> JPG:TJPEGImage;<br>begin<br> if OpenPictureDialog1.Execute then<br> begin<br> //保存图片到数据<br> for i:=0 to OpenPictureDialog1.Files.Count-1 do<br> begin<br> application.ProcessMessages;<br> try<br> ADOQuery1.Close;<br> ADOQuery1.Sql.Clear;<br> ADOQuery1.Sql.Add('select * from tPCStylePicData');<br> ADOQuery1.Open;<br> ADOQuery1.Append;<br> JPG:=TJPEGImage.Create;<br> JPG.CompressionQuality := 30;//压缩率<br> Strm:=TmemoryStream.Create ;<br> JPG.LoadFromFile(OpenPictureDialog1.Files.Strings);<br> JPG.SaveToStream(Strm);<br> Strm.Position:=0;<br> CompressBitmap(Strm,clmax); // 调用压缩函数进行压缩<br> TBlobField(ADOQuery1.FieldByName('Pic')).LoadFromStream(strm);<br> ADOQuery1.Post;<br> finally<br> JPG.Free;<br> Strm.Free;<br> end;<br> end;<br> end ;<br>end;<br><br>procedure TForm1.CompressBitmap(var CompressedStream: TMemoryStream;const CompressionLevel: TCompressionLevel);<br>var SourceStream: TCompressionStream;<br> DestStream: TMemoryStream;<br> Count: Integer;<br>Begin<br> //获得图像流的原始尺寸<br> Count := CompressedStream.Size;<br> DestStream := TMemoryStream.Create;<br> SourceStream:=TCompressionStream.Create(CompressionLevel, DestStream);<br> Try<br> //SourceStream中保存着原始的图像流<br> CompressedStream.SaveToStream(SourceStream);<br> //将原始图像流进行压缩, DestStream中保存着压缩后的图像流<br> SourceStream.Free;<br> CompressedStream.Clear;<br> //写入原始图像的尺寸<br> CompressedStream.WriteBuffer(Count, SizeOf(Count));<br> //写入经过压缩的图像流<br> CompressedStream.CopyFrom(DestStream, 0);<br> finally<br> DestStream.Free;<br> end;<br>end;<br>我用上面的代码把jpg图片用流压缩后存进了数据库,下面的代码是显示图片,<br>但只能显示未压缩的流,希望兄弟帮忙改一下,可以把压缩过的流从数据库中读出来,<br>然后解压,然后再显示<br>procedure TForm1.DBGrid1CellClick(Column: TColumn);<br>var<br> Stream :TMemorystream;<br> JPG:TJPEGImage;<br>begin<br> Stream:= TMemorystream.Create;<br> try<br> TBlobField(ADOQuery1.FieldByName('Pic')).SaveToStream(Stream);<br> Stream.Position:=0;<br> JPG:=TJPEGImage.Create;<br> JPG.LoadFromStream(Stream);<br> Image1.Picture.Assign(JPG);<br> finally<br> JPG.Free;<br> Stream.Free;<br> end;<br>end;