How to simply load a freeekin bitmap from a resource?

  • Thread starter Thread starter _BBB
  • Start date Start date
B

_BBB

I'm coming over from a background in VC++, and in general I've been
pleased with the simplicity of the .NET environment. Not here.

I've done google searches on this subject (loading bitmaps from a local
resource) and always come up with complex solutions. This can't be right.
Everyone packs bitmaps and other graphics and resources, integral to small
executables, right?

Setting up a local resource folder, loading in graphics, and setting their
properties to 'Embedded Resource' would seem to incorporate them, as
expected.

Then what? It's tough to make 'FromResource' happy. There's got to be
some simple solution to this.
 
_BBB...

I have ran into this problem before. The solution is not complicated, but
is hard to locate in the documentation. Here is what I have found and it
works for me...

I have added a folder to my project named "Images" and in this folder, I
have added a few JPG files. I then set the Build Action property for each
JPG file to "Embedded Resource". I placed this code on the load of a form
to set the Background image of the form to one of the JPG images (the one
named Sunset.jpg).
Dim thisExe As System.Reflection.Assembly
thisExe = System.Reflection.Assembly.GetExecutingAssembly()
Dim file As System.IO.Stream =
thisExe.GetManifestResourceStream(thisExe.GetName.Name &
".Sunset.jpg")Me.BackgroundImage = Image.FromStream(file)


Note that the function GetManifestResourceStream uses case sensitive
parameters. Therefore, ".Sunset.jpg" is not the same as ".sunset.jpg".

Here is some code that will cycle through the list of embedded resources.

Dim thisExe As System.Reflection.Assembly
thisExe = System.Reflection.Assembly.GetExecutingAssembly()

Dim resources() As String = thisExe.GetManifestResourceNames()
Dim resource As String = ""

For Each resource In resources
Debug.WriteLine(resource & Microsoft.VisualBasic.ControlChars.CrLf)
Next

I hope this helps.

Roger.Smith
 
I've done google searches on this subject (loading bitmaps from a local
resource) and always come up with complex solutions. This can't be right.
Everyone packs bitmaps and other graphics and resources, integral to small
executables, right?

Right. That's why Microsoft created some special Icon and Bitmap
constructors, just for you! :-)

To create an icon from an embedded icon bitmap:
Icon myIcon = new Icon(typeof(seeBelow), "NameOfMyIcon.ico")

To create some other bitmap from an embedded bitmap:
Bitmap myBitmap = new Bitmap(typeof(seeBelow), "NameOfMyBitmap.bmp")

The "seeBelow" type is some arbitrary type in the assembly that
contains the icon or bitmap with the specified name as an embedded
resource. That's it!

Look up the various Icon and Bitmap constructors for more details.
 
Back
Top