treeview

F

frazer

hi i have a set of bitmaps in an array.
and i want to add it to a imagelist so that i can show those images in my
treeview.


imageList2.Images.Add (Image.FromHbitmap (picture.Bitmap)); //cant use this
gives me an error

do i need to convert from bitmap to a memory stream?

thanx
 
B

Branimir Giurov

The following is an exact example which works:

Image i = (Image)Bitmap.FromFile(@"C:\WINDOWS\blue lace 16.bmp");
ImageList il = new ImageList();

il.Images.Add(i);

TreeView tv = new TreeView();

tv.ImageList = il;
tv.Nodes.Add("root");
tv.Nodes[0].ImageIndex = 0; //set the image to the first item

this.Controls.Add(tv);

Cheers,
Branimir
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

frazer said:
hi i have a set of bitmaps in an array.
and i want to add it to a imagelist so that i can show those images in my
treeview.

There is ImageList.ImageSize property that should correspond with
size of images. If it isn't then images will be resized (with
possible loss of quality).
imageList2.Images.Add (Image.FromHbitmap (picture.Bitmap)); //cant use this
gives me an error

I believe that your picture.Bitmap does not returns IntPtr.
ImageList.ImageCollection.Add method takes "Image" or "Icon" parameter.
do i need to convert from bitmap to a memory stream?

thanx

try this:

Bitmap[] bmpArray; // your array of bitmaps

// ... bmpArray fill

for(int i=0; i<bmpArray.Length; i++) {
imageList2.Images.Add(bmpArray);
}

If my sample will not do what you want or throws exception
then send feedback with possible StackTrace.

Regards

Marcin
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi,

I think your approach is a little bit overcomplicated. The Add method
accepts a System.Drawing.Image, and System.Drawing.Bitmap is inherited from
System.Drawing.Image. So it should be sufficient to use the following code
(given the Bitmap property returns an instance of System.Drawing.Bitmap):

imageList2.Images.Add (picture.Bitmap);
 

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