This script is an update to the Random Geometric Patterns Image Maker script. The original script had 12 patterns while the new version has 33 different pattern/fill combos. There are new patterns like Points, Squares, Stars and Triangles. The patterns now have a variety of fill types including Solid, Gradient and Hatch.

Here are some examples starting with Ellipses filled with (Skewed) Gradient colors.


These are Pie Shapes filled with Hatches.


Here are Squares filled with a Gradient.


These are Random Stars.


And these are Random Triangles with thick lines on a transparent background.


Lastly are Polygons with a Gradient fill.


This script interface and core functions are the same as in the original script, just the pattern options available are expanded. The only other changes are expanding the output image size options and changing that size text parser to split instead of substring for more flexibility. Also the Squares and Rectangles max size is set to 25% of the image size instead of being fixed. I’ll only describe using the fills and the new patterns which are the major changes in version 2.

The Hatch fills use one of 53 available Hatch Styles. I start by filling the array $hatchStyle with all the Styles in Drawing.Drawing2D.HatchStyle.

# Select Hatch Style
$maxHatchStyle = 53
$hatchStyle = @()
For ($i = [Convert]::ToInt32([Drawing.Drawing2D.HatchStyle]::Min); $i -lt $maxHatchStyle; $i++) {
    $hatchStyle += ([Drawing.Drawing2D.HatchStyle] $i)

The Hatch Styles are used in the HatchBrush Class. I setup the Brush with a random style and random colors. The Brush is then used in the Fill calls, like $bitmapGraphics.FillEllipse.

# Setup Brush
$brush = New-Object System.Drawing.Drawing2D.HatchBrush( `
[System.Drawing.Drawing2D.HatchStyle] $hatchStyle[(Get-Random -minimum 0 -maximum 52)], `
[System.Drawing.Color]::FromArgb((Get-Random -minimum 0 -maximum 255),(Get-Random -minimum 0 -maximum 255),(Get-Random -minimum 0 -maximum 255)), `
[System.Drawing.Color]::FromArgb((Get-Random -minimum 0 -maximum 255),(Get-Random -minimum 0 -maximum 255),(Get-Random -minimum 0 -maximum 255)))
# Draw the Ellipse
$bitmapGraphics.FillEllipse($brush, $upperLeftX, $upperLeftY, $width, $height)

Gradient Fills use the Drawing.Drawing2D.LinearGradientBrush Brush. The Brush uses a Start and End Point of the linear gradient. The Start Point is the upper left corner and the End Point is random. Also defined are the Starting and Ending colors, which are randomized.

# Setup Brush
$pointStart = New-Object System.Drawing.Point
$pointStart.X = $upperLeftX
$pointStart.Y = $upperLeftY
$pointStop = New-Object System.Drawing.Point
$pointStop.X = $random1
$pointStop.Y = $random2
$brush = New-Object System.Drawing.Drawing2D.LinearGradientBrush($pointStart, $pointStop,  `
    [System.Drawing.Color]::FromArgb((Get-Random -minimum 0 -maximum 255),(Get-Random -minimum 0 -maximum 255),(Get-Random -minimum 0 -maximum 255)), `
    [System.Drawing.Color]::FromArgb((Get-Random -minimum 0 -maximum 255),(Get-Random -minimum 0 -maximum 255),(Get-Random -minimum 0 -maximum 255)))
# Draw the Ellipse
$bitmapGraphics.FillEllipse($brush, $upperLeftX, $upperLeftY, $width, $height)

The Solid Fills use a Brush defined with three colors, which are random.

$red   = (Get-Random -minimum 0 -maximum 255)
$green = (Get-Random -minimum 0 -maximum 255)
$blue  = (Get-Random -minimum 0 -maximum 255)

# Setup Brush
$brushColor = [System.Drawing.Color]::FromArgb($red, $green, $blue)
$brush = New-Object System.Drawing.SolidBrush $brushColor

Stars use some trigonometry to calculate the points which form the Star. I got the base algorithm from a blog discussion about drawing a 5 pointed star in Java.

# Randomize Position and Define the Stars Points
$x = (Get-Random -minimum -100 -maximum $global:imageWidth)
$y = (Get-Random -minimum -100 -maximum $global:imageHeight)
$width = (Get-Random -minimum 0 -maximum 100)
$height = $width
$ax = [int](($width/2) + $x)
$ay = [int]$y
$bx = [int]($width+$x)
$by = [int](.374*[double]($height)+[double]($y))
$cx = [int](.825*[double]($width)+[double]($x))
$cy = [int]($height+$y)
$dx = [int](.175*[double]($height)+[double]($x))
$dy = [int]($height+$y)
$ex = [int]$x
$ey = [int]$by
# Draw the Star
$bitmapGraphics.DrawLine($pen, $ax, $ay, $cx, $cy)
$bitmapGraphics.DrawLine($pen, $bx, $by, $dx, $dy)
$bitmapGraphics.DrawLine($pen, $cx, $cy, $ex, $ey)
$bitmapGraphics.DrawLine($pen, $dx, $dy, $ax, $ay)
$bitmapGraphics.DrawLine($pen, $ex, $ey, $bx, $by)

Triangles are drawn by defining  three random points and drawing a line from the first point to the second, then the third and back to the first.

# Randomize Position and Define the Triangle Points
$startX = (Get-Random -minimum 0 -maximum $global:imageWidth)
$startY = (Get-Random -minimum 0 -maximum $global:imageHeight)
$secondX = (Get-Random -minimum 0 -maximum $global:imageWidth)
$secondY = (Get-Random -minimum 0 -maximum $global:imageHeight)
$thirdX = (Get-Random -minimum 0 -maximum $global:imageWidth)
$thirdY = (Get-Random -minimum 0 -maximum $global:imageHeight)
$saveX = $startX
$saveY = $startY
# Draw the Triangle
$bitmapGraphics.DrawLine($pen, $startX, $startY, $secondX, $secondY)
$bitmapGraphics.DrawLine($pen, $secondX, $secondY, $thirdX, $thirdY)
$bitmapGraphics.DrawLine($pen, $thirdX, $thirdY, $saveX, $saveY)


Download the complete script here – GeometricPatterns2.ps1