What am I missing here

Welcome Forums General PowerShell Q&A What am I missing here

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
1 month ago.

  • Author
    Posts
  • #124692

    Participant
    Points: 67
    Rank: Member

    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"."
  • #124713

    Participant
    Points: 44
    Rank: Member

    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
  • #124725

    Participant
    Points: 67
    Rank: Member

    You're casting it twice in that assignment. Remove "[string]" and try again.

    1
    2
    $background = [System.Drawing.Image]::FromFile("c:\pathto\image\automation.jpg")
    $form1.backgroundimage = $background
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    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.

  • #124769

    Participant
    Points: 44
    Rank: Member

    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
  • #124794

    Participant
    Points: 67
    Rank: Member

    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

    $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 }
    }

     

  • #124796

    Participant
    Points: 67
    Rank: Member

    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

    PowerShell
    6 lines

    1
    2
    3
    4
    5
    6
    $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

    Forgot that xml doesn't display here.

  • #124853

    Participant
    Points: 885
    Helping Hand
    Rank: Major Contributor

    Use gist to post xml here.

    gist.github.com

You must be logged in to reply to this topic.