Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 942
icnivad Avatar asked Jan 14 '10 22:01

icnivad


1 Answers

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  
like image 144
Keith Hill Avatar answered Sep 22 '22 17:09

Keith Hill