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.F orms.Appli cation.Sta rtupPath & "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
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
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
ASKER
Thanks,
I get the message "The type Assembly is not defined."
I get the message "The type Assembly is not defined."
you have to import system.reflection
ASKER
Now I get for statement
myAppIcon = ctype(_assembly.GetManifes tResourceS tream("You rNamespace .Applicati onIconName .ico"),Ico n)
the message
The value of type 'System.IO.Stream' cannot be converted to 'System.Drawing.Icon'.
myAppIcon = ctype(_assembly.GetManifes
the message
The value of type 'System.IO.Stream' cannot be converted to 'System.Drawing.Icon'.
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.
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.GetManifestResou rceStream ("YourNamespace.Applicatio nIconName. ico")
myAppIcon = new System.Drawing.Icon(ResStr eam)
dim resStream as System.IO.Stream ResStream = _assembly.GetManifestResou
myAppIcon = new System.Drawing.Icon(ResStr
sorry the first line should be
dim resStream as System.IO.Stream = _assembly.GetManifestResou rceStream ("YourNamespace.Applicatio nIconName. ico")
dim resStream as System.IO.Stream = _assembly.GetManifestResou
ASKER
OK,
now the code looks like
Imports System.Threading
Imports System.Reflection
Namespace NamespaceTest
Public Class frmTest
.....
Dim _assembly As [Assembly] = [Assembly].GetExecutingAss embly()
Dim myAppIcon As Icon
Dim resStream As System.IO.Stream = _assembly.GetManifestResou rceStream( "Namespace Test.Appli cationIcon Name.ico")
myAppIcon = New System.Drawing.Icon(resStr eam) ' <=== 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'
now the code looks like
Imports System.Threading
Imports System.Reflection
Namespace NamespaceTest
Public Class frmTest
.....
Dim _assembly As [Assembly] = [Assembly].GetExecutingAss
Dim myAppIcon As Icon
Dim resStream As System.IO.Stream = _assembly.GetManifestResou
myAppIcon = New System.Drawing.Icon(resStr
Me.Icon = myAppIcon
.....
End Class
End Namespace
the message
Unknown exception of type 'System.ArgumentException'
Additional information: 'null' is not valid for 'stream'
ASKER
Execution of
Dim resStream As System.IO.Stream = _assembly.GetManifestResou
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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. Executable Path, 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
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.
'-> 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 :)
Glad to help :)
Dim _assembly As [Assembly] = [Assembly].GetExecutingAss
Dim myAppIcon As Icon
dim myAppIcon = new Icon()
myAppIcon = ctype(_assembly.GetManifes
Me.Icon = myAppIcon