const //定义俩常量
ImageWidth = 32; //图片宽度
ImageHeight = 32; //图片高度
{压缩位图到文件}
procedure CompressBitmap(const Bitmap: TBitmap; const FileName: string);
var
BitSet: TBits; //位集合
Stream: TFileStream; //文件流
i, j, ByteIndex: Integer; //索引
BitValue: Byte; //字节
begin
BitSet := TBits.Create(); // 创建位集合
BitSet.Size := ImageWidth * ImageHeight; //位集合大小=长x宽
try
for i := 0 to ImageHeight - 1 do
begin
for j := 0 to ImageWidth - 1 do
begin
// 若黑色则置1(就是True),白色为 0 (就是False)
if Bitmap.Canvas.Pixels[j, i] = clBlack then
BitSet.Bits[i * ImageWidth + j] := True;
end;
end;
// 保存到文件
Stream := TFileStream.Create(FileName, fmCreate);
try
//8个位->组合一个Byte字节
for ByteIndex := 0 to (BitSet.Size div 8) - 1 do
begin
BitValue := 0;
//8个位一次。遍历8个位将对应的位值设置到Byte里。
for j := 0 to 7 do
begin
if BitSet.Bits[ByteIndex * 8 + j] then
BitValue := BitValue or (1 shl j);
end;
//然后把这个Byte写出到文件里
Stream.Write(BitValue, SizeOf(Byte));
end;
finally
Stream.Free;
end;
finally
BitSet.Free;
end;
end;
{从文件读取还原回bmp}
procedure DecompressBitmap(const FileName: string; var Bitmap: TBitmap);
var
Stream: TFileStream;
BitSet: TBits;
i, j, ByteIndex: Integer;
BitValue: Byte;
begin
BitSet := TBits.Create();
BitSet.Size := ImageWidth * ImageHeight;
try
// 从文件读取位数据
Stream := TFileStream.Create(FileName, fmOpenRead);
try
for ByteIndex := 0 to (BitSet.Size div 8) - 1 do
begin
Stream.Read(BitValue, SizeOf(Byte));
for j := 0 to 7 do
begin
if (ByteIndex * 8 + j) < BitSet.Size then
BitSet.Bits[ByteIndex * 8 + j] := ((BitValue and (1 shl j)) <> 0);
end;
end;
finally
Stream.Free;
end;
// 创建 BMP 图像
Bitmap.Width := ImageWidth;
Bitmap.Height := ImageHeight;
Bitmap.PixelFormat := pf32bit; // 使用 32 位像素格式
for i := 0 to ImageHeight - 1 do
begin
for j := 0 to ImageWidth - 1 do
begin
if BitSet.Bits[i * ImageWidth + j] then
Bitmap.Canvas.Pixels[j, i] := clBlack
else
Bitmap.Canvas.Pixels[j, i] := clWhite;
end;
end;
finally
BitSet.Free;
end;
end;
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | |
0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
1 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
2 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 |
3 | ||||||||||||||||||||||||||||||||
4 | ||||||||||||||||||||||||||||||||
5 | ||||||||||||||||||||||||||||||||
6 | ||||||||||||||||||||||||||||||||
7 | ||||||||||||||||||||||||||||||||
8 | ||||||||||||||||||||||||||||||||
9 | ||||||||||||||||||||||||||||||||
10 | ||||||||||||||||||||||||||||||||
11 | ||||||||||||||||||||||||||||||||
12 | ||||||||||||||||||||||||||||||||
13 | ||||||||||||||||||||||||||||||||
14 | ||||||||||||||||||||||||||||||||
15 | ||||||||||||||||||||||||||||||||
16 | ||||||||||||||||||||||||||||||||
17 | ||||||||||||||||||||||||||||||||
18 | ||||||||||||||||||||||||||||||||
19 | ||||||||||||||||||||||||||||||||
20 | ||||||||||||||||||||||||||||||||
21 | ||||||||||||||||||||||||||||||||
22 | ||||||||||||||||||||||||||||||||
23 | ||||||||||||||||||||||||||||||||
24 | ||||||||||||||||||||||||||||||||
25 | ||||||||||||||||||||||||||||||||
26 | ||||||||||||||||||||||||||||||||
27 | ||||||||||||||||||||||||||||||||
28 | ||||||||||||||||||||||||||||||||
29 | ||||||||||||||||||||||||||||||||
30 | ||||||||||||||||||||||||||||||||
31 |
procedure TForm12.Button2Click(Sender: TObject);
begin
{压缩}
var bmp := Tbitmap.Create;
bmp.LoadFromFile('32.bmp');
CompressBitmap(bmp, 'Yasuo.dat');
bmp.Free;
{解压还原}
var newbmp := Tbitmap.Create;
DecompressBitmap('Yasuo.dat', newbmp);
//显示到图片框看一下对不对
image1.Picture.Bitmap.Assign(newbmp);
newbmp.Free;
end;
子程序名 | 返回值类型 | 公开 | 备 注 | ||
_按钮1_被单击 |
变量名 | 类 型 | 静态 | 数组 | 备 注 | ||
Byte | 字节型 | |||||
i | 整数型 |
欢迎光临 精易论坛 (https://125.confly.eu.org/) | Powered by Discuz! X3.4 |