2016-03-08 20 views
2

我有一些可愛的PowerShell代碼,從另一個答案那兒剽竊,但我希望能夠漸變填充端添加到模塊:如何使用LinearGradientBrush在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) 

回答

2

下面是其中出現的工作的一個示例:

Add-Type -AssemblyName System.Drawing 

$bitmap = New-Object System.Drawing.Bitmap 100, 100 

$p0 = New-Object System.Drawing.Point(0, 0) 
$p1 = New-Object System.Drawing.Point(100, 100) 

$c0 = [System.Drawing.Color]::FromArgb(255, 255, 0, 0) 
$c1 = [System.Drawing.Color]::FromArgb(255, 0, 0, 255) 

$brush = New-Object System.Drawing.Drawing2D.LinearGradientBrush($p0, $p1, $c0, $c1) 

$graphics = [System.Drawing.Graphics]::FromImage($bitmap) 

$graphics.FillRectangle($brush, 0, 0, $bitmap.Width, $bitmap.Height) 

$bitmap.Save('c:\temp\test.bmp') 

所得圖像:

enter image description here

下面是使用內聯表達式的變化:

Add-Type -AssemblyName System.Drawing 

$bitmap = New-Object System.Drawing.Bitmap 100, 100 

[System.Drawing.Graphics]::FromImage($bitmap).FillRectangle(
    (New-Object System.Drawing.Drawing2D.LinearGradientBrush(
     (New-Object System.Drawing.Point(0, 0)), 
     (New-Object System.Drawing.Point(100, 100)), 
     [System.Drawing.Color]::FromArgb(255, 255, 0, 0), 
     [System.Drawing.Color]::FromArgb(255, 0, 0, 255))), 
    0, 0, $bitmap.Width, $bitmap.Height) 

$bitmap.Save('c:\temp\test-d.bmp') 
+1

感謝一個非常有用的答案。一個小問題...如何重新定義一個簡單的填充矩形的顏色? – Dycey