Tuesday, October 15, 2013

Extract all images from a toolbar image list

Ever have to change a toolbar image but the loaded image files no longer exist?
Here is a solution to run on the aplication and save the image files.

Receives an image list as parameter:

private string SaveAllToolbarImages(ImageList imgList)
        {
            if (toolBar == null)
            {
                return "No images";
            }

            try
            {
                string imgsPath = Path.GetDirectoryName(Application.ExecutablePath) + "\\ImagesFromToolbar";
                Directory.CreateDirectory(imgsPath);

                for (int x = 0; x < imgList.Images.Count; ++x)
                {
                    Image temp = imgList.Images[x];
                    temp.Save(imgsPath + "\\" + imgList.Images.Keys[x]);
                }
            }
            catch (System.Exception ex)
            {
                 return "Exception: " + ex.Message;
            }

            return "";
        }

Usage:

SaveAllToolbarImages(this.imageListToolbar);