zlib进行图象压缩与解压缩

shenhongsh 2005-05-12 09:37:44
终于登上来了,请教达人,看看我的程序
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls,jpeg,zlib;

type
TForm1 = class(TForm)
Button1: TButton;
Image1: TImage;
Button2: TButton;
Image2: TImage;
Button3: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
bmpstream:tmemorystream;
deststream:array of tmemorystream;
leftsize:integer;
ypos:integer=0;
xpos:integer=0;
// image:Timage;
//jpgstream:TJPEGImage;
ss:tcanvas;
jy:tmemorystream;

implementation
procedure UnCompressBitmap(const CompressedStream: TFileStream; var Bmp: TBitmap);

var

SourceStream: TDecompressionStream;

DestStream: TMemoryStream;

Buffer: PChar;

count:integer;
Begin

//从被压缩的图像流中读出原始图像的尺寸

// CompressedStream.ReadBuffer(Count, SizeOf(Count));
// form1.edit1.Text:=inttostr(count);
//根据图像尺寸大小为将要读入的原始图像流分配内存块
count:= CompressedStream.Size;
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;


procedure ScreenCap(LeftPos,TopPos,
RightPos,BottomPos:integer);
var
RectWidth,RectHeight:integer;
SourceDC,DestDC,Bhandle:integer;
Bitmap:TBitmap;
begin
RectWidth:=RightPos-LeftPos;
RectHeight:=BottomPos-TopPos;
SourceDC:=CreateDC('DISPLAY','','',nil);
DestDC:=CreateCompatibleDC(SourceDC);
Bhandle:=CreateCompatibleBitmap(SourceDC,
RectWidth,RectHeight);
SelectObject(DestDC,Bhandle);
BitBlt(DestDC,0,0,RectWidth,RectHeight,SourceDC,
LeftPos,TopPos,SRCCOPY);
Bitmap:=TBitmap.Create;
Bitmap.Handle:=BHandle;
BitMap.SaveToStream(BmpStream);
BmpStream.Position:=0;
LeftSize:=BmpStream.Size;
Bitmap.Free;
DeleteDC(DestDC);
ReleaseDC(Bhandle,SourceDC);
end;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var count:integer;
DestStream:tmemorystream;
sourcestream:TCompressionStream;
begin screencap(xpos,ypos,screen.Width,screen.Height);
bmpstream.SaveToFile('E:\Documents and Settings\shenhong\桌面\新建文件夹\source.dat');
image1.Picture.Bitmap.LoadFromStream(bmpstream);
Count:=bmpstream.Size;
DestStream:=TMemoryStream.Create;
SourceStream:=TCompressionStream.Create(cldefault, DestStream);
try
bmpStream.SaveToStream(SourceStream);
SourceStream.Free;
bmpstream.Clear;
bmpstream.WriteBuffer(Count, SizeOf(Count));
bmpstream.CopyFrom(DestStream, 0);
// jy.CopyFrom(DestStream, 0);
finally
deststream.SaveToFile('E:\Documents and Settings\shenhong\桌面\新建文件夹\yasuo.dat');
DestStream.Free;
end;
//deststream.SaveToFile('E:\Documents and Settings\shenhong\桌面\新建文件夹\dest.dat);


end;

procedure TForm1.FormCreate(Sender: TObject);
begin
//deststream:=tmemorystream.Create;
bmpstream:=tmemorystream.Create;
jy:=tmemorystream.Create;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
bmpstream.Destroy;
//deststream.destory;
end;

procedure TForm1.Button2Click(Sender: TObject);
var

CompressedStream: TFileStream;
Bmp: TBitmap;
begin
Bmp := TBitmap.Create;
//以文件流的只读方式打开自定义的压缩格式文件
CompressedStream := TFileStream.Create('E:\Documents and Settings\shenhong\桌面\新建文件夹\yasuo.dat', fmOpenRead);
//CompressedStream.Create();
Try
//将被压缩的图像流进行解压缩
UnCompressBitmap(CompressedStream, Bmp);
//将原始图像流还原为指定的 BMP文件
Bmp.SaveToFile('E:\Documents and Settings\shenhong\桌面\新建文件夹\jieyasuo.dat' );
image2.Picture.Bitmap:=bmp;
image2.Refresh;
finally
Bmp.Free;
CompressedStream.Free;
end;
end;
94K
本来是做的一个屏幕抓取,抓下来的大小是3M,转换为jpeg压缩之后是94K,用zlib压缩后是65K
所以打算用zlib,但是我解压缩之后发现用image显示的图象是黑色的图象,但是解压缩之后文件的大小和以前的一样,不知道是什么原因.有人帮忙吗?谢谢

...全文
217 1 打赏 收藏 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
shenhongsh 2005-05-12
  • 打赏
  • 举报
回复
有人顶没有,我自己顶
内容概要:本文介绍了一个基于Python与数字孪生技术的工业旅游园区运行监测系统的设计与实现。系统通过整合生产设备、环境传感器、客流统计和能源消耗等多源数据,构建统一的数据体系,并利用数字孪生模型实现园区的虚拟映射。系统架构涵盖数据采集、治理、时序存储、对象建模、智能分析与可视化展示五个层次,集成了数据清洗、异常检测(如Isolation Forest)、客流预测(如随机森林回归)、环境质量评价和综合运行评分等功能。通过FastAPI提供服务接口,支持实时监测、预警响应与科学决策,推动园区管理从人工向数据驱动转型。文中还提供了完整的数据生成、特征工程、模型训练与接口开发的代码示例。; 适合人群:具备Python编程基础,熟悉数据分析、机器学习及Web开发的技术人员,适用于高校学生、软件工程师、智能制造与智慧城市领域研究人员;尤其适合从事工业互联网、数字孪生、智慧园区等相关项目的1-3年工作经验开发者。; 使用场景及目标:①实现工业旅游园区的实时运行状态监控与三维可视化展示;②对客流、设备健康、环境舒适度与安全风险进行预测与预警;③为园区调度、应急管理、能源优化和游客服务提供数据支持与决策依据;④作为数字孪生教学案例,用于学习数据处理、模型构建与系统集成全流程。; 阅读建议:此资源结合理论模型与代码实践,建议读者在学习过程中同步运行示例代码,深入理解数据清洗、特征构造、算法建模与接口开发的关键步骤,并可进一步扩展至真实硬件接入、三维引擎集成与大规模部署应用。

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi GAME,图形处理/多媒体
社区管理员
  • GAME,图形处理/多媒体社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧