Icon as embedded resource

G

Garrett

I'm fairly new to VB .NET and programming in general, and I want to
give my program its own icon without it being a separate file. I have
the icon as part of the project, and its Build Action is set to
Embedded Resource. I've searched high and low for the code that will
actually make it load as the program's default icon, but nothing I've
found works. Can someone give me the code necessary to do this?
 
M

m@rio

Uzytkownik "Garrett"
<thisisarealaddressbutidontuseit@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopq
rstuvwxyzabcdefghijk.com> napisal w wiadomosci
I'm fairly new to VB .NET and programming in general, and I want to
give my program its own icon without it being a separate file. I have
the icon as part of the project, and its Build Action is set to
Embedded Resource. I've searched high and low for the code that will
actually make it load as the program's default icon, but nothing I've
found works. Can someone give me the code necessary to do this?

here you are

'''<summary>
''' Reads icon from embeded resource
''' </summary>
Public Shared Function readResourceIcon(ByVal IconName As String) As
Icon
' Get our assembly.
Dim executing_assembly As System.Reflection.Assembly = _
fMain.GetType.Assembly.GetEntryAssembly()

Dim picture_stream As Stream
Dim iconReaded As Icon

picture_stream =
executing_assembly.GetManifestResourceStream(IconName)
If Not (picture_stream Is Nothing) Then
iconReaded = New Icon(picture_stream)
picture_stream.Close()
Return iconReaded
End If
End Function

I hope this helps
Mario
 
B

Bob

Right-click on your project in Solution Explorer and select 'Properties'.
Click on the 'build' item under 'common properties'. You will be able to
chose an application icon there. If your project builds a DLL you will need
to temporarily set it to an EXE first.

HTH,
Bob

"Garrett"
<thisisarealaddressbutidontuseit@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopq
rstuvwxyzabcdefghijk.com> wrote in message
news:[email protected]...
 
G

Garrett

Thanks for your help, but I'm getting these errors with the code:

"Name 'fMain' is not declared."
"Type 'Stream' is not defined."
And, for Return iconReaded: "Value of type 'System.Drawing.Icon' cannot
be converted to '1-dimensonal array of System.Drawing.Icon'."
 
G

Guest

Try the below functions for getting different types of embedded images.

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
 
G

Garrett

Dennis,

Visual Studio won't let me Dim anything as type [Assembly] ("Type
[Assembly] is not defined"). Other than that, it doesn't catch any
errors with your code.
 
G

Guest

I am using VB.Net 2003 Professional Version and the following is directly out
of the help sheet on "Assembly" Class:

Public Class LoadInvoke
Public Shared Sub Main(ByVal args() As String)
Dim a As [Assembly] = [Assembly].LoadFrom(args(0))
Dim mytypes As Type() = a.GetTypes()

Are you sure you have the correct imports statements? Also, you might check
the references. You should have at least the following:

System
System.Data
System.Drawing
System.Management
System.XML
System.Widnows.Forms

You may not need all of these but these are the references I have in the
module containing the resource routines.
 
G

Garrett

I'm using Microsoft Development Environment 2003 with .NET Framework
1.1. Not sure what you mean by imports statements - can't find any. I
have all the references you mentioned except System.Management.
 
G

Guest

By Imports, I mean the following at the top of your module, form, etc.
before any Public Class, etc. statements.

Imports System
Imports System.Reflection
Imports System.Drawing
Imports System.Windows.Forms
Also, you need to add a reference to System.Management
 

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