【发布时间】:2011-03-24 04:53:59
【问题描述】:
我需要一种在运行时创建 24 位位图(并保存到文件)的快速方法,指定宽度、高度和颜色
类似
procedure CreateBMP(Width,Height:Word;Color:TColor;AFile: string);
然后这样调用
CreateBMP(100,100,ClRed,'Red.bmp');
【问题讨论】:
我需要一种在运行时创建 24 位位图(并保存到文件)的快速方法,指定宽度、高度和颜色
类似
procedure CreateBMP(Width,Height:Word;Color:TColor;AFile: string);
然后这样调用
CreateBMP(100,100,ClRed,'Red.bmp');
【问题讨论】:
您可以使用TBitmap的Canvas属性,将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;
【讨论】:
bmp.Canvas.FillRect(Rect(0,0,Height, Width)); 应该是bmp.Canvas.FillRect(Rect(0,0,Width, Height));
您实际上不需要调用 FillRect。如果在设置宽度和高度之前设置 Brush.Color,则位图将对所有像素使用此颜色。我从未真正看到过这种行为的记录,因此它可能会在未来的版本中发生变化。
【讨论】: