Quantcast
Channel: ::::::::: PowerShell :::::::::
Viewing all articles
Browse latest Browse all 25

[MSH] print-image

$
0
0
Your boss comes at your desk with a pile of CDs filled with high quality tiff images(roughly 20000 x 20000 pixels)
He tells you to print all the image files fit into a "letter" sized paper in 2 days

Well, that sort of stuff happend to me...

Man, was I frusted...

But that's when MSH came into rescue~
Well, the first thing that came up in my head was, "out-printer" cmdlet.
So I tried the following

MSH>$bitmap = new-object System.Drawing.Bitmap c:\test.tif
MSH>out-printer -printer "Adobe PDF" -in $bitmap


Voila. Now i was excited to see a picture in PDF format through "out-printer".
Wrong!

All I saw in the generated PDF file was

Tag :
PhysicalDimension : {Width=14454, Height=21957}
Size : {Width=14454, Height=21957}
Width : 14454
Height : 21957
HorizontalResolution : 1016
VerticalResolution : 1016
Flags : 77888
RawFormat : [ImageFormat: b96b3cb1-0728-11d3-9d7b-0000f81ef32e]
PixelFormat : Format1bppIndexed
Palette : System.Drawing.Imaging.ColorPalette
FrameDimensionsList : {7462dc86-6180-4c7e-8e3f-ee7333a7a483}
PropertyIdList : {256, 257, 258, 259, 262, 270, 273, 277, 278, 279, 282, 283, 32781}
PropertyItems : {256, 257, 258, 259, 262, 270, 273, 277, 278, 279, 282, 283, 32781}


, which is data you see just typing "MSH>$bitmap" in console.

Gosh, was I frusted, again. I looked into "out-printer" help page if there was some kind of "-encoding" option to print binary files(images) but there wans't any...

I resorted to using System.Drawing.Printing.PrintDocument

Well, using that Class, printing is an image is a breezy job
So i created a cmdlet called "print-image" which takes an image name and print it

Here is the usage of the cmdlet

print-image "image name" [printer name] [fit image to paper]


The cmdlet takes the mandatory parameter "image name" with optional printer to print to and whether image size should fit on a paper

Following is how the cmdlet can be used

MSH> ls *.tif | foreach { print-image $_ }


Well and that's exactly what i did... :)







function print-image {
param([string]$imageName = $(throw "Enter image name to print"),
[string]$printer = "",
[bool]$fitImageToPaper = $true)

trap { break; }

# check out Lee Holmes' blog(http://www.leeholmes.com/blog/HowDoIEasilyLoadAssembliesWhenLoadWithPartialNameHasBeenDeprecated.aspx)
# on how to avoid using deprecated "LoadWithPartialName" function
# To load assembly containing System.Drawing.Printing.PrintDocument
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

# Bitmap image to use to print image
$bitmap = $null

$doc = new-object System.Drawing.Printing.PrintDocument
# if printer name not given, use default printer
if ($printer -ne "") {
$doc.PrinterSettings.PrinterName = $printer
}

$doc.DocumentName = [System.IO.Path]::GetFileName($imageName)

$doc.add_BeginPrint({
Write-Host "==================== $($doc.DocumentName) ===================="
})

# clean up after printing...
$doc.add_EndPrint({
if ($bitmap -ne $null) {
$bitmap.Dispose()
$bitmap = $null
}
Write-Host "xxxxxxxxxxxxxxxxxxxx $($doc.DocumentName) xxxxxxxxxxxxxxxxxxxx"
})

# Adjust image size to fit into paper and print image
$doc.add_PrintPage({
Write-Host "Printing $imageName..."

$g = $_.Graphics
$pageBounds = $_.MarginBounds
$img = new-object Drawing.Bitmap($imageName)

$adjustedImageSize = $img.Size
$ratio = [double] 1;

# Adjust image size to fit on the paper
if ($fitImageToPaper) {
$fitWidth = [bool] ($img.Size.Width > $img.Size.Height)
if (($img.Size.Width -le $_.MarginBounds.Width) -and
($img.Size.Height -le $_.MarginBounds.Height)) {
$adjustedImageSize = new-object System.Drawing.SizeF($img.Size.Width, $img.Size.Height)
} else {
if ($fitWidth) {
$ratio = [double] ($_.MarginBounds.Width / $img.Size.Width);
} else {
$ratio = [double] ($_.MarginBounds.Height / $img.Size.Height)
}

$adjustedImageSize = new-object System.Drawing.SizeF($_.MarginBounds.Width, [float]($img.Size.Height * $ratio))
}
}

# calculate destination and source sizes
$recDest = new-object Drawing.RectangleF($pageBounds.Location, $adjustedImageSize)
$recSrc = new-object Drawing.RectangleF(0, 0, $img.Width, $img.Height)

# Print to the paper
$_.Graphics.DrawImage($img, $recDest, $recSrc, [Drawing.GraphicsUnit]"Pixel")

$_.HasMorePages = $false; # nothing else to print
})

$doc.Print()
}





Oh yeah, I would appreciate it if anyone can tell me how i can format the code to fit on to the site :)

Tags :

Viewing all articles
Browse latest Browse all 25

Trending Articles