Resource could not be found in class

G

Guest

Whenever I try to load a bitmap using the standard code:

Bitmap myBitmap = new Bitmap(this.GetType(), "bitmapResourceName")

I always end up with an error of the form "Resource "bitmapResourceName"
could not be found in class MyClass".
This seems to be something to do with having renamed either the namespace or
the class. Is this a known bug? The get-around seems to be to use the
following code:

string strNameSpace =
System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString();
System.IO.Stream stream
=System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(strNameSpace + "." + "bitmapResourceName");
Bitmap myBitmap = new Bitmap(stream);

but this seems awfully hamfisted compared to the more elegant original.
 
G

Guest

Dave,
It may seem "hamfisted". What you can do is turn this into a static method
on a utility class that can be reused by your code in any application. In
that case, all you would need to do is write:

Bitmap myBitmap=MyUtility.GetBitmapResource(resourceName);

Peter
 
G

Guest

Yes, indeed. But the question still remains, what is the problem with the
first Bitmap(GetType(), "name") constructor? Is it a bug? Will it be fixed?
Or am I doing something wrong? I have seen code containing calls to this
constructor that apparently works fine. In fact I recently used just such a
class which was fine in its own project, but when i copied the class into my
project, changed the name of the namespace and maybe a few other things, and
suddenly I'm geting this error again.
 

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