Can I "draw"/create an image with a given text with powershell?

icnivad picture icnivad · Jan 14, 2010 · Viewed 13.3k times · Source

I just wondered if it would be possible to create a small, simple jpg, png, gif with a given Text in powershell:

e.g: a small square, 250px × 61px, yellow background and black text on it: "Test"

Can I do this with "System.Drawing.Image"?

Thanks

Answer

Keith Hill picture Keith Hill · Jan 14, 2010

Sure, if you're on PowerShell 2.0 try this:

Add-Type -AssemblyName System.Drawing

$filename = "$home\foo.png" 
$bmp = new-object System.Drawing.Bitmap 250,61 
$font = new-object System.Drawing.Font Consolas,24 
$brushBg = [System.Drawing.Brushes]::Yellow 
$brushFg = [System.Drawing.Brushes]::Black 
$graphics = [System.Drawing.Graphics]::FromImage($bmp) 
$graphics.FillRectangle($brushBg,0,0,$bmp.Width,$bmp.Height) 
$graphics.DrawString('Hello World',$font,$brushFg,10,10) 
$graphics.Dispose() 
$bmp.Save($filename) 

Invoke-Item $filename