【问题标题】:How to draw a solid color bitmap with a Text Centered?如何绘制以文本为中心的纯色位图?
【发布时间】:2014-08-21 05:24:12
【问题描述】:

下面的代码应该创建一个 48x48 矩形的位图,蓝色背景色和一个水平和垂直居中的白色文本(实际上只是一个字母)。

但是什么也没发生。

procedure MakeCustomIcon(AText: string; AWidth: Integer; AHeight: Integer; AColor: TAlphaColor; var ABlob: TBlob);
var
  Bitmap: TBitmap;
  Rect: TRectF;
  InStream: TMemoryStream;
begin
  Bitmap := TBitmap.Create;
  InStream := TMemoryStream.Create;
  try
    Bitmap.SetSize(AWidth, AHeight);

    Bitmap.Canvas.Clear(AColor);

    Bitmap.Canvas.Stroke.Kind := TBrushKind.bkSolid;
    Bitmap.Canvas.StrokeThickness := 1;
    Bitmap.Canvas.Fill.Color := TAlphaColorRec.White;
    Bitmap.Canvas.BeginScene;

    Rect.Create(0, 0, AWidth, AHeight);
    Bitmap.Canvas.FillText(Rect, AText, true, 100, [TFillTextFlag.ftRightToLeft], TTextAlign.taCenter, TTextAlign.taCenter);

    Bitmap.Canvas.EndScene;

    Bitmap.SaveToStream(InStream);

    InStream.Position := 0;

    ABlob.Clear;
    ABlob.LoadFromStream(InStream);
  finally
    Bitmap.Free;
    InStream.Free;
  end;

我已经测试了我的程序的其余部分,以确保图像(那个 Blob)实际上正在传输和显示,并且它正在这样做。问题完全包含在上面的方法中绘制位图的方式上。

这个 TBlob 是一个字节数组。

我希望在 TListView 中使用如下所示的矩形:

【问题讨论】:

    标签: delphi bitmap firemonkey delphi-xe6


    【解决方案1】:

    我已经准备了一个项目。

    1-) 在 TImage 上写入文本

    2-) 在 TImage 上绘制

    3-) 对 TImage 的影响

    我试试 XE5

    样品:

    procedure ReDraw(Image: TImage);
    var
      MyRect: TRectF;
    begin
      if Image.Bitmap.IsEmpty then Exit;
    
      MyRect := TRectF.Create(0, Ozellik.SeritTop, Image.Bitmap.Width, Ozellik.SeritBot);
      with Image.Bitmap.Canvas do
      begin
        BeginScene;
        if not Seffaf.IsChecked then
          Fill.Color := Ozellik.SeritRenk
        else
          Fill.Color := TAlphaColorRec.Null;
        FillRect(MyRect, 0, 0, [], 1);
        Fill.Color := Ozellik.YaziRenk;
        if FontCombo.ItemIndex <> -1 then
          Font.Family := FontCombo.Items[FontCombo.ItemIndex];
        Font.Size := Ozellik.YaziBoyut;
        FillText(MyRect,FonYazi.Text.Trim,True,1,[],TTextAlign.taCenter,TTextAlign.taCenter);
        EndScene;
      end;
      Image.Repaint;
    end;
    

    http://www.dosya.tc/server32/vHsbaC/CapsYapMasa_st_.rar.html

    【讨论】:

    • 我认为显示代码比显示可疑链接更有帮助。
    • 你说得对,但文章是一个整体。我如何分开写作。
    【解决方案2】:

    所有画布绘图必须分组到 BeginScene/EndScene 块中。另外,建议在 try-finally 块内绘制。

    所以,而不是

    Bitmap.Canvas.Clear(AColor);
    ...
    Bitmap.Canvas.BeginScene;
    ...
    Bitmap.Canvas.EndScene;
    

    你应该这样做:

    Bitmap.Canvas.BeginScene;
    try
      Bitmap.Canvas.Clear(AColor);
      ...
    finally
      Bitmap.Canvas.EndScene;
    end; 
    

    -- 问候

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多