By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,281 Members | 2,201 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,281 IT Pros & Developers. It's quick & easy.

TreeView and Add an Array of Images

P: n/a
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;
Nov 17 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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[i] != null; i++)
{
Stream stream = GetEmbeddedFile(filenames[i]);
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...
}
}
Nov 18 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.