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

2

7

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');
Adamandeve answered 24/3, 2011 at 4:53 Comment(0)
R
17

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;
Rodrigues answered 24/3, 2011 at 4:58 Comment(2)
Just what I was going to suggest. I've used this technique to have an owner draw combo box with colour names and a small image.Circumference
bmp.Canvas.FillRect(Rect(0,0,Height, Width)); should be bmp.Canvas.FillRect(Rect(0,0,Width, Height));Maurizia
H
4

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.

Handling answered 24/3, 2011 at 17:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.