By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

C# Casting Images from Resource Files

P: n/a
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("wi n1", ".", 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

--

Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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("fl ag", ".", 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?
Code Guy wrote:
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("wi n1", ".", 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


Nov 15 '05 #2

P: n/a
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.

--
"Antenna" <q1****************@hotmail.com> wrote in message
news:bv**********@news.worldonline.be...
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("fl ag", ".", 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?
Code Guy wrote:
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("wi n1", ".", 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

Nov 15 '05 #3

P: n/a
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.

Code Guy wrote:
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.


Nov 15 '05 #4

P: n/a
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?

--
"Antenna" <q1****************@hotmail.com> wrote in message
news:bv**********@news.worldonline.be...
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.

Code Guy wrote:
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.

Nov 15 '05 #5

P: n/a
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("fl ag", ".", 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()
on that string. See if that works.

Code Guy wrote:
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?


Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.