程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi圖像數據壓縮事例(2)

Delphi圖像數據壓縮事例(2)

編輯:Delphi

//2.壓縮圖像

procedure CompressBitmap(var CompressedStream: TMemoryStream;const CompressionLevel: TCompressionLevel);
     var
      SourceStream: TCompressionStream;
      DestStream: TMemoryStream;
      Count: Integer;
     Begin
      //獲得圖像流的原始尺寸
      Count := CompressedStream.Size;
      DestStream := TMemoryStream.Create;
      SourceStream:=TCompressionStream.Create
      (CompressionLevel, DestStream);
      Try
       //SourceStream中保存著原始的圖像流
       CompressedStream.SaveToStream(SourceStream);
       //將原始圖像流進行壓縮, DestStream中保存著壓縮後的圖像流
       SourceStream.Free;
       CompressedStream.Clear;
      //寫入原始圖像的尺寸
       CompressedStream.WriteBuffer(Count, SizeOf
       (Count));
       //寫入經過壓縮的圖像流
       CompressedStream.CopyFrom(DestStream, 0);
       finally
       DestStream.Free;
      end;
     end;

3.還原被壓縮圖像

procedure UnCompressBitmap(const CompressedStream: TFileStream; var Bmp: TBitmap);
      var
       SourceStream: TDecompressionStream;
       DestStream: TMemoryStream;
       Buffer: PChar;
       Count: Integer;
      Begin
       //從被壓縮的圖像流中讀出原始圖像的尺寸
       CompressedStream.ReadBuffer(Count, SizeOf(Count));
       //根據圖像尺寸大小為將要讀入的原始圖像流分配內存塊
       GetMem(Buffer, Count);
       DestStream := TMemoryStream.Create;
       SourceStream := TDecompressionStream.Create(CompressedStream);
      Try
       //將被壓縮的圖像流解壓縮,然後存入 Buffer內存塊中
       SourceStream.ReadBuffer(Buffer^, Count);
       //將原始圖像流保存至 DestStream流中
       DestStream.WriteBuffer(Buffer^, Count);
       DestStream.Position := 0;//復位流指針
       //從 DestStream流中載入原始圖像流
       Bmp.LoadFromStream(DestStream);
       finally
       FreeMem(Buffer);
       DestStream.Free;
      end;
     end;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved