6

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

就像是

procedure CreateBMP(Width,Height:Word;Color:TColor;AFile: string);

像这样打电话

CreateBMP(100,100,ClRed,'Red.bmp');
4

2 回答 2

16

您可以使用 的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;
于 2011-03-24T04:58:15.267 回答
3

您实际上不需要调用 FillRect。如果在设置宽度和高度之前设置 Brush.Color,则位图将对所有像素使用此颜色。我从未真正看到过这种行为的记录,因此它可能会在未来的版本中发生变化。

于 2011-03-24T17:36:00.657 回答