TreeView and Add an Array of Images

J

Jeffrey Walton

Hi All,

I have an array of 16x16 bitmaps (60 total). I've tried adding the
array to the ImageList with the Add method, but the TreeView paints as
if no BMP is present.

The problem is the method does not 'break out' the bitmaps. If I
specify Index 0, I get an unexpected result (horizontal compressing of
the file).

How do I direct the TreeView/ImageList team to treat this as an array,
rather than a single image? I found an old post, but the suggestion
was incorrect (http://groups.google.com/group/
microsoft.public.dotnet.languages.csharp/browse_thread/thread/
151b30d4814eda55/).

What I don't want to do is add 60 individual files to the project. It
is bad enough that the program cannot find one file in the Resource
directory. I don't want to pollute the space any further with 60
additional files.

Jeff

ImageList images = new ImageList();
TreeView tree = new TreeView();

// Resource file previously copied into .\bin\Debug
images.Images.Add(new Bitmap(<file>)); // 60 images

// ImageSize does not produce expected result... The
// control does not deduce there are 60 elements
images.ImageSize = new System.Drawing.Size(16,16);

// Throws an error - width < 256
images.ImageSize = new System.Drawing.Size(960,16);

tree.Images = images;
 
J

Jeffrey Walton

In case anyone is interested, here's a solution which I hacked
together. This paradigm reeks of Java. I disliked it in college years
ago, and I still have not acquired a taste for it...

The image files were embedded in the executable (select the Image ->
Properties, Build Action = "Embedded Resource"). assemblyname and
assembly are static so they only have to be calculated once, without
the extra argument passing. If you're into software engineering, hack
it further the Java way.

////////////////////////////////////////////////
TreeView Tree = new TreeView();
TreeViewImageHelper helper = new TreeViewImageHelper();
....
Tree.ImageList = helper.CreateImageList();

////////////////////////////////////////////////
public class TreeViewImageHelper
{
private static string assemblyname = null;
private static Assembly assembly = null;

public TreeViewImageHelper() { }

public ImageList CreateImageList()
{
ImageList list = new ImageList();

string[] filenames = {
"Certificate.bmp",
"CertificateSelected.bmp",
"Message.bmp",
"MessageSelected.bmp",
null
};

for (int i = 0; filenames != null; i++)
{
Stream stream = GetEmbeddedFile(filenames);
if (null != stream)
{
list.Images.Add(Bitmap.FromStream(stream));
}
}
return list;
}

public Stream GetEmbeddedFile( string filename )
{
if( null == assemblyname )
{ assemblyname = Assembly.GetExecutingAssembly().GetName().Name; }

if (null == assembly)
{ assembly = System.Reflection.Assembly.Load(assemblyname); }

Stream bitmapstream = null;
try
{
bitmapstream = assembly.GetManifestResourceStream(
assemblyname + ".Resources" + "." + filename
);
}
catch
{ return null; }

return bitmapstream;
}

public int GetImageIndex(int number)
{
// return the appropriate index...
}
}
 

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