ImageEn, unit iegdiplus

TIECanvas.DrawText

TIECanvas.DrawText


Declaration

procedure DrawText(Text: WideString; X, Y : Integer; Angle: Integer = 0; Alignment: TIEAlignment = iejLeft); overload;
procedure DrawText(Text: WideString; BoundingRect: TRect; Alignment: TIEAlignment = iejLeft); overload;
procedure DrawText(Text: WideString; BoundingRect: TRect; Angle: Integer; Alignment: TIEAlignment = iejLeft); overload;


Description

Draw text at a specified position (no wrapping) or within a rectangle (text may wrap).

Note:
Use TextStyling to add borders to text
For a more advanced text drawing, use AdvancedDrawText

GDI+ Method: GdipDrawString


Examples

// Yellow text without border
ImageEnView1.IEBitmap.IECanvas.Font.Size  := 54;
ImageEnView1.IEBitmap.IECanvas.Font.Style := [fsBold];
ImageEnView1.IEBitmap.IECanvas.Font.Color := clYellow;
ImageEnView1.IEBitmap.IECanvas.DrawText( 'ImageEn!', 280, 200 );
ImageEnView1.Update();




// Yellow text with red border
ImageEnView1.IEBitmap.IECanvas.Font.Size  := 54;
ImageEnView1.IEBitmap.IECanvas.Font.Style := [fsBold];
ImageEnView1.IEBitmap.IECanvas.Font.Color := clYellow;
ImageEnView1.IEBitmap.IECanvas.TextStyling.BorderWidth := 3;
ImageEnView1.IEBitmap.IECanvas.TextStyling.BorderColor := clRed;
ImageEnView1.IEBitmap.IECanvas.DrawText( 'ImageEn!', 280, 200 );
ImageEnView1.Update();




// Semi-transparent white text with red border
ImageEnView1.IEBitmap.IECanvas.Font.Size  := 54;
ImageEnView1.IEBitmap.IECanvas.Font.Style := [fsBold];
ImageEnView1.IEBitmap.IECanvas.Font.Color := clWhite;
ImageEnView1.IEBitmap.IECanvas.TextStyling.BorderWidth := 3;
ImageEnView1.IEBitmap.IECanvas.TextStyling.BorderColor := clRed;
ImageEnView1.IEBitmap.IECanvas.TextStyling.FillTransparency1 := IEOpacityToAlphaD( 0.3 ); // 30% opaque
ImageEnView1.IEBitmap.IECanvas.DrawText( 'ImageEn!', 280, 200 );
ImageEnView1.Update();




// Transparent text with rounded yellow border
ImageEnView1.IEBitmap.IECanvas.Font.Size  := 54;
ImageEnView1.IEBitmap.IECanvas.Font.Style := [fsBold];
ImageEnView1.IEBitmap.IECanvas.Font.Color := clWhite;
ImageEnView1.IEBitmap.IECanvas.TextStyling.BorderWidth := 3;
ImageEnView1.IEBitmap.IECanvas.TextStyling.BorderColor := clYellow;
ImageEnView1.IEBitmap.IECanvas.TextStyling.BorderLineJoin := ieljRound;
ImageEnView1.IEBitmap.IECanvas.TextStyling.FillTransparency1 := 0;  // 100% transparent
ImageEnView1.IEBitmap.IECanvas.DrawText( 'ImageEn!', 280, 200 );
ImageEnView1.Update();




// Gradient text with border
ImageEnView1.IEBitmap.IECanvas.Font.Size  := 54;
ImageEnView1.IEBitmap.IECanvas.Font.Style := [fsBold];
ImageEnView1.IEBitmap.IECanvas.Font.Color := clRed;
ImageEnView1.IEBitmap.IECanvas.TextStyling.BorderWidth := 3;
ImageEnView1.IEBitmap.IECanvas.TextStyling.BorderColor := clBlack;
ImageEnView1.IEBitmap.IECanvas.TextStyling.FillColor2 := clYellow;
ImageEnView1.IEBitmap.IECanvas.TextStyling.FillGradient := gpgVertical;
ImageEnView1.IEBitmap.IECanvas.DrawText( 'ImageEn!', 280, 200 );
ImageEnView1.Update();




// Centered gradient text without border
ImageEnView1.IEBitmap.IECanvas.Font.Size  := 54;
ImageEnView1.IEBitmap.IECanvas.Font.Style := [fsBold];
ImageEnView1.IEBitmap.IECanvas.Font.Color := clRed;
ImageEnView1.IEBitmap.IECanvas.TextStyling.BorderWidth := 0;
ImageEnView1.IEBitmap.IECanvas.TextStyling.FillColor2 := clWhite;
ImageEnView1.IEBitmap.IECanvas.TextStyling.FillGradient := gpgVertCenter;
ImageEnView1.IEBitmap.IECanvas.DrawText( 'ImageEn!', 280, 200 );
ImageEnView1.Update();




// Text with gradient ranging from 0% to 70% opacity
ImageEnView1.IEBitmap.IECanvas.Font.Size  := 54;
ImageEnView1.IEBitmap.IECanvas.Font.Style := [fsBold];
ImageEnView1.IEBitmap.IECanvas.TextStyling.BorderWidth := 0;
ImageEnView1.IEBitmap.IECanvas.TextStyling.FillTransparency1 := 0;                        // 100% transparent
ImageEnView1.IEBitmap.IECanvas.TextStyling.FillTransparency2 := IEOpacityToAlphaD( 0.7 ); // 70% opacity
ImageEnView1.IEBitmap.IECanvas.TextStyling.FillGradient := gpgHorizontal;
ImageEnView1.IEBitmap.IECanvas.TextStyling.FillColor2 := clYellow;
ImageEnView1.IEBitmap.IECanvas.DrawText( 'ImageEn!', 280, 200 );
ImageEnView1.Update();




// Method to draw text within an area of a TIEBitmap
procedure TextOutEx(aBMP: TIEBitmap;
                    X, Y, W, H : Integer; const Text : String;
                    const sFontName : String; iFontSize : Integer; cFontColor : TColor; Style : TFontStyles;
                    Angle : Integer = 0;
                    bAntiAlias : Boolean = true; bAutoEnlarge : Boolean = False);
var
  AnExtent: TSize;
  Mask: TIEMask;
  x1, y1, x2, y2: Integer;
  CompCanvas: TIECanvas;
  iAlpha: Integer;
begin
  aBMP.Canvas.Font.Name  := sFontName;
  aBMP.Canvas.Font.Size  := iFontSize;
  aBMP.Canvas.Font.Color := cFontColor;
  aBMP.Canvas.Font.Style := Style;

  AnExtent := aBMP.IECanvas.MeasureText( Text );

  if Angle <> 0 then
    AnExtent := IERotatePoint2( AnExtent.cx, AnExtent.cy, Angle );

  if bAutoEnlarge and (( aBMP.Width < AnExtent.cx ) or ( aBMP.Height < AnExtent.cy )) then
  begin
    iAlpha := 255;
    if aBMP.HasAlphaChannel then
      iAlpha := aBMP.Alpha[0, 0];
    aBMP.Resize( iMax( aBMP.Width, AnExtent.cx ), iMax( aBMP.Height, AnExtent.cy ), Background, iAlpha);
  end;

  CompCanvas := aBMP.CreateROICanvas(Rect(0, 0, aBMP.Width, aBMP.Height), bAntiAlias, true, bAntiAlias);
  try
    if not bAntiAlias then  // Cannot use together with alpha composting
      CompCanvas.TextRendering := ietrTextRenderingHintSingleBitPerPixel;

    CompCanvas.Brush.Color := aBMP.Canvas.Font.Color;
    CompCanvas.Font.Color  := aBMP.Canvas.Font.Color;
    CompCanvas.Font.Name   := aBMP.Canvas.Font.Name;
    CompCanvas.Font.Height := aBMP.Canvas.Font.Height;
    CompCanvas.Font.Style  := aBMP.Canvas.Font.Style;

    if ( W = 0 ) and ( H = 0 ) then
      CompCanvas.DrawText( Text, X, Y, -Angle )
    else
      CompCanvas.DrawText( Text, Rect( X, Y, X + W, Y + H ), -Angle );
  finally
    CompCanvas.Free();
  end;
end;