ImageList problem

C

CafeCat

Hi all,

I have a problem in ImageList. When I add 2 images to the ImageList, program
will allways get NullReferenceException error. I find system name the 2
images as "resource" and "resource1", and when program run to
this.imageList1.Images.Add(((System.Drawing.Image)(resources.GetObject("reso
urce1"))));

the error happen.

do you know what is the matter?
 
M

Mark Johnson

Resources are sometimes tricky, depending on where (which Class and which
Project the class is being used in) the resource has been added.
Try something like this :
System.Reflection.Assembly asm =
System.Reflection.Assembly.GetExecutingAssembly();
string[] sa_ResourceNames = asm.GetManifestResourceNames();
and look at the result in the sa_ResourceNames and find the resource you are
looking for.

I also use a Method in each class where resource's are used :

In this case the Class is called "Cards"
There is a resource called : "Desktop.mj10777.Array.48.gif"
- in your case: "resource" and "resource1"
This class is used in the project (which can of cource can change!) :
"CardGame"

sa_ResourceNames[0] has the result :
"CardGame.Cards.Desktop.mj10777.Array.48.gif"

On GetAssemblyName looks for ".Cards." and returns "CardGame.Cards." in the
Field s_AssemblyName and
is called on Construction (s_AssemblyName = OnGetAssemblyName(); ) for use
later :

bmp_Work = new
Bitmap(asm.GetManifestResourceStream(s_AssemblyName+"Desktop.mj10777.Array.4
8.gif"));

Since Compact cannot return the name of the class, ".????." must be
hardcoded for each class (which is a pity).

Hope this helps
Mark Johnson, Berlin Germany
(e-mail address removed)


#region OnGetAssemblyName
/// <summary>
/// <list type="table">
/// <item><description>Retrieve Assemblyname as used in Resource
file</description></item>
/// </list>
/// </summary>
/// <remarks>
/// <para></para>
/// </remarks>
/// <returns>Assembly name up to (including) ".Card." returned</returns>
/// <seealso
cref="Cards(System.Windows.Forms.Panel,String,Int32,Int32,Int32)"/>
/// <seealso cref="Cards.s_AssemblyName"/>
public String OnGetAssemblyName()
{
System.Reflection.Assembly asm =
System.Reflection.Assembly.GetExecutingAssembly();
string[] sa_ResourceNames = asm.GetManifestResourceNames();
String s_AssemblyName = "";
for (int i=0,x=0;i<sa_ResourceNames.Length;i++)
{ // "CardGame.Cards.Desktop.mj10777.Array.48.gif"
x = sa_ResourceNames.IndexOf(".Cards.");
if (x > 0)
{ // "CardGame.Cards."
s_AssemblyName = sa_ResourceNames.Substring(0,x+7);
break;
} // if (x > 0)
} // for (int i=0;i<sa_ResourceNames.Length;i++)
return s_Assembly;
} // public String OnGetAssemblyName()
#endregion
 

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