我需要一种在运行时创建 24 位位图(并保存到文件)的快速方法,指定宽度、高度和颜色
就像是
procedure CreateBMP(Width,Height:Word;Color:TColor;AFile: string);
像这样打电话
CreateBMP(100,100,ClRed,'Red.bmp');
您可以使用 的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;
您实际上不需要调用 FillRect。如果在设置宽度和高度之前设置 Brush.Color,则位图将对所有像素使用此颜色。我从未真正看到过这种行为的记录,因此它可能会在未来的版本中发生变化。