embedded images in .net?

  • Thread starter Thread starter DRAT
  • Start date Start date
D

DRAT

Guest
I would like to embedd images into my exe in visual basic.net

If I can't figure out how to do this then how can I reference the directory from which my executable was run, so that I can access images in the game folder?

Embedding-
I tried to add existing object, browse to image, then after its added to the project, go to its properties and set "build action" to "embedded resource." Then, according to two tutorials I found I just need to import system.resources and use the following code to access the images from the resources file-

Dim rm As New ResourceManager("Chess.form1", Me.GetType().Assembly)
button1.image = CType(rm.GetObject("1234.bmp"), System.Drawing.Image)

One tutorial said to reference the image as 1234_bmp rather than 1234.bmp but either way it references nothing. It doesn't error but no image appears on the button and the image.height returns null. The chess.form1.resources file I'm attempting to access is found in the obj\debug dir. That is the only .resources file I could find.

If you could please tell me how to embedd images or at least how to access files relative to the path of the executable wi oudl papreciate it. Keep in mind that this is Visual Basic.net

Thx
Daniel
 
Add the image to your project as normal (don't worry about copying it anywhere - .NET will copy it to your project folder when you add it). Select the BMP file (or whatever type it is) and change it's "Build Action" to "Embedded Resource".

Now you'll want to use the Assembly's GetManifestResourceStream method to load the embedded resource. It returns a Stream object, which can be passed to a Bitmap object to get the image.

You can use something like the following. Note that the name of the file has the namespace attached to it ("WinTest" in my sample):
(this may not be right, I'm typing from memory):
' Assume this is loading the form's background image
Me.BackgroundImage = New Bitmap(Me.GetType().Assembly.GetManifestResourceStream("WinTest.test.bmp")

Hope that helps!
If it still doesn't work, let me know. I use C#, but I can probably wiggle it out in VB.NET :)

-nerseus
 
When I try that I get "Null is not a valid value for stream." I wasn't sure wether to use just chess.blah.bmp or chess.form1.blah.bmp but either way returns the same error. Any suggestions?

Or even a way to just reference the windows path relative to the path the program was executed from? That way I could just keep the images in the same directory as the executable and not worry about moving the folder.
 
You can put the images in the same folder as the executable, sure. They won't be embedded in the EXE, but that's fine (probably better, actually). For development, just put the image in \bin\Debug or \bin\Release then reference the picture without any path information, such as:
button1.Image = New Bitmap("blah.bmp")

Or you could store them in a subfolder (such as \bin\Debug\images) and use:
button1.Image = New Bitmap("images\\blah.bmp")
(The double backslash is used in C#, not sure about VB)

When you give someone your EXE, just create a subfolder named "images" under the location of the EXE.

Yet another alternative is to have a dummy form that contains the pictures embedded already (or in a PictureBox or multiple PictureBoxes on your form). Set the Image property at design time and .NET will embed the picture in your RESX file. When you compile to an EXE, the picture will get embedded automatically. Then you can do something like:
button1.Image = picBox1.Image

Hope that helps!

-nerseus
 
Attached is a sample VB.NET project that shows how to reference an embedded image file.

To recreate, do this:
Create a new project named "embedded image".
Add the file "testimage.bmp"
For the image's properties, change it's "Build Action" to "Embedded Resource".

In Form_Load, add the following:
' The project name is "embedded image" - look at Solution Explorer to see it
' You must reference the image by "namespace.filename.extension".
' The namespace can be seen by looking at the project properties. In this case,
' it's "embedded_image". The underscore was added because the project name has a space
' The filename is "testimage.bmp"
' Together, they form: "embedded_image.testimage.bmp"
Me.Button1.Image = New Bitmap(Me.GetType().Assembly.GetManifestResourceStream("embedded_image.testimage.bmp"))


Obviously, you can leave out the comments if you want :)

Hope that helps!
-nerseus
 
Thx
That code you supplied does work. Its the same code that I listed above but I discovered why it isn't working for me. Using VB i got into the habbit of not being case sensitive. Unfortunately referrencing resources requires case sensitivity so I was trying to acess wKing.gif when the resource was wking.gif. Yet another reason for everyone in the world to just stick with C so that cross language confusion like this doesn't happen :)

Thx again
DRAT
 
Back
Top