Solved

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

Posted on 2004-10-14
13
636 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
  • 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now