Solved

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

Posted on 2004-10-14
13
646 Views
Last Modified: 2010-04-24
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
0
Comment
Question by:HStrix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 9

Expert Comment

by:checoo
ID: 12316591
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
0
 

Author Comment

by:HStrix
ID: 12316642
Thanks,
I get the message "The type Assembly is not defined."
0
 
LVL 9

Expert Comment

by:checoo
ID: 12316649
you have to import system.reflection
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:HStrix
ID: 12316670
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'.
0
 

Author Comment

by:HStrix
ID: 12316684
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.

0
 
LVL 9

Expert Comment

by:checoo
ID: 12316699
try the following

dim resStream as System.IO.Stream ResStream = _assembly.GetManifestResourceStream ("YourNamespace.ApplicationIconName.ico")
 myAppIcon = new System.Drawing.Icon(ResStream)
0
 
LVL 9

Expert Comment

by:checoo
ID: 12316726
sorry the first line should be

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

Author Comment

by:HStrix
ID: 12317282
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'

0
 

Author Comment

by:HStrix
ID: 12317331

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

gives as result: resStream = Nothing
0
 
LVL 9

Expert Comment

by:checoo
ID: 12317645
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
0
 
LVL 18

Accepted Solution

by:
armoghan earned 500 total points
ID: 12318198
0
 

Author Comment

by:HStrix
ID: 12318751
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



0
 
LVL 18

Expert Comment

by:armoghan
ID: 12318924
I dont think there would be a way using the assembly

Glad to help :)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question