Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I create bmp files (bitmaps) of a single color using delphi

Tags:

delphi

bmp

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');
like image 893
Salvador Avatar asked Mar 24 '11 04:03

Salvador


2 Answers

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;
like image 52
RRUZ Avatar answered Nov 07 '22 10:11

RRUZ


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.

like image 33
Mitch Avatar answered Nov 07 '22 09:11

Mitch