-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathText2Image.psm1
163 lines (144 loc) · 6.32 KB
/
Text2Image.psm1
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<#
.Synopsis
Convert Text to Image. Automate your screenshots.
.DESCRIPTION
Convert Text to Image. Redirect your output into variable/file and then convert it to image. With our preset styles easily automate your screenshot capturing routine for your blog, twitter, etc.
Image formats supported: Png, Bmp, Gif, Jpeg, Tiff.
.EXAMPLE
PS C:\> t2i -ImageText "`r`nTesting Text2Image Powershell Module`r`n" -ImageStyle PuTTY -Verbose
VERBOSE: Performing the operation "New-Image" on target "'
Testing Text2Image Powershell Module
'. Using PuTTY style".
#>
function New-Image
{
[CmdletBinding(SupportsShouldProcess=$true,
PositionalBinding=$false,
ConfirmImpact='Medium')]
[Alias('t2i')]
Param
(
# Text which should be converted to Image
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false,
Position=0)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[Alias("Text")]
[string]
$ImageText,
# Generated Image Style
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false)]
[ValidateNotNull()]
[ValidateSet("PowerShell","CMD","PuTTY","LinuxTerminal")]
[Alias("Style")]
$ImageStyle="PowerShell",
# New Image Format
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false)]
[ValidateSet("Png", "Bmp", "Gif", "Jpeg", "Tiff")]
$ImageFormat="Png",
# New Image Output Path. Default to Current Lcoation
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false)]
[Alias("path")]
$OutputPath,
# New Image Name
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false)]
[Alias("name","ImageName")]
$NewImageName="NewImage"
)
Begin
{
}
Process
{
if ($pscmdlet.ShouldProcess("'$($ImageText)'. Using $ImageStyle style"))
{
try
{
switch ($ImageStyle)
{
'PowerShell' {
$ImageStyleObjProps=@{
FontName="Lucida Console"
FontSize=9
TextColor=[System.Drawing.Brushes]::White
BackgroundColor=[System.Drawing.Color]::FromArgb(1,36,86)
}
break
}
'CMD' {
$ImageStyleObjProps=@{
FontName="Consolas"
FontSize=10
TextColor=[System.Drawing.Brushes]::LightGray
BackgroundColor=[System.Drawing.Color]::FromArgb(12,12,12)
}
break
}
'PuTTY' {
$ImageStyleObjProps=@{
FontName="Courier New"
FontSize=10
TextColor=[System.Drawing.Brushes]::LightGray
BackgroundColor=[System.Drawing.Color]::FromArgb(0,0,0)
}
break
}
'LinuxTerminal' {
$ImageStyleObjProps=@{
FontName="Terminus Font"
FontSize=9
TextColor=[System.Drawing.Brushes]::White
BackgroundColor=[System.Drawing.Color]::FromArgb(0,0,0)
}
break
}
Default {}
}
$ImageStyleObj=New-Object -TypeName psobject -Property $ImageStyleObjProps
$Format=[System.Drawing.Imaging.ImageFormat]::$ImageFormat
$FontObj=New-Object System.Drawing.Font $ImageStyleObj.FontName,$ImageStyleObj.FontSize
$BitmapObj=New-Object System.Drawing.Bitmap 1,1
$GraphicsObj=[System.Drawing.Graphics]::FromImage($BitmapObj)
$StringSize=$GraphicsObj.MeasureString($ImageText, $FontObj)
$BitmapObj=New-Object System.Drawing.Bitmap $([int]$StringSize.Width),$([int]$StringSize.Height)
$GraphicsObj=[System.Drawing.Graphics]::FromImage($BitmapObj)
$GraphicsObj.CompositingQuality=[System.Drawing.Drawing2D.CompositingQuality]::HighQuality
$GraphicsObj.InterpolationMode=[System.Drawing.Drawing2D.InterpolationMode]::HighQualityBilinear
$GraphicsObj.PixelOffsetMode=[System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality
$GraphicsObj.SmoothingMode=[System.Drawing.Drawing2D.SmoothingMode]::HighQuality
$GraphicsObj.Clear($ImageStyleObj.BackgroundColor)
$GraphicsObj.DrawString($ImageText, $FontObj, $ImageStyleObj.TextColor, 0, 0)
$FontObj.Dispose()
$GraphicsObj.Flush()
$GraphicsObj.Dispose()
if($OutputPath -eq $null){
$OutputPath=Get-Location
$OutputPath=$OutputPath.Path.Replace("\","\\")+"\\"
}else{
$OutputPath=$OutputPath.Replace("\","\\")+"\\"
}
$ImageFileName="$OutputPath"+"$NewImageName.$($ImageFormat.ToLower())"
$BitmapObj.Save("$ImageFileName", $Format);
}
catch
{
Write-Verbose "Failed - please review exception message for more details"
Write-Output ("Catched Exception: - $($_.exception.message)")
}
}
}
End
{
}
}