Did you know that you can use PowerShell to create images? While PowerShell is primarily known as a scripting language for managing systems, it also has the capability to work with graphics. In this blog post, I’ll show you how to create a custom New Year’s greeting complete with colorful fireworks using PowerShell.
What You’ll Need
To follow along, you’ll need:
- A Windows machine with PowerShell (version 5 or higher).
- A basic understanding of PowerShell scripting.
The Script: Bringing Fireworks to Life
Here’s the PowerShell script we’ll use to create a festive image:
# Greeting text
Add-Type -AssemblyName System.Drawing
$greeting = "Happy New Year 2025!"
# Colors for the firework text and explosions
$colors = @(
[Drawing.Color]::Red,
[Drawing.Color]::Yellow,
[Drawing.Color]::Green,
[Drawing.Color]::Cyan,
[Drawing.Color]::Blue,
[Drawing.Color]::Magenta
)
# Function to create an image
function New-FireworkImage {
param (
[Parameter(Mandatory)][string]$FilePath,
[Parameter(Mandatory)][string]$Greeting
)
# Create a bitmap
Add-Type -AssemblyName System.Drawing
$bitmap = New-Object System.Drawing.Bitmap 800, 600
$graphics = [Drawing.Graphics]::FromImage($bitmap)
$graphics.Clear([Drawing.Color]::Black)
# Define fonts and colors for the greeting
$fontFamily = "Comic Sans MS" # A font that looks more like handwriting
$font = New-Object System.Drawing.Font($fontFamily, 48, [Drawing.FontStyle]::Bold)
$brushColor = $colors | Get-Random
$textBrush = New-Object System.Drawing.SolidBrush $brushColor
# Add firework explosions
for ($i = 0; $i -lt 10; $i++) {
$x = Get-Random -Minimum 50 -Maximum 750
$y = Get-Random -Minimum 50 -Maximum 550
$size = Get-Random -Minimum 30 -Maximum 100
# Ensure the explosion color is not the same as the text color
do {
$explosionColor = $colors | Get-Random
} while ($explosionColor -eq $brushColor)
$brush = New-Object System.Drawing.SolidBrush $explosionColor
# Draw circles as firework explosions
$graphics.FillEllipse($brush, $x, $y, $size, $size)
}
# Calculate text size to center it
$textSize = $graphics.MeasureString($Greeting, $font)
$xPosition = (800 - $textSize.Width) / 2
$yPosition = (600 - $textSize.Height) / 2
# Draw the greeting text on the image
$graphics.DrawString($Greeting, $font, $textBrush, $xPosition, $yPosition)
# Save the image
$bitmap.Save($FilePath, [Drawing.Imaging.ImageFormat]::Jpeg)
# Release resources
$graphics.Dispose()
$bitmap.Dispose()
Write-Host "Image saved: $FilePath"
}
# Function to open the image in Paint
function Open-WithPaint {
param (
[Parameter(Mandatory)][string]$FilePath
)
Start-Process -FilePath "mspaint.exe" -ArgumentList $FilePath
}
# Determine the default directory for the file
$outputDirectory = [IO.Path]::Combine($env:USERPROFILE, "Pictures")
if (-not (Test-Path $outputDirectory)) {
New-Item -ItemType Directory -Path $outputDirectory | Out-Null
}
# Set the file path
$outputFilePath = [IO.Path]::Combine($outputDirectory, "FireworkGreeting.jpg")
# Create the image and open it in Paint
New-FireworkImage -FilePath $outputFilePath -Greeting $greeting
Open-WithPaint -FilePath $outputFilePath
How It Works
- Setting Up the Canvas: The script uses
System.Drawing
to create an 800×600 pixel black canvas. - Firework Explosions: Randomly placed, colorful circles are drawn on the canvas to simulate fireworks.
- Greeting Text: A custom message, “Happy New Year 2025!”, is drawn in the center using a handwritten-style font.
- Output: The image is saved as a
.jpg
file in yourPictures
directory and then opened in Paint for viewing.
Running the Script
Copy the script into a .ps1
file and execute it in PowerShell. Make sure to adjust your execution policy if required:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
After running the script, check your Pictures
folder for the generated image.
Why Use PowerShell for Graphics?
Creating images with PowerShell might not be the most common use case, but it showcases the language’s versatility. Whether for automation or fun projects, PowerShell offers creative ways to explore beyond system management.