Resource could not be found in class

  • Thread starter Thread starter Guest
  • Start date Start date
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.
 
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
 
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.
 
Back
Top