Icon files

L

Lou

if I add a new item (Solution Items) to my project and its an icon(.ico),
how can I reference that file when I am coding, Do I have to also add it to
an image control, I don't want the file to be part of the .resx file.
Its like VB6 when I could use load all my images from a resource file.
Lou
 
G

Guest

if I add a new item (Solution Items) to my project and its an icon(.ico),
how can I reference that file when I am coding

The easiest thing to do is as follows.

1. Add the .ico file(s) to your project.
2. For each icon file, set the build action to "embedded resource". This
will include the .ico file in your .exe file (my direct experience is only
with windows form .exe files, but I expect this will work with other app
types).
3. In your program, you can get an icon object via the code fragment shown
below. If the name of the icon file xxx.ico, and the name of your app is
yyy, then the name of the icon resource in your .exe file is "yyy.xxx.ico".

dim s as string = "yyy.xxx.ico" ' your names replace yyy and xxx
dim MyIcon = New
Icon(System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(s))

Now you can set a form's icon like: someform.Icon=MyIcon.
 
L

Lou

Where do I set the build Action?


AMercer said:
The easiest thing to do is as follows.

1. Add the .ico file(s) to your project.
2. For each icon file, set the build action to "embedded resource". This
will include the .ico file in your .exe file (my direct experience is only
with windows form .exe files, but I expect this will work with other app
types).
3. In your program, you can get an icon object via the code fragment
shown
below. If the name of the icon file xxx.ico, and the name of your app is
yyy, then the name of the icon resource in your .exe file is
"yyy.xxx.ico".

dim s as string = "yyy.xxx.ico" ' your names replace yyy and xxx
dim MyIcon = New
Icon(System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(s))

Now you can set a form's icon like: someform.Icon=MyIcon.
 
L

Lou

Under "Solutions Items" folder I have 1 file
MyIcon.ico
There is no property to set the build action for that icon that I can find?
 
C

CT

You need to add it to the project and not the solution, as it will compiled
as part of the project assembly.
 
L

Lou

I managed to add the icon to the proj and set its action to embedded but the
code
dim MyIcon = New
Icon(System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(s))
returns null
 
H

Herfried K. Wagner [MVP]

Lou said:
Under "Solutions Items" folder I have 1 file
MyIcon.ico
There is no property to set the build action for that icon that I can
find?

Add the icon to the /project/, select it in solution explorer and set the
build action in the property window.
 
L

Lou

This method does not seem to work or I can't get it to work?

I added an icon file to my project
I set it to "embedded resource" and added the code below in the proper
format but
it returns null.
There doesn't seem to be anything else to look for?

There must be something missing?

-Lou
 
C

CT

Make sure s contains the fully namespace qualified name of the icon file,
i.e. if the name of your project is Test (assuming you didn't specify a
different namespace) and the name of the file is res.ico, then assign
"Test.res.ico" to s.
 
G

Guest

Try this class to retrieve embedded resources. Note that the names of the
files are CASE sensitive including the .ico or .bmp endings.

Imports System
Imports System.Reflection
Imports System.Drawing
Imports System.Windows.Forms

Public Class Resources
'WARNING: Icon and Image Names are Case Sensistive
Private Shared Function AssemblyName() As String
Static v_AssemblyName As String
If v_AssemblyName Is Nothing Then v_AssemblyName =
System.Reflection.Assembly.GetExecutingAssembly().GetName.Name & "."
Return v_AssemblyName
End Function
Public Shared Function GetIcon(ByVal IconName As String) As Icon
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim v_name As String = IconName
If InStr(v_name.ToLower, ".ico") <= 0 Then v_name = v_name & ".ico"
Dim Name As String = AssemblyName() & v_name
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Icon(s)
s.Close()
End If
End Function

Public Shared Function GetImage(ByVal BitMapName As String) As Bitmap
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim v_name As String = BitMapName
If InStr(v_name.ToLower, ".bmp") <= 0 Then v_name = v_name & ".bmp"
Dim Name As String = AssemblyName() & v_name
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Bitmap(s)
s.Close()
End If
End Function

Public Shared Function GetCursor(ByVal CursorName As String) As Cursor
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim v_name As String = CursorName
If InStr(v_name.ToLower, ".cur") <= 0 Then v_name = v_name & ".cur"
Dim Name As String = AssemblyName() & v_name
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Cursor(s)
s.Close()
End If
End Function
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
 
A

Alec MacLean

Thats a handy tip, Thanks!

I'd been trying all sorts of methods for swapping the icons for my
statusbarpanels - this did the job nicely.

Alec
 
L

Lou

I tried this class and I added code to retrieve the .ico file but it ruturns
"Nothing".
I have added an icon file under my project and it shows up as "Security.ico"
and I have set
the build Action to Embedded Resource. The File Name is "Security.ico"

Here is my form level code that uses your nice class.
Dim Res As New CResource

Dim myIconS As Icon

myIconS = Res.GetIcon("Security")

Me.Icon = myIconS



Dennis said:
Try this class to retrieve embedded resources. Note that the names of the
files are CASE sensitive including the .ico or .bmp endings.

Imports System
Imports System.Reflection
Imports System.Drawing
Imports System.Windows.Forms

Public Class Resources
'WARNING: Icon and Image Names are Case Sensistive
Private Shared Function AssemblyName() As String
Static v_AssemblyName As String
If v_AssemblyName Is Nothing Then v_AssemblyName =
System.Reflection.Assembly.GetExecutingAssembly().GetName.Name & "."
Return v_AssemblyName
End Function
Public Shared Function GetIcon(ByVal IconName As String) As Icon
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim v_name As String = IconName
If InStr(v_name.ToLower, ".ico") <= 0 Then v_name = v_name & ".ico"
Dim Name As String = AssemblyName() & v_name
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Icon(s)
s.Close()
End If
End Function

Public Shared Function GetImage(ByVal BitMapName As String) As Bitmap
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim v_name As String = BitMapName
If InStr(v_name.ToLower, ".bmp") <= 0 Then v_name = v_name & ".bmp"
Dim Name As String = AssemblyName() & v_name
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Bitmap(s)
s.Close()
End If
End Function

Public Shared Function GetCursor(ByVal CursorName As String) As Cursor
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim v_name As String = CursorName
If InStr(v_name.ToLower, ".cur") <= 0 Then v_name = v_name & ".cur"
Dim Name As String = AssemblyName() & v_name
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Cursor(s)
s.Close()
End If
End Function
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
--
Dennis in Houston


Lou said:
This method does not seem to work or I can't get it to work?

I added an icon file to my project
I set it to "embedded resource" and added the code below in the proper
format but
it returns null.
There doesn't seem to be anything else to look for?

There must be something missing?

-Lou
 
G

Guest

Some of the errors I"ve made when it returns nothing are:

The Case of either the name or extension was different between the file name
and the name I passed to the class (be sure to check the extension case,
i.e., ICO or ico that they are the same.

When I have multiple projects under one solution, I have to start the
project in a separate solution that I want to add the icon to by itself then
add the icon and set the build to "embedded". Then rebuild the project. Then
restart my solution with multiple projects.


--
Dennis in Houston


Lou said:
I tried this class and I added code to retrieve the .ico file but it ruturns
"Nothing".
I have added an icon file under my project and it shows up as "Security.ico"
and I have set
the build Action to Embedded Resource. The File Name is "Security.ico"

Here is my form level code that uses your nice class.
Dim Res As New CResource

Dim myIconS As Icon

myIconS = Res.GetIcon("Security")

Me.Icon = myIconS



Dennis said:
Try this class to retrieve embedded resources. Note that the names of the
files are CASE sensitive including the .ico or .bmp endings.

Imports System
Imports System.Reflection
Imports System.Drawing
Imports System.Windows.Forms

Public Class Resources
'WARNING: Icon and Image Names are Case Sensistive
Private Shared Function AssemblyName() As String
Static v_AssemblyName As String
If v_AssemblyName Is Nothing Then v_AssemblyName =
System.Reflection.Assembly.GetExecutingAssembly().GetName.Name & "."
Return v_AssemblyName
End Function
Public Shared Function GetIcon(ByVal IconName As String) As Icon
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim v_name As String = IconName
If InStr(v_name.ToLower, ".ico") <= 0 Then v_name = v_name & ".ico"
Dim Name As String = AssemblyName() & v_name
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Icon(s)
s.Close()
End If
End Function

Public Shared Function GetImage(ByVal BitMapName As String) As Bitmap
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim v_name As String = BitMapName
If InStr(v_name.ToLower, ".bmp") <= 0 Then v_name = v_name & ".bmp"
Dim Name As String = AssemblyName() & v_name
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Bitmap(s)
s.Close()
End If
End Function

Public Shared Function GetCursor(ByVal CursorName As String) As Cursor
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
Dim v_name As String = CursorName
If InStr(v_name.ToLower, ".cur") <= 0 Then v_name = v_name & ".cur"
Dim Name As String = AssemblyName() & v_name
Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
If Not s Is Nothing Then
Return New Cursor(s)
s.Close()
End If
End Function
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
--
Dennis in Houston


Lou said:
This method does not seem to work or I can't get it to work?

I added an icon file to my project
I set it to "embedded resource" and added the code below in the proper
format but
it returns null.
There doesn't seem to be anything else to look for?

There must be something missing?

-Lou

if I add a new item (Solution Items) to my project and its an
icon(.ico),
how can I reference that file when I am coding

The easiest thing to do is as follows.

1. Add the .ico file(s) to your project.
2. For each icon file, set the build action to "embedded resource".
This
will include the .ico file in your .exe file (my direct experience is
only
with windows form .exe files, but I expect this will work with other
app
types).
3. In your program, you can get an icon object via the code fragment
shown
below. If the name of the icon file xxx.ico, and the name of your app
is
yyy, then the name of the icon resource in your .exe file is
"yyy.xxx.ico".

dim s as string = "yyy.xxx.ico" ' your names replace yyy and xxx
dim MyIcon = New
Icon(System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(s))

Now you can set a form's icon like: someform.Icon=MyIcon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top