C# Casting Images from Resource Files

C

Code Guy

Hello,

I am trying to execute the following code:

private void button1_Click(object sender, System.EventArgs e)
{
PictureBox c = new PictureBox();
c.BorderStyle = BorderStyle.FixedSingle;
this.Controls.Add(c);

ResourceManager rm =
ResourceManager.CreateFileBasedResourceManager("win1", ".", null);
c.Image = (Image)rm.GetObject("TWOSPADES");
}

I am receiving the error: specified cast is not valid.

I know the GetObject call returns an object because I can view it's
properties. I don't understand why I cannot cast the object into an Image
type. Any help would be appreciated.

Thanks

--
 
A

Antenna

The object returned is not an Image/Bitmap object. Maybe you can do:

Object o = rm.GetObject("TWOSPADES");
Console.WriteLine(o.ToString());

to discover the type of the returned object?

I just tested it and it works for me. I did the following:

1. Create a bitmap "flag.bmp"
2. Add the bitmap to a resx file using the following code:
Image img = Image.FromFile("flag.bmp");
ResXResourceWriter rsxw = new ResXResourceWriter("flag.resx");
rsxw.AddResource("flag",img);
rsxw.Close();
3. Convert flag.resx to flag.resources using resgen.exe
4. Test everything using:
ResourceManager rm =
ResourceManager.CreateFileBasedResourceManager("flag", ".", null);
Image i = (Image)rm.GetObject("flag");
Console.WriteLine(i.ToString());

The output is: System.Drawing.Bitmap

So, I think your code is ok, but maybe names or file formats are wrong.
BTW, if the name of your resource TWOSPADES or twospades?
 
C

Code Guy

The returned object is System.Drawing.Bitmap in my original code.

Maybe you're right, it could be in the way I add the image to the resouce
file. Here is that code:

IResourceWriter writer = new ResourceWriter(OutputFileName);
object res = null;
res = Image.FromFile(line);
name = line.Substring(0, line.Length - (ext.Length + 1)).ToUpper();
// trim the .bmp
writer.AddResource(name, res);

Is there something wrong with the above code? I don't use resgen.exe to
create the final .resource file. OutputFileName contains a .resource file
name, not .resx. Is that the problem? Is the original instantiation of res
as an object the problem?


Thanks for looking at this.
 
A

Antenna

I see. But do you actually Close() or Generate() the resource?
And, if so, is "name" what you expect? TWOSPADES? You've checked that?

I would first try to call writer.Close() if you haven't done that.
 
C

Code Guy

I am using
writer.Close();

Also, I was able to verify the name of the file. I can see the images in
the .resource using this code:

private void ReadResourceFile(string fileName)
{
ResourceReader rrdr = null ;
try
{
rrdr = new ResourceReader(fileName);
currentResHolder = new ResHolder(rrdr);
propertyGridResources.SelectedObject = currentResHolder;
}
finally
{
if (null != rrdr) rrdr.Close();
}
}

I need to load each image individually as well. Does the code I have
described work in your environment?
 
A

Antenna

No matter what I do, it works. Here's the code I use:

static void Main(String[] args)
{
/* Image img = Image.FromFile("flag.bmp");
IResourceWriter writer = new ResourceWriter("flag.resources");

// Adds resources to the resource writer.
writer.AddResource("flag", img);

// Writes the resources to the file or stream, and closes it.
writer.Close();
*/
ResourceManager rm =
ResourceManager.CreateFileBasedResourceManager("flag", ".", null);
Image i = (Image)rm.GetObject("flag");
Console.WriteLine(i.ToString());
/*
IResourceReader reader = new ResourceReader("flag.resources");
IDictionaryEnumerator en = reader.GetEnumerator();

// Goes through the enumerator, printing out the key and value pairs.
while (en.MoveNext()) {
Console.WriteLine();
Console.WriteLine("Name: {0}", en.Key);
Console.WriteLine("Value: {0}", en.Value);
}
reader.Close();
*/
}

Even with 2 bitmaps, it works. Maybe there's something wrong with the
call to CreateFileBaseResourceManager()? And what culture are your
resources in? Maybe you have to specify somewhere the culture? Try
enumerating your .resources file with the above code. If the output is
ok, try adding a simple string to your .resources. Then try GetObject()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top