{
函数名称: CreateBitmapFile
功能:创建位图文件
参数说明
FileName: 要生成的文件名
Width:位图宽度
Height:位图高度
ColorTableSize: 调色板数据长度
ColorBits:颜色位数
DataBits:位图数据存储区首地址
ColorTableData:调色板数据存储区首地址
返回值
创建成功返回 True,否则返回 False
}
function CreateBitmapFile(FileName:string;Width,Height,ColorTableSize:Integer;
ColorBits:Byte;DataBits,ColorTableData:Pointer):Boolean;
var
fs: TFileStream;
bmpfh: TBitmapFileHeader;
bmpih: TBitmapInfoHeader;
i,remainder,bytesPerLine: integer;
align:DWORD;
begin
Result:=False;
bmpfh.bfType := $4D42;
bmpfh.bfSize := Width * Height * ColorBits + SizeOf(TBitmapFileHeader) + SizeOf(TBitmapInfoHeader) + ColorTableSize;
bmpfh.bfReserved1 := 0;
bmpfh.bfReserved2 := 0;
bmpfh.bfOffBits := SizeOf(TBitmapFileHeader) +
SizeOf(TBitmapInfoHeader) + ColorTableSize;
FillChar(bmpih, SizeOf(bmpih), 0);
with bmpih do
begin
biSize := SizeOf(TBitmapInfoHeader);
biWidth := Width;
biHeight := Height;
biPlanes := 1;
biBitCount := ColorBits;
biCompression := BI_RGB;
biSizeImage := Width * Height * ColorBits div 8;
end;
fs := TFileStream.Create(FileName,fmCreate,GENERIC_READ or GENERIC_WRITE);
try
fs.Write(bmpfh, SizeOf(TBitmapFileHeader));
fs.Write(bmpih, SizeOf(bmpih));
if ColorTableSize>0 then
fs.Write(ColorTableData^, 1024);
bytesPerLine:=(Width*ColorBits) div 8 + Ord(not (Width*ColorBits) mod 8=0);
remainder:=bytesPerLine mod 4;
align:=0;
//bitmap data is organized upward and rightward,and bytes of each row must be multiple of 4
for i:=Height-1 downto 0 do
begin
fs.Write(Pointer(Cardinal(DataBits)+i*bytesPerLine)^, bytesPerLine);
if remainder>0 then
fs.Write(align,4-remainder);
end;
Result:=True;
finally
fs.Free;
end;
end;