过程块在Graphics.DrawString中不起作用 [英] Process Block Not Work in Graphics.DrawString
问题描述
我有一个PowerShell函数( out()
)。当我想从管道获取一个结果到一个 image
中时,它会从管道中获取最后一个对象。例如:我想显示( gps
)中的所有对象:
I have a PowerShell function (out()
). When I want to get a result from pipeline into an image
, it takes the last object from the pipeline. For example: I want to show all objects in (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
结果:
我想在头像中显示前两个物体对象
I want to show First 2 prcoess objects into my image
推荐答案
我可以看到大量有效的方法。使用 begin
块来设置不会改变的变量。使用进程
块来收集进程名称。最后,使用 end
来显示图像。我们使用 -join
将它们全部放在一个字符串中,这样我们就不必一直重绘和管理位置。
I could see loads of valid approaches for this. Use begin
block to set the variables that are not going to change. Use the process
block to collect the process names. Finally, use end
to display the image. We use -join
to get them all in one string so we don't have to redraw, and manage locations, all the time.
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
我做了一些最佳实践代码更改。这不是我如何离开它,而是纠正你遇到的问题。
I made a few best practice code changes. This is not how I would leave it but corrects the issue that you were having.
注意:在格式字符串中使用月份为MM
Note: Use MM for months in your format strings
这篇关于过程块在Graphics.DrawString中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!