我如何使用delphi创建单色的bmp文件(位图)

Sal*_*dor 5 delphi bmp

我需要一种在运行时创建24位位图(并保存到文件)的快速方法,指定宽度,高度和颜色

就像是

procedure CreateBMP(Width,Height:Word;Color:TColor;AFile: string);
Run Code Online (Sandbox Code Playgroud)

并像这样打电话

CreateBMP(100,100,ClRed,'Red.bmp');
Run Code Online (Sandbox Code Playgroud)

RRU*_*RUZ 14

你可以使用的Canvas属性TBitmap,设置Brush你想要使用的颜色,然后调用FillRect函数来填充位图.

尝试这样的事情:

procedure CreateBitmapSolidColor(Width,Height:Word;Color:TColor;const FileName : TFileName);
var
 bmp : TBitmap;
begin
 bmp := TBitmap.Create;
 try
   bmp.PixelFormat := pf24bit;
   bmp.Width := Width;
   bmp.Height := Height;
   bmp.Canvas.Brush.Color := Color;
   bmp.Canvas.FillRect(Rect(0, 0, Width, Height));
   bmp.SaveToFile(FileName);
 finally
   bmp.Free;
 end;
end;
Run Code Online (Sandbox Code Playgroud)

  • `bmp.Canvas.FillRect(Rect(0,0,Height,Width));`应该是`bmp.Canvas.FillRect(Rect(0,0,Width,Height));` (3认同)