Reputation: 157
I have a Windows form which includes a TreeView. The treeview has images for its nodes which are embedded resources in the project. When running the form on my development machine (in and outside of Visual Studio), it runs fine and the treenodes each have their proper image. When running on the production box, the window runs into an error when it attempts to load the images that are embedded. I've added logic to not use the images if this happens so the tree still displays, but I would like to figure out what the problem is and how to fix it.
The error message that is returned is a generic "parameter is not valid" when attempting to create a new Bitmap. The stack trace gives the line "System.Drawing.Bitmap..ctor(Type type, String resource)" as the point of failure. See the code below.
ImageList imageListNodes = new ImageList();
Bitmap mIcon;
mIcon = new Bitmap(this.GetType(), "m2.ico"); // This is the line that fails.
imageListNodes.Images.Add("m", (Image)mIcon);
tvRecords.ImageList = imageListNodes; // This is the treeview used in the form.
The file m2.ico has been added directly to the project. Its properties include Build Action = Embedded Resource, and Copy to Output Directory = Do not copy.
My development computer is running Windows 7, and the production is Windows Server 2003 SP2, with a Citrix environment. The project is in .NET 3.5, which they both have. This form would be available on different client machines, so it should be able to work in Server 2003 and up.
Thanks for any suggestions!
EDIT: A few minutes in and already it's suggested that this setup won't work at all. I noted above that this works on my computer both in Visual Studio debug mode, and when called on its own. The icon files themselves are off on a network share, so there is no way that the DLL could be pulling them in; the files must be embedded correctly, and the Bitmap / Icon conversion must be working correctly, or it wouldn't work at all on my machine.
I appreciate the suggestions, and I am willing to try other methods of accomplishing the same goal, but please note that my initial question at least is why this works on my computer but not another one. Thanks!
Upvotes: 1
Views: 421
Reputation: 3829
I don't think you can load an .ico into a image just like that. Why don't you try to load the icon first in a Icon object and then call the Icon.ToBitmap to get the image.
Upvotes: 1
Reputation: 31349
Try using the Icon class, not Bitmap. I do not believe they are compatible.
Edit: Just remembered something else... Icon is a multiple image format. They ARE bitmap, but it can contain multiple images at different resolutions. But, like @PedroC88 said, call ToBitmap and it will convert. Though I think which size it uses is system determined and you cannot choose.
Upvotes: 0