![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| Vista Ultimate x64 | Windows programming and image manipulation in Powershell question I'm trying to do a little Windows programming through Powershell. I can manipulate some aspects of a window, e.g., set the icon or BackGroundColor: this works:$form = New-Object Windows.Forms.Form this works:$icon = "C:\CDROMS\ICO\wow-1.ico" this works:$form.icon = $icon this works: $form.BackGroundColor = "Red" But I am not able to set a background image. Presumably this would work:$form.BackGroundImage = $image But unfortunately I am never able to reach that point. This does not work; "no appropriate constructor" error:$image1 = New-Object System.Drawing.Bitmap This does not work; "no appropriate constructor" error:$image1 = New-Object System.Drawing.Image This line executes ok...$image1 = Get-Content C:\textures\free-textures3tn.jpg but... ...when you follow it with... $form.BackGroundImage = $image1 you get the "Cannot convert System.Object[] to System.Drawing.Image" error So my question posed broadly is, how can I use a background image on a form in Powershell? And more narrowly, how can I make Powershell accept an image as the value of $image, so that I can execute $form.BackGroundImage = $image?Here's my sample program. It just creates a Windows form with a couple of buttons that launch scripts named startworst.ps1 and stopworst.ps1 respectively: #Windows programming using Powershell example, WoW.ps1 Set-PSDebug-Trace 0 -Strict [string]$b1="startworst.ps1" [string]$b2="stopworst.ps1" function Get-OkCancel { param ($question="Start or Stop only the worst CPU-hogging services.") function load-assembly { param([string] $assemblyName) # Our assembly name shortcuts $assemblyMappings= ( ("forms", "System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"), ("drawing", "System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") ) # List the assembly shortcuts we support if(-not$assemblyName) { "Please specify an assembly name. Supported assemblies are: "foreach($assemblyin$assemblyMappings) { $assembly[0] } return } # Load the assembly they request # This fails with an error message if this specific assembly version can't # be loaded. foreach($assemblyin$assemblyMappings) { if($assemblyName-eq$assembly[0]) { [void] [Reflection.Assembly]::Load($assembly[1]) } } } # end of function Load-Assembly load-assembly "forms" load-assembly "drawing" function point ($x,$y) { New-Object Drawing.Point $x,$y } $form=New-Object Windows.Forms.Form $icon="C:\CDROMS\ICO\wow-1.ico" ######################## ### $image = ??????????? ######################## $form.Icon =$icon $form.Text ="WoWready" $form.Size = point 462 98 ######################################## ### $form.BackGroundImage = ???????????? ######################################## $label=New-Object Windows.Forms.Label $label.Text =$question $label.Location = point 110 10 $label.Size = point 250 50 $label.Anchor="top" $ok=New-Object Windows.Forms.Button $ok.text="Start" $ok.Location = point 10 10 $ok.Anchor="top,left" $ok.add_click({ Invoke-Expression$b1 }) $cancel=New-Object Windows.Forms.Button $cancel.text="Stop" $cancel.Location = point 360 10 $cancel.Anchor="top,right" $cancel.add_click({ Invoke-Expression$b2 }) $form.controls.addRange(($label,$ok,$cancel)) $form.Add_Shown({$form.Activate()}) $form.ShowDialog() } # End of function Get-OKCancel get-okCancel #end of Windows programming using Powershell example, WoW.ps1 |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Windows Image Back-up question | Vista performance & maintenance | |||
| Text manipulation question | PowerShell | |||
| Book: Professional Windows PowerShell Programming: Snapins, Cmdlets,Hosts and Providers (Paperback) by Wrox | PowerShell | |||
| [ANN] New PowerShell programming book due in September | PowerShell | |||
| Common PowerShell programming traps | PowerShell | |||