469,331 Members | 4,121 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Dynamically loading an Icon into an ImageList from a Stream

I'm having a problem loading an Icon into an ImageList.

When I load the icon directly it works fine, like this:
Icon sourceIcon = << Get an icon from somewhere... >>;
treeViewImageList.Images.Add("abc", sourceIcon);
But when I first save the original icon to a stream and then try loading the
ImageList from the stream, the icon image is messed up. Here's that code:
Icon sourceIcon = << Get an icon from somewhere... >>;
MemoryStream imageStream = new MemoryStream();
sourceIcon.Save(imageStream);
imageStream.Position = 0;
Icon icon = new Icon(imageStream);
treeViewImageList.Images.Add("abc", icon);
The ultimate goal is to use the stream to save the icon byte array to a
database and then load it again later but first I need to be able to get the
icon to reload correctly from the stream.

Any ideas what I'm doing wrong here?

Thanks.

- Adam
Aug 28 '07 #1
1 4536
The following works absolutely fine for me. Are you by any chance
doing something funny with the ImageList.ImageSize? Scaling will make
them look terrible...

Marc

static void Main()
{
Application.EnableVisualStyles();
// tray1 is a random icon in a resx
Icon ico1 = Resource1.tray1, ico2;
using(MemoryStream ms = new MemoryStream()) {
ico1.Save(ms);
ms.Position = 0;
ico2 = new Icon(ms);
}
// show the 2 icons on a treeview
using(Form f = new Form())
using(ImageList il = new ImageList())
using(TreeView tv = new TreeView()) {
il.Images.Add("ico1", ico1);
il.Images.Add("ico2", ico2);
tv.ImageList = il;
tv.Dock = DockStyle.Fill;
tv.Nodes.Add("node1", "Original", "ico1");
tv.Nodes.Add("node2", "Saved", "ico2");
tv.ExpandAll();
f.Controls.Add(tv);
f.ShowDialog();
}
}

Aug 29 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Randy Webb | last post: by
1 post views Thread by Josť Joye | last post: by
5 posts views Thread by footballhead | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.