直接创建位图文件
1{
2
3 函数名称: CreateBitmapFile
4
5 功能:创建位图文件
6
7 参数说明
8
9 FileName: 要生成的文件名
10 Width:位图宽度
11 Height:位图高度
12 ColorTableSize: 调色板数据长度
13 ColorBits:颜色位数
14 DataBits:位图数据存储区首地址
15 ColorTableData:调色板数据存储区首地址
16
17 返回值
18
19 创建成功返回 True,否则返回 False
20
21}
22
23function CreateBitmapFile(FileName:string;Width,Height,ColorTableSize:Integer;
24 ColorBits:Byte;DataBits,ColorTableData:Pointer):Boolean;
25var
26 fs: TFileStream;
27 bmpfh: TBitmapFileHeader;
28 bmpih: TBitmapInfoHeader;
29 i,remainder,bytesPerLine: integer;
30 align:DWORD;
31begin
32 Result:=False;
33 bmpfh.bfType := $4D42;
34 bmpfh.bfSize := Width * Height * ColorBits + SizeOf(TBitmapFileHeader) + SizeOf(TBitmapInfoHeader) + ColorTableSize;
35 bmpfh.bfReserved1 := 0;
36 bmpfh.bfReserved2 := 0;
37 bmpfh.bfOffBits := SizeOf(TBitmapFileHeader) +
38 SizeOf(TBitmapInfoHeader) + ColorTableSize;
39 FillChar(bmpih, SizeOf(bmpih), 0);
40 with bmpih do
41 begin
42 biSize := SizeOf(TBitmapInfoHeader);
43 biWidth := Width;
44 biHeight := Height;
45 biPlanes := 1;
46 biBitCount := ColorBits;
47 biCompression := BI_RGB;
48 biSizeImage := Width * Height * ColorBits div 8;
49 end;
50 fs := TFileStream.Create(FileName,fmCreate,GENERIC_READ or GENERIC_WRITE);
51 try
52 fs.Write(bmpfh, SizeOf(TBitmapFileHeader));
53 fs.Write(bmpih, SizeOf(bmpih));
54 if ColorTableSize>0 then
55 fs.Write(ColorTableData^, 1024);
56 bytesPerLine:=(Width*ColorBits) div 8 + Ord(not (Width*ColorBits) mod 8=0);
57 remainder:=bytesPerLine mod 4;
58 align:=0;
59 //bitmap data is organized upward and rightward,and bytes of each row must be multiple of 4
60 for i:=Height-1 downto 0 do
61 begin
62 fs.Write(Pointer(Cardinal(DataBits)+i*bytesPerLine)^, bytesPerLine);
63 if remainder>0 then
64 fs.Write(align,4-remainder);
65 end;
66 Result:=True;
67 finally
68 fs.Free;
69 end;
70end;
71
2
3 函数名称: CreateBitmapFile
4
5 功能:创建位图文件
6
7 参数说明
8
9 FileName: 要生成的文件名
10 Width:位图宽度
11 Height:位图高度
12 ColorTableSize: 调色板数据长度
13 ColorBits:颜色位数
14 DataBits:位图数据存储区首地址
15 ColorTableData:调色板数据存储区首地址
16
17 返回值
18
19 创建成功返回 True,否则返回 False
20
21}
22
23function CreateBitmapFile(FileName:string;Width,Height,ColorTableSize:Integer;
24 ColorBits:Byte;DataBits,ColorTableData:Pointer):Boolean;
25var
26 fs: TFileStream;
27 bmpfh: TBitmapFileHeader;
28 bmpih: TBitmapInfoHeader;
29 i,remainder,bytesPerLine: integer;
30 align:DWORD;
31begin
32 Result:=False;
33 bmpfh.bfType := $4D42;
34 bmpfh.bfSize := Width * Height * ColorBits + SizeOf(TBitmapFileHeader) + SizeOf(TBitmapInfoHeader) + ColorTableSize;
35 bmpfh.bfReserved1 := 0;
36 bmpfh.bfReserved2 := 0;
37 bmpfh.bfOffBits := SizeOf(TBitmapFileHeader) +
38 SizeOf(TBitmapInfoHeader) + ColorTableSize;
39 FillChar(bmpih, SizeOf(bmpih), 0);
40 with bmpih do
41 begin
42 biSize := SizeOf(TBitmapInfoHeader);
43 biWidth := Width;
44 biHeight := Height;
45 biPlanes := 1;
46 biBitCount := ColorBits;
47 biCompression := BI_RGB;
48 biSizeImage := Width * Height * ColorBits div 8;
49 end;
50 fs := TFileStream.Create(FileName,fmCreate,GENERIC_READ or GENERIC_WRITE);
51 try
52 fs.Write(bmpfh, SizeOf(TBitmapFileHeader));
53 fs.Write(bmpih, SizeOf(bmpih));
54 if ColorTableSize>0 then
55 fs.Write(ColorTableData^, 1024);
56 bytesPerLine:=(Width*ColorBits) div 8 + Ord(not (Width*ColorBits) mod 8=0);
57 remainder:=bytesPerLine mod 4;
58 align:=0;
59 //bitmap data is organized upward and rightward,and bytes of each row must be multiple of 4
60 for i:=Height-1 downto 0 do
61 begin
62 fs.Write(Pointer(Cardinal(DataBits)+i*bytesPerLine)^, bytesPerLine);
63 if remainder>0 then
64 fs.Write(align,4-remainder);
65 end;
66 Result:=True;
67 finally
68 fs.Free;
69 end;
70end;
71