-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ps1
107 lines (89 loc) · 3.54 KB
/
index.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
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
param(
[string]$PicturePath,
[string]$RecipeName
)
if (!$PicturePath) {
"`nSorry, no picture was given to me.`n"
"Usage:`n$PSCommandPath [-PicturePath] full_path_to_image_file`n"
break
}
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
function display([System.Drawing.Image]$img) {
$w_form = 400 # form width
$h_form = 250 # form height
$b = [int]$img.Size.Width
$h = [int]$img.Size.Height
$b_thumb = $b
$h_thumb = $h
# portrait picture higher than 500px or landscape picture wider than 700px?
if ( ($b -gt $h) -and ($b -gt 700) ) {
$b_thumb = 700
$h_thumb = [int](700 * $h / $b)
$imgthumb = $img.GetThumbnailImage($b_thumb, $h_thumb, $null, 0) # create bitmap with 700px width
}
elseif ( ($b -le $h) -and ($h -gt 500) ) {
$b_thumb = [int](500 * $b / $h)
$h_thumb = 500
$imgthumb = $img.GetThumbnailImage($b_thumb, $h_thumb, $null, 0) # create bitmap with 500px height
}
else {
$imgthumb=$img
}
$form = New-Object Windows.Forms.Form
# $form.Text = "Picture"
$form.Text = $RecipeName
# $form.Size = New-Object System.Drawing.Size($w_form,$h_form) # minimal size
$form.Size = New-Object System.Drawing.Size($b,$h) # minimal size
$form.StartPosition = "CenterScreen"
# $form.AutoSize = $True
$form.AutoSize = $True
$form.AutoSizeMode = "GrowOnly" # or "GrowAndShrink"
$form.Topmost = $True
$font_normal = New-Object System.Drawing.Font("Tahoma",13,[Drawing.FontStyle]::Regular)
$font_bold = New-Object System.Drawing.Font("Tahoma",16,[Drawing.FontStyle]::Bold)
$PictureBox = New-Object Windows.Forms.PictureBox
# $PictureBox.Location = New-Object System.Drawing.Point(0,0)
$PictureBox.Location = New-Object System.Drawing.Point(0,0)
$PictureBox.Size = New-Object System.Drawing.Size($b_thumb, $h_thumb)
$PictureBox.Image = $imgthumb;
$form.Controls.Add($PictureBox)
$LabelDescription = New-Object Windows.Forms.Label
$LabelDescription.Location = New-Object System.Drawing.Point(5,5)
#$LabelDescription.Size = New-Object System.Drawing.Size(375,25)
$LabelDescription.Font = $font_bold;
# $LabelDescription.Text = "original size: $b x $h, display size: $b_thumb x $h_thumb"
$LabelDescription.AutoSize = $True
$form.Controls.Add($LabelDescription)
# $OKButton = New-Object System.Windows.Forms.Button
# $OKButton.Size = New-Object System.Drawing.Size(75,45)
# # OKButton centered under the picture
# $OKButton.Location = New-Object System.Drawing.Point( (($form.Size.Width - $OKButton.Size.Width) / 2),(50+$h_thumb) )
# $OKButton.Text = "OK"
# $OKButton.Font = $font_bold
# $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
# $form.Controls.Add($OKButton)
# $form.AcceptButton = $OKButton
return $form.ShowDialog()
}
# read image file to byte array
try {
$img = [System.IO.File]::ReadAllBytes("$PicturePath")
}
catch {
"Error reading file. Please give me the full path to the image file.`nExiting ..."
break
}
$ms = New-Object System.IO.MemoryStream # i need some memory
$ms.Write($img, 0, $img.Length) # image bytes to memory stream
# convert image to Windows System Bitmap
try {
$img = [System.Drawing.Image]::FromStream($ms,$true,$true)
}
catch {
"Error loading image.`nExiting ..."
$ms.Dispose() # free memory
break
}
$ms.Dispose() # free memory
display($img)