Hey guys, Stuck on this one. I’m sure its something simple I’m missing.
So this works
$form1.BackgroundImage = [System.Drawing.Image]::FromFile("c:\pathto\image\automation.jpg")
Will populate my form with the correct background.
Its when I try to add that as a variable which I’m setting in a config file.
$background = [string][System.Drawing.Image]::FromFile("c:\pathto\image\automation.jpg")
$form1.backgroundimage = $background
Tried using the following to a expand out the variable as well
$ExecutionContext.InvokeCommand.ExpandString($background)
Getting the following error
ERROR: Exception setting "BackgroundImage": "Cannot convert the "[System.Drawing.Image]::FromFile("c:\pathto\image\automation.jpg")" value of type
ERROR: "System.String" to type "System.Drawing.Image"."
You’re casting it twice in that assignment. Remove “[string]” and try again.
$background = [System.Drawing.Image]::FromFile("c:\pathto\image\automation.jpg")
$form1.backgroundimage = $background
[quote quote=124713]You’re casting it twice in that assignment. Remove “[string]” and try again.
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
$background = [System.Drawing.Image]::FromFile("c:\pathto\image\automation.jpg")
$form1.backgroundimage = $background
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote]
Thanks for the reply. Charles tried it same result. I was hoping to be able to use it to set different backgrounds based on different scenarios as a visual aid, but I can accomplish it another way. It’s just my damn OCD that won’t let me let it go LOL. Think I’m going to have to forgo it. I have spent way too much time. I’ll just set it and forget it.
That’s weird, then… This works fine for me.
PS E:\> Add-Type -AssemblyName System.Windows.Forms
PS E:\> $Form = New-Object system.Windows.Forms.Form
PS E:\> $background = [System.Drawing.Image]::FromFile("c:\temp\chart2.png")
PS E:\> $form.BackgroundImage = $background
PS E:\> $Form.ShowDialog()
But if I try to do it with that “[string]” cast in there, too, I get the error you’re seeing.
PS E:\> $background = [string][System.Drawing.Image]::FromFile("c:\temp\chart2.png")
PS E:\> $form.BackgroundImage = $background
Exception setting "BackgroundImage": "Cannot convert the "System.Drawing.Bitmap" value of type "System.String" to type
"System.Drawing.Image"."
At line:1 char:1
+ $form.BackgroundImage = $background
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
I’m going to try that, the only difference is I’m setting a variable from an xml config.
That code is
<add key=“bacground” value =" [System.Drawing.Image]::FromFile(“C:\pathto\images\Border_Grey.png”)"/>
I have tried differnt variances.
Just in case someone wants to give it a go.
Ps script needs this to read from xml
$configfile = ("c:\pathto\somfile.conf")
[xml]$config = Get-Content ($configfile)
foreach ($item in $Config.configuration.appSettings.add)
{ if ($item.key -eq "background")
{ $background = $item.value }
}
[quote quote=124794]I’m going to try that, the only difference is I’m setting a variable from an xml config.
That code is
I have tried differnt variances.
Just in case someone wants to give it a go.
Ps script needs this to read from xml
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
$configfile = ("c:\pathto\somfile.conf")
[xml]$config = Get-Content ($configfile)
foreach ($item in $Config.configuration.appSettings.add)
{ if ($item.key -eq "background")
{ $background = $item.value }
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote]
Forgot that xml doesn't display here.
Use gist to post xml here.
gist.github.com