none
Cannot Load Icon For WinForm When On Shared Drive RRS feed

  • Question

  • I'm currently having an issue whereby, I cannot load the icon for my GUI Form script when the script is placed and launched from a shared location.

    I have done quite a bit of research but couldn't find a definite way to solve this. It isn't that important so if it's not possible then I will just remove the icon option from the form.

    Please let me know if you have any suggestions. Thank you! This is the code I use:

    $form.Icon = [system.drawing.icon]::ExtractAssociatedIcon("$PSScriptRoot\logo.bmp")

    Friday, October 13, 2017 2:53 PM

Answers

All replies

  • What is in $PSScriptRoot.  Write the contents to the console.

    You can embed an icon into a script by converting it to base64.


    \_(ツ)_/

    Friday, October 13, 2017 8:24 PM
  • Hi JRV,

    I was doing some research and $PSScriptRoot is wherever the script is being run from, I tested this and it seems to be the same as putting the get-location into a variable but cleaner.

    I haven't tried the base64 method as, if I'm being honest, I got lost VERY quickly :) Thank you for the suggestion. Are their any guides on this site to do the base64 conversion and the ones I have found seem to miss things out which makes me get lost :/

    Monday, October 23, 2017 9:27 AM
    • Edited by jrv Monday, October 23, 2017 9:47 AM
    • Marked as answer by I.T Delinquent Monday, October 23, 2017 10:28 AM
    Monday, October 23, 2017 9:38 AM
  • To convert to image:

    $pb.Image = [convert]::FromBase64String($b64)


    \_(ツ)_/



    • Edited by jrv Monday, October 23, 2017 9:50 AM
    Monday, October 23, 2017 9:46 AM
  • Hi jrv,

    I have followed one to the guides and it works fine when running the script from the ISE, as in the icon shows correctly on the form. However, when I run the script through a batch file (so that I can bypass execution policies and such) it gives the following errors and doesn't show the icon on the form.

    Unable to find type [System.Drawing.Bitmap].
    At \\sandpdc\Engineers\Scripts\New Users Form\v1\Form_v1.1.ps1:77 char:13
    + $iconbmp = [System.Drawing.Bitmap][System.Drawing.Image]::FromStream( ...
    +             ~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (System.Drawing.Bitmap:TypeName) [], RuntimeException
        + FullyQualifiedErrorId : TypeNotFound

    You cannot call a method on a null-valued expression.
    At \\sandpdc\Engineers\Scripts\New Users Form\v1\Form_v1.1.ps1:78 char:1
    + $iconhandle = $iconbmp.GetHicon()
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    Unable to find type [System.Drawing.Icon].
    At \\sandpdc\Engineers\Scripts\New Users Form\v1\Form_v1.1.ps1:79 char:9
    + $icon = [System.Drawing.Icon]::FromHandle($iconhandle)
    +         ~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (System.Drawing.Icon:TypeName) [], RuntimeException
        + FullyQualifiedErrorId : TypeNotFound

    Please let me know if I'm making any dumb mistakes :)

    Monday, October 23, 2017 10:06 AM
  • Add-Type -AssemblyName System.Windows.Forms


    \_(ツ)_/

    Monday, October 23, 2017 10:09 AM
  • Perfect, that's now working. Thank you for your help jrv!
    Monday, October 23, 2017 10:28 AM