windows - 进程 block 在 Graphics.DrawString 中不起作用

标签 windows powershell object graphics

我有一个 PowerShell 函数 (out())。当我想将管道的结果获取到图像时,它会从管道中获取最后一个对象。例如:我想显示 (gps) 中的所有对象:

function out {
[cmdletbinding()]
param(
    [parameter(
        Mandatory = $true,
        ValueFromPipeline = $true,
        ValueFromPipelineByPropertyName = $true)]
    [string[]]
    $n
)



    Process {
    $dirname = Get-Location | Select-Object -ExpandProperty Path
    $filename = $(Get-Date -f "yyyy-mm-dd--hh-mm-ss-tt") + "--image.png"
    $ImagePath = $dirname + "\" + $filename

      ForEach ($input in $n) {

        #PUT Your Image:
         $basefilename = "D:\ZalansDB\imgs\main\bg_D.jpg"
            $bmp = [System.Drawing.Bitmap]::FromFile("$basefilename")

            $font = new-object System.Drawing.Font ('Microsoft Sans Serif',16)
            $fcolors = [System.Drawing.Brushes]
            $graphics = [System.Drawing.Graphics]::FromImage($bmp)

            $graphics.DrawString($input,$font,$fcolors::White,30,40)


            $filename = $ImagePath
            $graphics.Dispose() 
            $bmp.Save($filename)
         } 




            Invoke-Item $filename 


    } 



}

Get-Process | Select-Object -First 2 | out

结果:
enter image description here

我想在我的图像中显示前 2 个过程对象

最佳答案

我可以看到很多有效的方法。使用 begin block 设置不会更改的变量。使用 process block 收集进程名称。最后,使用 end 显示图像。我们使用 -join 将它们全部放在一个字符串中,这样我们就不必一直重绘和管理位置。

function Set-ProcessPicture{
    [cmdletbinding()]
    param(
        [parameter(
            Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true)]
        [Alias("ProcessName")]
        $Process,
        [parameter(Mandatory=$false)]
        $BaseImagePath = "d:\temp\test.jpg"
    )


    begin{
        $dirname = Get-Location | Select-Object -ExpandProperty Path
        $font = new-object System.Drawing.Font ('Microsoft Sans Serif',16)
        $fcolors = [System.Drawing.Brushes]
        $baseBitmap = [System.Drawing.Bitmap]::FromFile($BaseImagePath)
        $graphics = [System.Drawing.Graphics]::FromImage($baseBitmap)

        $outputString = @()
    }

    Process {
        $outputString += $process
    } 
    end {
        $graphics.DrawString(($outputString -join "`r`n"),$font,$fcolors::White,30,40)
        $filename = [io.path]::Combine((Get-Location).Path,(Get-Date -f "yyyy-MM-dd--hh-mm-ss-fff") + "--image.png")
        $graphics.Dispose() 
        $baseBitmap.Save($filename)

        Invoke-Item $filename 
    }

}

Get-Process | Select-Object -First 2 | Set-ProcessPicture

Sample Output

我做了一些最佳实践代码更改。这不是我想要的方式,而是纠正您遇到的问题。

注意:在格式字符串中使用 MM 表示月份

关于windows - 进程 block 在 Graphics.DrawString 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49620488/

相关文章:

powershell - 使用 "dot"变量名称的数组设置 PSObject 路径

javascript - 如何创建一个具有许多其他对象功能别名的对象

c++ - 在 Windows 7 中创建 POSIX 应用程序?

powershell - 复合赋值运算符在子范围内如何表现?

powershell - 如何在powershell中删除标签@{}

javascript - Array.prototype.map() 返回空项,但 Array.prototype.forEach() 不会

c# - 如何从字符串创建名称为对象的对象?

c++ - 与linux和windows c++兼容的共享内存库

windows - .NET 应用程序与独立的 Windows 和 Azure 兼容

python - ffmpeg 无法检测文件,而 os 可以