Solved

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

Posted on 2004-10-14
13
650 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

630 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