I need a fast way to create 24 bits bitmaps (and save to a file) in runtime,specifing the Width , Height and color
something like
procedure CreateBMP(Width,Height:Word;Color:TColor;AFile: string);
and call like this
CreateBMP(100,100,ClRed,'Red.bmp');
you can use the Canvas
property of the TBitmap
, setting the Brush
to the color which you want to use, then call FillRect
function to fill the Bitmap.
try something like this :
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;
You don't actually need to call FillRect. If you set the Brush.Color before setting the width and height the bitmap will use this color for all the pixels. I've never actually seen this behavior documented so it may change in future versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With