Printing image using .net framework in powershell RRS feed

  • Question

  • I wrote this powershell script to print images. But when the script runs and starts to print I am getting empty page printout. I tried to use printdocument and graphics from .NET framework.This is my first time using powershell and .NET framework so I used these scripts for reference: script1script2. I don't know what I am missing here. Any help is greatly appreciated.

    function Start-Print {
        $CopyCount = 1
        $pd = New-Object System.Drawing.Printing.PrintDocument
        $files = Get-ChildItem -Path "D:\Users\Phuntsho Wangdi\Desktop\New folder\unprinted\*" -Include "*.jpeg", "*.png", "*.jpg"
        foreach ($global:file in $files) {
            $file_name = Get-ChildItem -Name $file
            Write-Host $file_name
            $pd.DefaultPageSettings.PrinterSettings.PrinterName = "Canon E510 series printer"
            $pd.DefaultPageSettings.Landscape = $True
            $pd.PrinterSettings.Copies = $CopyCount
            $pd.DefaultPageSettings.Color = $false
            $pd.DocumentName = $file_name
            $action = {
                $bmp = New-Object System.Drawing.Bitmap("$file")
                #implemented from https://www.powershellgallery.com/packages/RoughDraft/0.1/Content/Send-Printer.ps1
                $newWidth = $bmp.Width * 100 / $bmp.HorizontalResolution
                $newHeight = $bmp.height * 100 / $bmp.VerticalResolution
                $widthFactor = $newWidth / $EventArgs.PageSettings.PrintableArea.height
                $HeightFactor = $newHeight / $EventArgs.PageSettings.PrintableArea.Width
                $widthMargin = 0
                $heightMargin = 0
                if ($widthFactor -ge 1 -or $HeightFactor -ge 1) {
                    if ($widthFactor -gt $HeightFactor) {
                        $newWidth = $newWidth / $widthFactor 
                        $newHeight = $newHeight / $widthFactor 
                    else {
                        $newWidth = $newWidth / $HeightFactor
                        $newHeight = $newHeight / $HeightFactor
                $heightMargin = (($EventArgs.PageSettings.PrintableArea.Height - $newHeight) / 2) / 2
                $EventArgs.Graphics.DrawImage($bmp, ($EventArgs.PageSetting.HardMarginX + $widthMargin), ($EventArgs.PageSetting.HardMarginY + $heightMargin), $newWidth, $newHeight)
            $endprint = {
            Register-ObjectEvent -InputObject $pd -EventName "PrintPage" -Action $action
            Register-ObjectEvent -InputObject $pd -EventName "EndPrint" -Action $endprint
        Write-Host "Done!"

    Sunday, August 11, 2019 1:16 PM

All replies

  • Seems the script file here can suit your need.


    Monday, August 12, 2019 4:22 AM
  • Hi phuntsho, 

    Thank you for posting here.

    According to your description, your issue is more related to PowerShell. Therefore, it will be more appropriate to ask your question in PowerShell Forums.

    The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions.

    Thank you for your understanding.

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 12, 2019 5:42 AM
  • Ok, thanks
    Monday, August 12, 2019 11:32 AM