469,963 Members | 2,138 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

TreeView and Add an Array of Images

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/

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.


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;
Nov 17 '07 #1
1 2631
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 = {

for (int i = 0; filenames[i] != null; i++)
Stream stream = GetEmbeddedFile(filenames[i]);
if (null != 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;
bitmapstream = assembly.GetManifestResourceStream(
assemblyname + ".Resources" + "." + filename
{ return null; }

return bitmapstream;

public int GetImageIndex(int number)
// return the appropriate index...
Nov 18 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Anthony Boudouvas | last post: by
3 posts views Thread by frazer | last post: by
6 posts views Thread by cyriel1920 | last post: by
reply views Thread by bsturg21 | last post: by
reply views Thread by =?Utf-8?B?V2FubmFiZQ==?= | last post: by
reply views Thread by =?Utf-8?B?TWlrZSBDb2xsaW5z?= | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.