【问题标题】:How I create bmp files (bitmaps) of a single color using delphi如何使用 delphi 创建单色的 bmp 文件(位图)
【发布时间】:2011-03-24 04:53:59
【问题描述】:

我需要一种在运行时创建 24 位位图(并保存到文件)的快速方法,指定宽度、高度和颜色

类似

procedure CreateBMP(Width,Height:Word;Color:TColor;AFile: string);

然后这样调用

CreateBMP(100,100,ClRed,'Red.bmp');

【问题讨论】:

    标签: delphi bmp


    【解决方案1】:

    您可以使用TBitmapCanvas属性,将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));
    【解决方案2】:

    您实际上不需要调用 FillRect。如果在设置宽度和高度之前设置 Brush.Color,则位图将对所有像素使用此颜色。我从未真正看到过这种行为的记录,因此它可能会在未来的版本中发生变化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多