Windows 进程块在Graphics.DrawString中不起作用

Windows 进程块在Graphics.DrawString中不起作用,windows,powershell,object,graphics,Windows,Powershell,Object,Graphics,我有一个PowerShell函数(out())。当我想从管道中获取一个结果到图像中时,它会从管道中获取最后一个对象。例如:我想在(gps)中显示所有对象: 结果: 我想在我的图像中显示前两个prcoess对象我可以看到很多有效的方法。使用beginblock设置不会更改的变量。使用进程块收集进程名称。最后,使用end显示图像。我们使用-join将它们放在一个字符串中,这样我们就不必一直重新绘制和管理位置 function Set-ProcessPicture{ [cmdletbindi

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

结果


我想在我的图像中显示前两个prcoess对象

我可以看到很多有效的方法。使用
begin
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

我做了一些最佳实践代码更改。这不是我将如何离开它,但纠正了您的问题


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

您总是在同一位置(30、40)绘制,因此您永远不会看到多个结果。这是故意的吗?Bruce Payette!,哦,你是真的吗?!谁是我的梦中情人?我不相信。谢谢,先生。您的文件名不是唯一的。如果你在那里睡上一秒钟,它会起作用,因为它允许名字改变。我也会用begin块来处理你只需要设置一次的事情。哦?我不清楚。要将所有进程作为文本放在一个图像文件中吗?你应该一次把他们全部收集起来,然后用新台词加入他们。麻生太郎-是的,我是真正的布鲁斯·帕耶特:-)很高兴认识你!看来@Matt对你的问题处理得很好。干杯谢谢,我们能得到和控制台一样的“gps | select-F2”结果吗?我的意思是,如果您键入上述cmdlet,则会得到不同的结果。我想把同样的控制台结果拍下来。谢谢,如果你想这样做,那么你可以通过管道输入输出字符串,这将得到一个字符串,就像控制台一样。这样做有点否定了管道函数,尽管我使用:$outputString+=$process | out string,但它会在我的图片中显示每个结果。知道吗,我通过更改这一行来修复:
$graphics.DrawString($outputString | out string),$font,$fcolors::White,930100)
-谢谢
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