Home All Groups Group Topic Archive Search About

Accessing embedded resources

Author
8 Feb 2006 2:46 PM
C-Services Holland b.v.
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

Author
8 Feb 2006 2:54 PM
alantolan
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.GetExecutingAssembly.GetManifestResourceStream
_

(<resourcename>))

hth,
Alan.
Author
10 Feb 2006 2:14 PM
C-Services Holland b.v.
alanto***@users.com wrote:
Show quoteHide quote
> 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.GetExecutingAssembly.GetManifestResourceStream(<resourcename>))

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
Author
9 Feb 2006 12:51 AM
Dennis
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


Show quoteHide quote
"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
>