Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2004

Tip: Looking for answers? Try searching our database.

Using Embedded Resources - For Newbies

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dennis - 23 Oct 2004 14:11 GMT
Thought the group might be interested in the following class(modified from an
example I found on this newsgroup) for using embedded resources such as
icons, bitmaps, etc.  Suppose you add an icon named "Icon1" to a project and
embed it in the project by select "embedded Resource" from right clicking on
the icon1.ico in the solution Explorer listing.  The following class can be
used to retrieve the icon image for use in the project:

'Retrieve the embedded icon
dim r as new resource
dim ic as icon
ic = r.GetIcon("Icon1")

'Resource Class
Imports System.Reflection
Public Class Resources
   Shared v_AssemblyName As String
   Shared initialize As Boolean
   Public Sub New()
       If Not initialize Then
           Dim thisExe As System.Reflection.Assembly = _
System.Reflection.Assembly.GetExecutingAssembly()
           v_AssemblyName = thisExe.GetName.Name & "."
           initialize = True
       End If
   End Sub

   Public Function GetIcon(ByVal IconName As String) As Icon
       Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
       Dim Name As String = v_AssemblyName & IconName & ".ico"
       Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
       If Not s Is Nothing Then
           Return New Icon(s)
           s.Close()
       Else
           Return Nothing
       End If
   End Function

   Public Function GetImage(ByVal ImageName As String) As Image
       Dim asm As [Assembly] = [Assembly].GetExecutingAssembly
       Dim Name As String = v_AssemblyName & ImageName & ".bmp"
       Dim s As System.IO.Stream = asm.GetManifestResourceStream(Name)
       If Not s Is Nothing Then
           Return New Bitmap(s)
           s.Close()
       Else
           Return Nothing
       End If
   End Function

End Class
Signature

Dennis in Houston

One Handed Man \( OHM - Terry Burns \) - 23 Oct 2004 14:33 GMT
Nice one, I'm going to use that in an application I'm writing.

Thanks for sharing it with the group :)

Signature

OHM ( Terry Burns )   * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
    ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--

> Thought the group might be interested in the following class(modified from
> an
[quoted text clipped - 51 lines]
>
> End Class
Dennis - 23 Oct 2004 16:45 GMT
You're welcome.  Hope we have gracefully agreed to disagree on the spelling
issue...we all have our own opinions.

"One Handed Man ( OHM - Terry Burns )" wrote:

> Nice one, I'm going to use that in an application I'm writing.
>
[quoted text clipped - 55 lines]
> >
> > End Class
One Handed Man \( OHM - Terry Burns \) - 23 Oct 2004 18:06 GMT
I don't remember what spelling issues your talking about, unless your they
guy I had a big argument with about how important spelling was some two or
three months ago, in which case jus ignore me as I was simply being awkward
for the hell of it.

Cheers

Signature

OHM ( Terry Burns )   * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
    ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--

> You're welcome.  Hope we have gracefully agreed to disagree on the
> spelling
[quoted text clipped - 65 lines]
>> >
>> > End Class
Dennis - 23 Oct 2004 20:27 GMT
I'm the one...cheers.

"One Handed Man ( OHM - Terry Burns )" wrote:

> I don't remember what spelling issues your talking about, unless your they
> guy I had a big argument with about how important spelling was some two or
[quoted text clipped - 72 lines]
> >> >
> >> > End Class
Larry Serflaten - 23 Oct 2004 16:45 GMT
> Thought the group might be interested in the following class(modified from an
> example I found on this newsgroup) for using embedded resources such as
[quoted text clipped - 7 lines]
> dim ic as icon
> ic = r.GetIcon("Icon1")

That's a good idea.  Might I suggest a little improvemant may make it more
versatile.  As you show, you have to create the resource before you can use it,
but it may be useful to use it in this manner:

   Private bmp As Bitmap = MyResource.GetBitmap("mountains")

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                         Handles MyBase.Load
       Me.Icon = MyResource.GetIcon("Mountain.ico")
   End Sub

   Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
                                          Handles MyBase.Paint
       e.Graphics.DrawImageUnscaled(bmp, 0, 0)
   End Sub

Note the bmp variable is declared at the module level, where the resource class
could not be created....  To cover that usage, you could make those functions
shared also.  Here is another go at that class with a few additions:

HTH
LFS

Public Class MyResource

   Public Shared Function AssemblyName() As String
       Return System.Reflection.Assembly.GetExecutingAssembly.GetName.Name
   End Function

   Public Shared Function GetIcon(ByVal Name As String) As System.Drawing.Icon
       ' *** NAME is CASE SENSITIVE  ***
       If Name.Length > 0 Then
           Dim Filename As String
           If InStr(LCase(Name), ".ico") > 0 Then
               Filename = Name
           Else
               Filename = Name & ".ico"
           End If
           Dim res As System.IO.Stream = [Assembly].GetExecutingAssembly.GetManifestResourceStream( _
                                          AssemblyName() & "." & Filename)
           If Not res Is Nothing Then
               GetIcon = New Icon(res)
               res.Close()
           End If
       End If
   End Function

   Public Shared Function GetBitmap(ByVal Name As String) As System.Drawing.Bitmap
       ' *** NAME is CASE SENSITIVE  ***
       If Name.Length > 0 Then
           Dim Filename As String
           If InStr(LCase(Name), ".bmp") > 0 Then
               Filename = Name
           Else
               Filename = Name & ".bmp"
           End If
           Dim res As System.IO.Stream = [Assembly].GetExecutingAssembly.GetManifestResourceStream( _
                                          AssemblyName() & "." & Filename)

           If Not res Is Nothing Then
               GetBitmap = DirectCast(Bitmap.FromStream(res), Bitmap)
               res.Close()
           End If
       End If
   End Function

End Class
Dennis - 23 Oct 2004 20:35 GMT
Thanks...that does make it easier to use.  I'm just not a very experienced
programmer (just a hobbiest and not much time to devote to it).

> > Thought the group might be interested in the following class(modified from an
> > example I found on this newsgroup) for using embedded resources such as
[quoted text clipped - 75 lines]
>
> End Class
Dennis - 23 Oct 2004 20:59 GMT
Larry, just one question.  You noted that the name was case sensitive.  Was
this only to determine if the name had the .ico or .bmp extension or does
that mean that the whole resource name is Case Sensitive, i.e., will
"icon1.ico" get a resource that was loaded named "ICON1.ICO"?  Thanks again
for the tips.

> > Thought the group might be interested in the following class(modified from an
> > example I found on this newsgroup) for using embedded resources such as
[quoted text clipped - 75 lines]
>
> End Class
Larry Serflaten - 23 Oct 2004 22:19 GMT
"Dennis" <Dennis@discussions.microsoft.com> wrote
> Larry, just one question.  You noted that the name was case sensitive.  Was
> this only to determine if the name had the .ico or .bmp extension or does
> that mean that the whole resource name is Case Sensitive, i.e., will
> "icon1.ico" get a resource that was loaded named "ICON1.ICO"?  Thanks again
> for the tips.

The name has to exactly match the name of the resource.  In this case
'icon.ico' and 'ICON.ico' will not match and the method will fail.  That's
why I thought it important to  mention it right there in the methods....

LFS
Dennis - 23 Oct 2004 23:43 GMT
Yeah, I found that out by trial and error.  However, the case of the
extension it seems needs to be in lower case.

> "Dennis" <Dennis@discussions.microsoft.com> wrote
> > Larry, just one question.  You noted that the name was case sensitive.  Was
[quoted text clipped - 8 lines]
>
> LFS

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.