Search code examples
c#savecontrolsloadpicturebox

Save programmatically created picturebox control and reload


Actually I am in trouble. My project is to create a desktop with shortcuts. The desktop is ready, it is a picturebox as background. And now the shortcuts.

Through a dialog, I will create a shortcut on the picturebox (desktop). Example: Here is my desktop: https://pasteboard.co/IHSdXvX.png

Here is my dialog: https://pasteboard.co/IHSefrz.png First textbox is the path to the file. Second textbox is the name of the shortcut.

And here is the result: https://pasteboard.co/IHSeM7Y.png In the upper left corner is a new green shortcut. It is a picturebox, which was created by this code:

 Icon ico = Icon.ExtractAssociatedIcon(textBox1.Text);

        var picture = new PictureBox
        {
            Name = textBox2.Text,
            Size = new Size(48, 46),
            Location = new Point(100, 100),
            Image = ico.ToBitmap(),
            SizeMode = PictureBoxSizeMode.Zoom,

        };


        Form1Singleton.FormVerweis.pictureBox1.Controls.Add(picture);

So, my question is:

How can I save the new created picturebox (the green icon upper left corner) and load it back to the same position, and the same click-Event to start the .exe behind it (exe path in textbox 1 in the dialog before) when the application starts. Information about the picturebox to save: Location, Icon, .exe Path.

Thanks for your help and I am really happy about code-examples. Joshua


Solution

  • You will need to create a class that stores all the information you need to create an icon:

    [Serializable]
    public class IconDefinition
    {
        public string Name { get; set; }
        public string Path { get; set; }
        public int X { get; set; }
        public int Y { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
    }
    

    Then you have some method that actually creates your icon based on these values:

    void createIcon(IconDefinition iconDefinition)
    {
        PictureBox pictureBox = new PictureBox()
        {
            Name = iconDefinition.Name,
            Size = new Size(iconDefinition.Width, iconDefinition.Height),
            Location = new Point(iconDefinition.X, iconDefinition.Y),
            Image = Icon.ExtractAssociatedIcon(iconDefinition.Path).ToBitmap(),
            SizeMode = PictureBoxSizeMode.Zoom
        };
        pictureBox.Click += myClickHandler;
        Form1Singleton.FormVerweis.pictureBox1.Controls.Add(pictureBox);
    }
    

    Next you have your methods to serialize and deserialize these. Since you probably want to store all your icons in the same file for persistence you would use a List for serialization.

    static void saveIcons(List<IconDefinition> icons)
    {
        using (Stream stream = new FileStream(@".\icons.bin", FileMode.Create, FileAccess.Read))
            new BinaryFormatter().Serialize(stream, icons);
    }
    
    static List<IconDefinition> loadIcons()
    {
        using (Stream stream = new FileStream(@".\icons.bin", FileMode.Open, FileAccess.Read))
            return (List<IconDefinition>)new BinaryFormatter().Deserialize(stream);
    }
    

    These will provide you functionality to write a List<IconDefinition> to a file, or restore it from a file.

    Now you will have to have a place somewhere to put all your icons:

    List<IconDefinition> allIcons = new List<IconDefinition>();
    

    When manually creating the icon (as you do already):

    IconDefinition iconDefinition = new IconDefinition()
    {
        Path = textBox1.Text,
        Name = textBox2.Text,
        X = 100,
        Y = 100,
        Width = 48,
        Height = 46
    };
    allIcons.Add(iconDefinition);
    createIcon(iconDefinition);
    

    When you want to save all your icons:

    saveIcons(allIcons);
    

    And when your application starts to restore them:

    // When application starts you can load the icons
    allIcons = loadIcons();
    // then restore them all
    foreach (IconDefinition definition in allIcons)
        createIcon(definition);
    

    Hope that helps.