By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,556 Members | 1,663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,556 IT Pros & Developers. It's quick & easy.

Accessing embedded resources

P: n/a
Hi all,

I've created a custom button which I can stick into my toolbox. This all
works well and I see my button in the designer. This button uses
customised graphics and I want to stick it in the dll as an embedded
resource. In the project tree I added a folder in which I stick the
bitmaps (png's actually) and set he compile option for them to
EmbeddedResource. But how do I access them from my (vb)source?

The docs say:

new bitmap(type, resourcename)

but what is the resourcename? Where do I find that? I've tried just
entering the filename, but that gives me an error saying it can't be
located in the namespace of my custom button.
--
Rinze van Huizen
C-Services Holland b.v
Feb 8 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

The resource name is <namespace.name>

For example, if you have a bitmap, bar.bmp embedded in a project with a
namespace of foo. Then the resource name is foo.bar.bmp

NOTES: The resource name is case sensitive
It is the namespace that is important not the project name
- by default they are the same but may not be

I have seen the

new bitmap(type, resourcename)

version before and cannot promise that it will work.

I generally use

Dim bmp As New
System.Drawing.Bitmap(System.Reflection.Assembly.G etExecutingAssembly.GetManifestResourceStream
_

(<resourcename>))

hth,
Alan.

Feb 8 '06 #2

P: n/a
Here's a class that I use to retrieve different types of resources (note that
the resource names are case sensitive including any .extension, i.e., .ICO is
different from .ico).

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
"C-Services Holland b.v." wrote:
Hi all,

I've created a custom button which I can stick into my toolbox. This all
works well and I see my button in the designer. This button uses
customised graphics and I want to stick it in the dll as an embedded
resource. In the project tree I added a folder in which I stick the
bitmaps (png's actually) and set he compile option for them to
EmbeddedResource. But how do I access them from my (vb)source?

The docs say:

new bitmap(type, resourcename)

but what is the resourcename? Where do I find that? I've tried just
entering the filename, but that gives me an error saying it can't be
located in the namespace of my custom button.
--
Rinze van Huizen
C-Services Holland b.v

Feb 9 '06 #3

P: n/a
al*******@users.com wrote:
The resource name is <namespace.name>

For example, if you have a bitmap, bar.bmp embedded in a project with a
namespace of foo. Then the resource name is foo.bar.bmp

NOTES: The resource name is case sensitive
It is the namespace that is important not the project name
- by default they are the same but may not be

I have seen the

new bitmap(type, resourcename)

version before and cannot promise that it will work.

I generally use

Dim bmp As New
System.Drawing.Bitmap(System.Reflection.Assembly.G etExecutingAssembly.GetManifestResourceStream(<res ourcename>))


Thank you both, that did the trick. I'm keeping Denis' class for future
reference since it was a bit overkill for this particular instance.

--
Rinze van Huizen
C-Services Holland b.v
Feb 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.