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
Solved

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

Posted on 2004-10-14
13
642 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…

792 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