How can I access images in the Resources folder?

  • Thread starter Thread starter Cooz
  • Start date Start date
C

Cooz

Hi everyone,

I have several .gif images in my Resources folder. I want a user to be able
to select a name in a ComboBox and then have the corresponding .gif displayed
in a PictureBox. I thought this sub would do the trick:

Private Sub KaartenComboBox_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
KaartenComboBox.SelectedIndexChanged
Try
Me.PictureBox1.Image =
System.Drawing.Image.FromFile(Me.KaartenComboBox.Text & ".gif")
Catch ex As Exception
If ex.ToString = "FileNotFoundException" Then
Me.PictureBox1.Image = Me.PictureBox1.ErrorImage
End If
End Try
End Sub

But no: nothing happens. Apparently VB does not look in the resources folder
when I don't provide a path. Which lines of code should I use instead?

I use VB 2005.

Thank you,
Cooz
 
Hello,

Usually a "resource" is embedded in the EXE file and is read directly from
the EXE file. This way a single file contains all is needed for your
application (including icons etc...). This is the intended usage for the
Resources directory.

If you are just reading a random file from the disk, AFAIK it will never
look automatically somewhere else than in the current directory for this
process as your app knows nothing about this file.

So :
- if this file is supposed to be in a folder under the current directory
you'll have to specify the folder name
- if this file is supposed to be in a folder under the folder that contains
the EXE file it's best to include a full name (Application.StartupPath will
return the EXE location)
- make sure also you used the appropriate option in VS for this file for the
"Copy to output" directory so that the file is copied when you build your
EXE

It's perhaps better to use another folder to better manage your file if you
wish one day to have both external files and embedded resources in your EXE
file...
 
Hi everyone,

I have several .gif images in my Resources folder. I want a user to be able
to select a name in a ComboBox and then have the corresponding .gif displayed
in a PictureBox. I thought this sub would do the trick:

    Private Sub KaartenComboBox_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
KaartenComboBox.SelectedIndexChanged
        Try
            Me.PictureBox1.Image =
System.Drawing.Image.FromFile(Me.KaartenComboBox.Text & ".gif")
        Catch ex As Exception
            If ex.ToString = "FileNotFoundException" Then
                Me.PictureBox1.Image = Me.PictureBox1.ErrorImage
            End If
        End Try
    End Sub

But no: nothing happens. Apparently VB does not look in the resources folder
when I don't provide a path. Which lines of code should I use instead?

I use VB 2005.

Dim assembly as Assembly
Dim stream as Stream
assembly = System.Reflection.Assembly.GetExecutingAssembly()
stream = assembly.GetManifestResourceStream
("<yourprojectname>.filename.gif")

You can take it from there can't you?
 
Dim assembly as Assembly
Dim stream as Stream
assembly = System.Reflection.Assembly.GetExecutingAssembly()
stream = assembly.GetManifestResourceStream
("<yourprojectname>.filename.gif")

You can take it from there can't you?

After thinking about it, you may need to change the names of the
variables assembly and stream since VB is case INsensitive.

I was keying that in from a C# project I wrote.
 
Dim assembly as Assembly
Dim stream as Stream
assembly = System.Reflection.Assembly.GetExecutingAssembly()
stream = assembly.GetManifestResourceStream
("<yourprojectname>.filename.gif")

You can take it from there can't you?

Another followup comment. Not sure about the filename reference, but
I'm pretty sure the "<yourprojectname>" is.

Also, project resources are not stored as individual files in a folder
somewhere. The are all converted to ASCII and stored in a resource
file, the default resource file is Resources.resx, and in actuality,
an XML file. Resources contained in the default resorce file can be
obtained from the current executing assembly as illustrated above.

An alternative, if you want to be able to add resources as run time,
is to use a custom resource file. To store resources in it you would
(sorry, this is in C#):

ResXResourceWriter resxWriter;

if (File.Exists(Application.StartupPath + "\\myCustomResources.resx"))
{
File.Delete(Application.StartupPath + "\\myCustomResources.resx");
}
resxWriter = new ResXResourceWriter(Application.StartupPath + "\
\myCustomResources.resx");
for (int i = 0; i < this.Sounds.Count; i++)
{
resxWriter.AddResource(this.Sounds.Name, this.Sounds.Sound);
}
resxWriter.Generate();
resxWriter.Close();

The Sound class is:

public class Sound
{
public string Name { get; set; }
public byte[] Sound { get; set; }
}

Sounds is just a generic List of the Sound class;

Now, I was using this for WAV files, but will work with virtually any
type of file.

To load the resources:

Sound sound;
ResXResourceReader resxReader;
IDictionaryEnumerator id = null;

resxReader = new ResXResourceReader(Application.StartupPath + "\
\myCustomResources.resx");
id = resxReader.GetEnumerator();
foreach (DictionaryEntry d in resxReader)
{
sound = new Sound();
sound.Name = d.Key.ToString();
sound.Sound = (byte[])d.Value;
this.Sounds.Add(sound);
}
 
Hi Joe,

Well, this probably will work - you come across as someone with a lot of
programming experience. For the moment it is not necessary for me to add
resources at run time, but that might come in handy one day. I'll experiment
with your suggestion.

Thank you for you elaborate reply.

Cooz

Joe Cool said:
Dim assembly as Assembly
Dim stream as Stream
assembly = System.Reflection.Assembly.GetExecutingAssembly()
stream = assembly.GetManifestResourceStream
("<yourprojectname>.filename.gif")

You can take it from there can't you?

Another followup comment. Not sure about the filename reference, but
I'm pretty sure the "<yourprojectname>" is.

Also, project resources are not stored as individual files in a folder
somewhere. The are all converted to ASCII and stored in a resource
file, the default resource file is Resources.resx, and in actuality,
an XML file. Resources contained in the default resorce file can be
obtained from the current executing assembly as illustrated above.

An alternative, if you want to be able to add resources as run time,
is to use a custom resource file. To store resources in it you would
(sorry, this is in C#):

ResXResourceWriter resxWriter;

if (File.Exists(Application.StartupPath + "\\myCustomResources.resx"))
{
File.Delete(Application.StartupPath + "\\myCustomResources.resx");
}
resxWriter = new ResXResourceWriter(Application.StartupPath + "\
\myCustomResources.resx");
for (int i = 0; i < this.Sounds.Count; i++)
{
resxWriter.AddResource(this.Sounds.Name, this.Sounds.Sound);
}
resxWriter.Generate();
resxWriter.Close();

The Sound class is:

public class Sound
{
public string Name { get; set; }
public byte[] Sound { get; set; }
}

Sounds is just a generic List of the Sound class;

Now, I was using this for WAV files, but will work with virtually any
type of file.

To load the resources:

Sound sound;
ResXResourceReader resxReader;
IDictionaryEnumerator id = null;

resxReader = new ResXResourceReader(Application.StartupPath + "\
\myCustomResources.resx");
id = resxReader.GetEnumerator();
foreach (DictionaryEntry d in resxReader)
{
sound = new Sound();
sound.Name = d.Key.ToString();
sound.Sound = (byte[])d.Value;
this.Sounds.Add(sound);
}
 
Hi Patrice,

The "use another folder" idea appeals. Since the application I'm writing is
not too important - a tool for my 8-years-old daughter to help her with
topography - storing the maps in a folder in the StartupPath location works
just fine.

Thank you for your reply.

Cooz
 
Back
Top