您现在的位置:首页 >> 图形媒体 >> 图形媒体 >> 内容

Delphi中直接创建位图文件CreateBitmapFile

时间:2011/9/3 15:00:16 点击:

  核心提示:{ 函数名称: CreateBitmapFile 功能:创建位图文件 参数说明 FileName: 要生成的文件名 Width:位图宽度 Height:位图高度 ColorTableSize: 调色板...

{

      函数名称: 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;

作者:网络 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
本类推荐
  • 没有
本类固顶
  • 没有
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号