Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Windows programming and image manipulation in Powershell question

Reply
 
Old 02-16-2008   #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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46