@@ -2,30 +2,30 @@ Add-Type -AssemblyName System.Drawing
2
2
Add-Type - AssemblyName System.Windows.Forms
3
3
4
4
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
5
- $OpenFileDialog .Filter = " Images (*.jpg)| *.jpg"
5
+ $FileDialog .Filter = " Images|*.bmp; *.jpg;*.jpeg;*.png;*.gif;*.tiff;*.ico "
6
6
$OpenFileDialog.Multiselect = $false
7
7
8
- if ($OpenFileDialog .ShowDialog () -eq [System.Windows.Forms.DialogResult ]::OK) {
9
- $imgPath = $OpenFileDialog .FileName
10
- Write-Host " File selected: $imgPath " - ForegroundColor Green
8
+ if ($FileDialog .ShowDialog () -eq [System.Windows.Forms.DialogResult ]::OK) {
9
+ $selectedImagePath = $FileDialog .FileName
10
+ Write-Host " File selected: $selectedImagePath " - ForegroundColor Green
11
11
Write-Host " "
12
12
} else {
13
- Write-Host " No file selected." - ForegroundColor Yellow
13
+ Write-Host " No File selected." - ForegroundColor Yellow
14
14
Write-Host " "
15
15
return
16
16
}
17
17
18
- $doc = New-Object System.Drawing.Printing.PrintDocument
19
- $doc .PrinterSettings = New-Object System.Drawing.Printing.PrinterSettings
20
- $doc .PrinterSettings.PrinterName = " Microsoft Print to PDF"
21
- $doc .PrinterSettings.PrintToFile = $true
22
- $outFile = [System.IO.Path ]::ChangeExtension($imgPath , " .pdf" )
23
- $doc .PrinterSettings.PrintFileName = $outFile
18
+ $printDocument = New-Object System.Drawing.Printing.PrintDocument
19
+ $printDocument .PrinterSettings = New-Object System.Drawing.Printing.PrinterSettings
20
+ $printDocument .PrinterSettings.PrinterName = " Microsoft Print to PDF"
21
+ $printDocument .PrinterSettings.PrintToFile = $true
22
+ $outputFilePath = [System.IO.Path ]::ChangeExtension($selectedImagePath , " .pdf" )
23
+ $printDocument .PrinterSettings.PrintFileName = $outputFilePath
24
24
25
- $script :_pageIndex = 0
26
- $doc .add_PrintPage ({
25
+ $pageIndex = 0
26
+ $printDocument .add_PrintPage ({
27
27
param ($sender , [System.Drawing.Printing.PrintPageEventArgs ] $e )
28
- $image = [System.Drawing.Image ]::FromFile($imgPath )
28
+ $image = [System.Drawing.Image ]::FromFile($selectedImagePath )
29
29
try {
30
30
$e.Graphics.DrawImage ($image , $e.PageBounds )
31
31
$e.HasMorePages = $false
@@ -34,7 +34,6 @@ $doc.add_PrintPage({
34
34
}
35
35
})
36
36
37
- $doc.PrintController = New-Object System.Drawing.Printing.StandardPrintController
38
-
39
- $doc.Print ()
40
- Write-Host " File printed to $outFile " - ForegroundColor Green
37
+ $printDocument.PrintController = New-Object System.Drawing.Printing.StandardPrintController
38
+ $printDocument.Print ()
39
+ Write-Host " File saved in $outputFilePath " - ForegroundColor Green
0 commit comments