Link to home
Start Free TrialLog in
Avatar of HStrix
HStrix

asked on

VB.Net: How to get the exe's icon

Hello experts,
in my Vb.Net windows application I want to use
the icon of the created exe file.

Getting the form's icon I can use
           Me.Icon
and I can modify the form's icon using
           Me.Icon = New System.Drawing.Icon(System.Windows.Forms.Application.StartupPath & "myicon.ico")

How can I use the exe's icon in the following statement?
          Me.Icon = .... ' ???
The exe icon is set using the properties of the project.

If anyone knows a solution,
please supply related code.

   Thank you very much for every help.

   HStrix
Avatar of checoo
checoo

Something like the following should work

Dim _assembly As [Assembly] = [Assembly].GetExecutingAssembly()
Dim myAppIcon As Icon
dim myAppIcon = new Icon()
myAppIcon = ctype(_assembly.GetManifestResourceStream("YourNamespace.ApplicationIconName.ico"),Icon)

Me.Icon = myAppIcon
Avatar of HStrix

ASKER

Thanks,
I get the message "The type Assembly is not defined."
you have to import system.reflection
Avatar of HStrix

ASKER

Now I get for statement
   myAppIcon = ctype(_assembly.GetManifestResourceStream("YourNamespace.ApplicationIconName.ico"),Icon)
the message
  The value of type 'System.IO.Stream' cannot be converted to 'System.Drawing.Icon'.
Avatar of HStrix

ASKER

I used
   Dim myAppIcon As Icon
not
  dim myAppIcon = new Icon() ' <== this one gives message:
       Overload couldn't be performed, New doesn't accept the number of arguments.

try the following

dim resStream as System.IO.Stream ResStream = _assembly.GetManifestResourceStream ("YourNamespace.ApplicationIconName.ico")
 myAppIcon = new System.Drawing.Icon(ResStream)
sorry the first line should be

dim resStream as System.IO.Stream = _assembly.GetManifestResourceStream ("YourNamespace.ApplicationIconName.ico")
Avatar of HStrix

ASKER

OK,
now the code looks like
   Imports System.Threading  
   Imports System.Reflection
   Namespace NamespaceTest
     Public Class frmTest
     .....
        Dim _assembly As [Assembly] = [Assembly].GetExecutingAssembly()
        Dim myAppIcon As Icon
        Dim resStream As System.IO.Stream = _assembly.GetManifestResourceStream("NamespaceTest.ApplicationIconName.ico")
        myAppIcon = New System.Drawing.Icon(resStream)   ' <=== error occurs here
        Me.Icon = myAppIcon
     .....
     End Class
   End Namespace
the message
   Unknown exception of type 'System.ArgumentException'  has occured in system.drawing.dll .
   Additional information: 'null' is not valid for 'stream'

Avatar of HStrix

ASKER


Execution of
     Dim resStream As System.IO.Stream = _assembly.GetManifestResourceStream("NamespaceTest.ApplicationIconName.ico")

gives as result: resStream = Nothing
its not able to get the icon resource during runtime. It might be because the application namespace is wrong. And the ApplicationIconNAme should be replaced by the name of the icon you have used for your application...the default value is app.ico
ASKER CERTIFIED SOLUTION
Avatar of armoghan
armoghan
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HStrix

ASKER

Thanks armoghan,
based on your hint I created the following code:
---
#Region " API"
        Public Declare Function DestroyIcon Lib "User32.dll" (ByRef phicon As IntPtr) As Boolean
        Public Declare Function ExtractIconEx Lib "shell32.dll" _
        (ByVal lpszFile As String, ByVal nIconIndex As Integer, _
        ByRef phiconLarge As IntPtr, ByRef phiconSmall As IntPtr, ByVal nIcons As Integer) As IntPtr
#End Region

        Private Function getAppIcon() As Icon
            Dim iconPtr As IntPtr
            Dim myIcon As Icon
            ExtractIconEx(Application.ExecutablePath, 0, iconPtr, Nothing, 1)
            '-> returnvalue is number of icons found, so normally check for > 0
            myIcon = Icon.FromHandle(iconPtr)
            getAppIcon = myIcon
            'when "finished using" the iconPtr:
            DestroyIcon(iconPtr)
        End Function
---
This code is working as expected, but it is using API calls.

I thought that there is way to do this without API in pure Vb.Net.

But - it is working!

  Thanks again

    HStrix



I dont think there would be a way using the assembly

Glad to help :)