我有一些可爱的 PowerShell 代码,抄自另一个答案,但我希望能够在块中添加一个渐变填充端:
Add-Type -AssemblyName System.Drawing
$bmp = new-object System.Drawing.Bitmap 208,61
$brushBg = [System.Drawing.Brushes]::Blue
# replace this line...
$brushGr = [System.Drawing.Brushes]::Yellow
$graphics = [System.Drawing.Graphics]::FromImage($bmp)
$graphics.FillRectangle($brushBg,0,0,$bmp.Width,$bmp.Height)
$graphics.FillRectangle($brushGr ,$bmp.Width-50,0,50,$bmp.Height)
$graphics.Dispose()
$bmp.Save($filename)
到目前为止,谷歌不是我的朋友,因为我不明白如何将 C# 和 VB 的 MSDN 文档与 PowerShell 等效。我对替换行的猜测是这样的:
# ... with these lines
$col1 = new-object System.Drawing.Color.FromRgb( 209, 227, 250)
$col2 = new-object System.Drawing.Color.FromRgb(170, 199, 238)
$pt1 = new-object System.Drawing.Point(0.5, 0)
$pt2 = new-object System.Drawing.Point(0.5, 1)
$brushGr = new-object System.Drawing.LinearGradientBrush( $col1 , $col2, $pt1, $pt2)
非常感谢任何帮助。
感谢下面 Dharmatech 的出色回答,如果它对其他人有任何用处,这是我最终用于绘制标记条的工作代码(保存MYFILENAME.ps1
并运行它powershell -ExecutionPolicy Bypass -file MYFILENAME.ps1
)
Add-Type -AssemblyName System.Drawing
$filename ="$env:userprofile\Desktop\px95.png"
$bitmap = New-Object System.Drawing.Bitmap 95,19
$barBlendWidth = 20
$start = $bitmap.Width - $barBlendWidth
$mid = $bitmap.Width - ( 0 + $barBlendWidth / 2 )
$p0 = New-Object System.Drawing.Point($start, 10 )
$p1 = New-Object System.Drawing.Point($mid, 10 )
$p2 = New-Object System.Drawing.Point($bitmap.Width, 10 )
$fromColour = [System.Drawing.Color]::FromArgb(255, 144, 238, 144)
$midColour = [System.Drawing.Color]::FromArgb(255, 255, 255, 255)
$toColour = [System.Drawing.Color]::FromArgb(255, 255, 255, 70)
$brushBG = [System.Drawing.Brushes]::LightGreen
$brush1 = New-Object System.Drawing.Drawing2D.LinearGradientBrush($p0, $p1, $fromColour, $midColour)
$brush2 = New-Object System.Drawing.Drawing2D.LinearGradientBrush($p1 , $p2, $midColour, $toColour)
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
$graphics.FillRectangle($brushBG, 0, 0, $bitmap.Width, $bitmap.Height)
$graphics.FillRectangle($brush1 , $start ,0,$barBlendWidth,$bitmap.Height)
$graphics.FillRectangle($brush2 , $mid ,0,$barBlendWidth,$bitmap.Height)
$bitmap.Save($filename)