TIECanvas is a wrapper for GDI+ and TCanvas, and supports anti-aliasing, alpha merging, and advanced drawing methods.
It is available via TIEBitmap.IECanvas or by creating it from a TBitmap or TCanvas.
// Fill image with a vertical gradient from yellow to red ImageEnView1.IEBitmap.IECanvas.GradientFillRect( Rect( 0, 0, ImageEnView1.IEBitmap.Width, ImageEnView1.IEBitmap.Height ), clYellow, clRed, gpgVertical ); ImageEnView1.Update();
// Draw an envelope with 50% transparency with ImageEnView1.IEBitmap.IECanvas do begin Pen.Mode := pmCopy; Pen.Style := psSolid; Pen.Color := clBlack; Pen.Transparency := 128;
// Draw a semi-transparent text box onto a bitmap const Horz_Margin = 8; Vert_Margin = 3; Center_Text = False; var x, y: integer; tw, rw, rh: integer; iec: TIECanvas; ss: string; begin ss := 'This is my text'; x := 100; y := 100;
for i := 0 to ptCount - 1 do begin currX := arPts[ i ].x; currY := arPts[ i ].y;
drawPts[i].x := Left + BorderWidth div 2 + round( currX / 1000 * exWidth ); drawPts[i].y := Top + BorderWidth div 2 + round( currY / 1000 * exHeight ); end;
// PAINT TO CANVAS if AntiAlias then Canvas.SmoothingMode := iesmAntialias else Canvas.SmoothingMode := iesmBestPerformance;
with Canvas do begin if BorderWidth > 0 then begin Pen.Style := psSolid; Pen.Width := BorderWidth; Pen.Color := BorderColor; end else Pen.Style := psClear; Pen.Mode := pmCopy; Pen.LineJoin := ieljRound;
if FillColor = clNone then Brush.Style := bsClear else begin Brush.Color := FillColor; Brush.Style := bsSolid; end;
if FillColor2 = clNone then Brush.SetGradient( gpgNone, 0, 0 ) else begin Brush.SetGradient( FillGradient, Width, Height ); Brush.BackTransparency := 255; Brush.BackColor := FillColor2; end;
Polygon( slice( drawpts, ptCount )); end; end;
// Method to draw a styled ellipse to a canvas procedure IEDrawSimpleShape(Canvas: TIECanvas; Left, Top, Width, Height: Integer; BorderColor: TColor; BorderWidth: Integer; FillColor: TColor; FillColor2: TColor = clNone; FillGradient: TIEGDIPlusGradient = gpgVertical; AntiAlias: Boolean = True); const Rect_Rounding = 20; var drawGradient: Boolean; begin drawGradient := ( FillGradient <> gpgNone ) and ( FillColor2 <> FillColor ) and ( FillColor <> clNone ) and ( FillColor2 <> clNone );
if AntiAlias then Canvas.SmoothingMode := iesmAntialias else Canvas.SmoothingMode := iesmBestPerformance;
// Border if ( BorderColor = clNone ) or ( BorderWidth < 1 ) then Canvas.Pen.Style := psClear else Canvas.Pen.Style := psSolid; Canvas.Pen.Color := BorderColor; Canvas.Pen.Width := BorderWidth;
// Fill if ( FillColor = clNone ) then Canvas.Brush.Style := bsClear else Canvas.Brush.Style := bsSolid; Canvas.Brush.Color := FillColor;
inc( Left , BorderWidth div 2 ); inc( Top , BorderWidth div 2 ); dec( Width , BorderWidth ); dec( Height, BorderWidth );
if drawGradient = False then Canvas.Brush.SetGradient( gpgNone, 0, 0 ) else begin Canvas.Brush.SetGradient( FillGradient, Width, Height ); Canvas.Brush.BackTransparency := 255; Canvas.Brush.BackColor := FillColor2; end;
Canvas.Ellipse( Left, Top, Left + Width, Top + Height ); end;
// 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;
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;
// Draw a filled 5-pointed star var pp: array[0..10] of TPoint; begin ImageEnView1.IEBitmap.IECanvas.Pen.Color := clOrangeRed; ImageEnView1.IEBitmap.IECanvas.Pen.Width := 3; ImageEnView1.IEBitmap.IECanvas.Brush.Color := clYellow;