During my spare time, i spent sometime converting "print-image" cmdlet into a Snapin version.
I have extended its functionality just by a tiny bit by adding "Landscape" mode printing. Well, I still haven't tried adding a "help" file yet so I will try to add one when I have more time(so for now, something like "print-image -?" will complain that cmdlet cannot find a help xml file).
Anyways, here is the usage for "print-image" for this this version.
Well, if you would like to change "margins", there are two options. First one is you go to your printer setting and change the margin size there, or modify the msh script to be able to receive margin data.
To retrieve available printer names on your current machine, you can load "System.Drawing" assembly and then retrieve "System.Drawing.Printing.PrinterSettings.InstalledPrinters" property like the following
and then use the printer names listed above
Here is how you can use the "print-image"
![Image hosting by Photobucket]()
Uhm, I don't have any storage areas where I can just put my source or compiled binaries on... So, i am just pasting the source here...
Tags : Monadmsh
I have extended its functionality just by a tiny bit by adding "Landscape" mode printing. Well, I still haven't tried adding a "help" file yet so I will try to add one when I have more time(so for now, something like "print-image -?" will complain that cmdlet cannot find a help xml file).
Anyways, here is the usage for "print-image" for this this version.
Where,
This command also supports only one ubiquitous parameter: -Verbose(-vb)
print-image [-FileName|-fn] System.String [[-Printer|-p|-pn] System.String] [[-Landscape|-l|-ls] System.Boolean]
- FileName: Fully Qualified File Name
- Printer: Name of print to send image to
- Landscape: Print images in Landscape mode
Well, if you would like to change "margins", there are two options. First one is you go to your printer setting and change the margin size there, or modify the msh script to be able to receive margin data.
To retrieve available printer names on your current machine, you can load "System.Drawing" assembly and then retrieve "System.Drawing.Printing.PrinterSettings.InstalledPrinters" property like the following
MSH>[reflection.assembly]::loadwithpartialname("system.drawing")
GAC Version Location
--- ------- --------
True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\Syst...
MSH>[Drawing.Printing.PrinterSettings]::InstalledPrinters
Microsoft Office Document Image Writer
HP LaserJet 8150 PCL 5e
HP LaserJet 5000 Series PCL 5e
HP Color LaserJet 5500 PCL6(Color)
Adobe PDF
MSH>
and then use the printer names listed above
Here is how you can use the "print-image"

Uhm, I don't have any storage areas where I can just put my source or compiled binaries on... So, i am just pasting the source here...
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.ComponentModel;
using System.Management.Automation;
using System.Collections.Generic;
using System.Text;
namespace D2D.Snapins
{
/// <summary>
/// Prints image files
/// </summary>
/// <remarks>
/// @TODO: Display Preview form
/// </remarks>
[RunInstaller(true)]
publicclass PrintImageSnapin : MshSnapIn
{
publicoverridestring Description
{
get { return"Prints an image file"; }
}
publicoverridestring Name
{
get { return"D2D_print-image"; }
}
publicoverridestring Vendor
{
get { return"DontBotherMeWithSpam"; }
}
}
[Cmdlet("print", "image")]
publicclass PrintImageCommand : MshCmdlet
{
#region Private Member Variables
/// <summary>
/// Fully qualified image file name to print
/// </summary>
privatestring fileName;
/// <summary>
/// Name of printer to use
/// </summary>
privatestring printer;
/// <summary>
/// Should we use landscape mode or not
/// </summary>
privatebool landscape;
/// <summary>
/// Responsible for printing image document
/// </summary>
private PrintDocument doc;
/// <summary>
/// Image to constructed from input file name
/// </summary>
private Bitmap img;
/// <summary>
/// Holds exception thrown in functions other than
/// "BeginProcessing", "EndProcessing", "ProcessRecord"
/// </summary>
private Exception ex;
#endregion
#region MSH Parameters
/// <summary>
/// Fully qualified image file name to print
/// </summary>
[Alias("fn")]
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true,
HelpMessage = "Fully qualified image file name to print")]
publicstring FileName
{
get { returnthis.fileName; }
set { this.fileName = value; }
}
/// <summary>
/// Name of printer to use
/// </summary>
[Alias("p", "pn")]
[Parameter(Mandatory = false, Position = 1, ValueFromPipeline = true,
HelpMessage = "Name of printer to print image")]
publicstring Printer
{
get { returnthis.printer; }
set { this.printer = value; }
}
[Alias("l", "ls")]
[Parameter(Mandatory = false, Position = 2, ValueFromPipeline = false,
HelpMessage = "Toggle Landscape mode")]
[DefaultValue(false)]
publicbool Landscape
{
get { returnthis.landscape; }
set { this.landscape = value; }
}
#endregion
protectedoverridevoid EndProcessing()
{
this.doc = new PrintDocument();
this.doc.BeginPrint += new PrintEventHandler(doc_BeginPrint);
this.doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
this.doc.EndPrint += new PrintEventHandler(doc_EndPrint);
this.doc.Print();
}
#region PrintDocument Event Handlers
/// <summary>
/// Construct image to print
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
privatevoid doc_BeginPrint(object sender, PrintEventArgs e)
{
// Construct image to print
try
{
this.img = new Bitmap(this.FileName);
this.doc.DocumentName = Path.GetFileName(this.FileName);
this.doc.DefaultPageSettings.Landscape = this.Landscape;
if (this.Printer != "")
{
this.doc.PrinterSettings.PrinterName = this.Printer;
}
}
catch (Exception ex)
{
// We have failed create an image from the given file name
// No need to process any further, actually...
/// How do i abort the script at this point?
ErrorRecord err = new ErrorRecord(ex, "doc_BeginPrintError",
ErrorCategory.InvalidData, this.fileName);
WriteError(err);
e.Cancel = true; // abort printing job
}
//e.PrintAction = this.PrintAction;
e.Cancel = false;
WriteVerbose(string.Format(
"==================== {0} ====================",
this.doc.DocumentName));
}
/// <summary>
/// Print constructed image according to PrintAction
/// </summary>
privatevoid doc_PrintPage(object sender, PrintPageEventArgs e)
{
WriteVerbose(string.Format("Printing {0}",
this.doc.DocumentName));
Graphics g = e.Graphics;
Rectangle paperBounds = e.MarginBounds;
SizeF adjSize = AdjustImageSize(this.img.Size, paperBounds);
if (this.ex == null)
{
// Calculate source and destination sizes
RectangleF destRec = new RectangleF(paperBounds.Location, adjSize);
RectangleF srcRec = new RectangleF(0, 0, img.Width, img.Height);
// Print to according to "PrintAction"
g.DrawImage(this.img, destRec, srcRec, GraphicsUnit.Pixel);
}
else
{
ErrorRecord err = new ErrorRecord(this.ex, "AdjustImageSizeError",
ErrorCategory.NotSpecified, null);
WriteError(err);
}
// Nothing else to print...
e.HasMorePages = false;
}
/// <summary>
/// Clean up used resources
/// </summary>
privatevoid doc_EndPrint(object sender, PrintEventArgs e)
{
// Free Bitmap resource
if (this.img != null)
{
this.img.Dispose();
this.img = null;
}
WriteVerbose(string.Format(
"xxxxxxxxxxxxxxxxxxxx {0} xxxxxxxxxxxxxxxxxxxx",
this.doc.DocumentName));
}
#endregion
/// <summary>
/// Adjust image size to fit to the paper size
/// </summary>
/// <param name="imgSize">Size of image to adjust to fit to paper size</param>
/// <param name="paperBound">Bounding area of paper</param>
/// <returns>Adjusted size of image that fits on the paper</returns>
/// <remarks>If image's width is bigger than its height, try to fit width, else fit height</remarks>
private SizeF AdjustImageSize(Size imgSize, RectangleF paperBound)
{
bool fitWidth = (imgSize.Width > imgSize.Height); // Fit width or the height?
double ratio = 1;
SizeF adjSize = new SizeF(imgSize.Width, imgSize.Height); // adjusted size to return
try
{
// Image size is smaller than actual paper size to print on,
// so we use the original image's size
if ((imgSize.Width > paperBound.Width) &&
(imgSize.Height > paperBound.Height))
{
// If width is longer than the height of its image,
// we get the ratio of the width, else get the ratio of the height
if ((fitWidth == true))
{
ratio = (double)(paperBound.Width / imgSize.Height);
}
else
{
ratio = (double)(paperBound.Height / imgSize.Height);
}
adjSize = new SizeF(
// Flip the width of the image in Landscape mode
(this.Landscape ? paperBound.Height : paperBound.Width),
(float)(imgSize.Height * ratio));
}
}
catch (Exception ex)
{
this.ex = ex;
}
return adjSize;
}
}
}
Tags : Monadmsh