Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

GMImage.DrawRectangle

Draws a rectangle.

Component Version macOS Windows Linux Server iOS SDK
GraphicsMagick 3.0 ✅ Yes ✅ Yes ✅ Yes ✅ Yes ✅ Yes
MBS( "GMImage.DrawRectangle"; ImageRef; upperLeftX; upperLeftY; lowerRightX; lowerRightY )   More

Parameters

Parameter Description Example
ImageRef The image reference number. 1
upperLeftX Upper left X position.
upperLeftY Upper left Y position.
lowerRightX Lower right X position.
lowerRightY Lower right Y position.

Result

Returns OK or error message.

Description

Draws a rectangle.
Draw a rectangle using stroke color and thickness from upper-left coordinates to lower-right coordinates.
If a fill color is specified, then the object is filled.

If you have an open path, the command is added there for later drawing with GMImage.DrawPath.

Examples

Creates a new image and draws a rectangle inside:

Set Variable [ $img; Value:MBS("GMImage.New"; "300x200"; "RGB 1 1 1") ]
Set Variable [ $r; Value:MBS("GMImage.SetStrokeColor"; $img; "RGB 1 0 0") ]
Set Variable [ $r; Value:MBS("GMImage.SetFillColor"; $img; "RGB 0 0 1") ]
Set Variable [ $r; Value:MBS("GMImage.SetLineWidth"; $img; 5) ]
Set Variable [ $r; Value:MBS("GMImage.DrawRectangle"; $img; 50; 50; 250; 150) ]
Set Variable [ $png; Value:MBS("GMImage.WriteToPNGContainer"; $img; "test.png") ]
Set Variable [ $r; Value:MBS("GMImage.Release"; $img) ]
Set Field [ Drawing::Image; $png ]

Draws a cirlce in a rectangle in a circle:

Set Variable [$img; Value:MBS( "GMImage.New"; "100x100"; "white" )]
Set Variable [$r; Value:MBS( "GMImage.SetFillColor"; $img; "RGB 1 0 0" )]
Set Variable [$r; Value:MBS( "GMImage.SetStrokeColor"; $img; "RGB 0 0 1" )]
Set Variable [$r; Value:MBS( "GMImage.DrawCircle"; $img; 50; 50; 50; 90 )]
Set Variable [$r; Value:MBS( "GMImage.DrawRectangle"; $img; 22; 22; 100-22; 100-22 )]
Set Variable [$r; Value:MBS( "GMImage.DrawCircle"; $img; 50; 50; 50; 100-22 )]
Set Field [test::test; MBS( "GMImage.WriteToPNGContainer"; $img; "test.png" )]
Set Variable [$r; Value:MBS( "GMImage.Release"; $img)]

Draw something and write to file on desktop:

# New Image
Set Variable [$img; Value:MBS("GMImage.New"; "300x200"; "RGB 1 1 1")]
# Set colors and line width
Set Variable [$r; Value:MBS("GMImage.SetStrokeColor"; $img; "RGB 1 0 0")]
Set Variable [$r; Value:MBS("GMImage.SetFillColor"; $img; "RGB 0 0 1")]
Set Variable [$r; Value:MBS("GMImage.SetLineWidth"; $img; 5)]
# draw a rectangle
Set Variable [$r; Value:MBS("GMImage.DrawRectangle"; $img; 50; 50; 250; 150)]
# save to test.png on desktop:
Set Variable [$DesktopPath; Value:MBS( "Folders.UserDesktop" )]
Set Variable [$path; Value:MBS( "Path.AddPathComponent"; $DesktopPath; "test.png")]
Set Variable [$r; Value:MBS("GMImage.WriteToFile"; $img; $path)]
# cleanup
Set Variable [$r; Value:MBS("GMImage.Release"; $img)]

Draw bar charts:

Set Field [ MyChart::Chart ;

Let ( [

    cities = List ( "Hamburg" ; "Berlin" ; "Munich" ; "Cologne" ) ;
    values = List ( 1800000 ; 3700000 ; 1500000 ; 1100000 ) ;

    colors = List ( "#4e79a7" ; "#f28e2b" ; "#e15759" ; "#76b7b2" ; "#59a14f" ) ;

    img = MBS( "GMImage.New"; "600x400"; "white" ) ;

    maxValue = Max ( 1800000 ; 3700000 ; 1500000 ; 1100000 ) ;

    r = MBS( "GMImage.SetStrokeColor"; img; "black" ) ;
    r = MBS( "GMImage.DrawLine"; img; 50; 350; 550; 350 ) ; // X-axis
    r = MBS( "GMImage.DrawLine"; img; 50; 50; 50; 350 ) ; // Y-axis

    barWidth = 80 ;
    spacing = 30 ;
    baseX = 70 ;

    r = Let ( [
            count = ValueCount ( cities ) ;
            colorCount = ValueCount ( colors ) ;
            r = While (
                    [ i = 1 ] ; i ≤ count ;
                    [
                        city = GetValue ( cities ; i ) ;
                        val = GetValue ( values ; i ) ;
                        color = GetValue ( colors ; Mod ( i - 1 ; colorCount ) + 1 ) ;

                        height = (val / maxValue) * 300 ;
                        x1 = baseX + (i - 1) * (barWidth + spacing) ;
                        x2 = x1 + barWidth ;
                        y1 = 350 - height ;
                        y2 = 350 ;

                        r = MBS( "GMImage.SetFillColor"; img; color ) ;
                        r = MBS( "GMImage.DrawRectangle"; img; x1; y1; x2; y2 ) ;
                        r = MBS( "GMImage.SetFontPointSize"; img; 16 );
                       
                        geometry = barWidth & "x20+" & x1 & "+360" ;
                        r = MBS( "GMImage.Annotate"; img; city; geometry; 5 ) ; // 5 = CenterGravity

                        i = i + 1
                    ] ; r
              )
        ] ; r ) ;

    result = MBS( "GMImage.WriteToPNGContainer"; img; "test.png" );
    r = MBS("GMImage.Free"; img)
   
] ; result ) ]

See also

Example Databases

Blog Entries

This function checks for a license.

Created 18th August 2014, last changed 4th September 2025


GMImage.DrawPath - GMImage.DrawRoundRectangle