-
Notifications
You must be signed in to change notification settings - Fork 2
/
imagify.ps1
51 lines (38 loc) · 3.14 KB
/
imagify.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# PARAMS
# 1. text to imagify
# 2. font name
# 3. font size
# 4. output
param( [Parameter(Mandatory=$True, Position=1)]
[string] $the_string,
[string] $font="Arial",
[float] $size=10.0,
[string] $out="out.png" )
$foreBrush = [System.Drawing.Brushes]::White
$backBrush = [System.Drawing.Brushes]::Black
[system.reflection.assembly]::loadWithPartialName('system') | out-null
[system.reflection.assembly]::loadWithPartialName('system.drawing') | out-null
[system.reflection.assembly]::loadWithPartialName('system.drawing.imaging') | out-null
[system.reflection.assembly]::loadWithPartialName('system.windows.forms') | out-null
# STUFF
# Create font
$nFont = new-object system.drawing.font($font, $size)
# Get size of text $the-string drawn with font $nFont
$sz = [system.windows.forms.textrenderer]::MeasureText($the_string, $nFont)
# Create Bitmap
$bm = new-object system.drawing.bitmap($sz.width, $sz.height)
# Create Graphics
$gr = [System.Drawing.Graphics]::FromImage($bm)
# Paint image's background
$rect = new-object system.drawing.rectanglef(0, 0, $sz.width, $sz.height)
$gr.FillRectangle($backBrush, $rect)
# Draw string, centered
$strFrmt = new-object system.drawing.stringformat
$strFrmt.Alignment = [system.drawing.StringAlignment]::Center
$strFrmt.LineAlignment = [system.drawing.StringAlignment]::Center
$gr.DrawString($the_string, $nFont, $foreBrush, $rect, $strFrmt)
# Close Graphics
$gr.Dispose();
# Save and close Bitmap
$bm.Save($out, [system.drawing.imaging.imageformat]::Png);
$bm.Dispose();