If I need to repeatedly create a picturebox of the same image, I understand that one way of doing so is creating an array of pictureboxes and then making one more visible every few seconds, but the image of the picturebox must be pathed to an image on your computer. If I need to send the full game to someone, the address of the image to the picturebox will not be the same and there'll be a problem... Is there any way to "package" the image in the program?

Recommended Answers

All 6 Replies

You can embed images into your program yes using Resources.

Is this a Winforms App?

Hey there, thanks for replying... Yeah, it is. So from this point, is there a way of "creating" pictureboxes on the fly while the form is running? Or must I have an array of them ready before starting the application?
Cheers

No you can create them on the fly. What is it you want to do specifically?

Creating controls at runtime is certainly possible. For example:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace FormScratch
{
    public class MainForm : Form
    {
        private Button buttonAddImage;
        private FlowLayoutPanel flowLayoutPanelThumbs;

        private void InitializeComponent()
        {
            buttonAddImage = new Button();
            flowLayoutPanelThumbs = new FlowLayoutPanel();
            SuspendLayout();

            buttonAddImage.Location = new Point(13, 13);
            buttonAddImage.Name = "buttonAddImage";
            buttonAddImage.Size = new Size(75, 23);
            buttonAddImage.TabIndex = 0;
            buttonAddImage.Text = "Add Image";
            buttonAddImage.UseVisualStyleBackColor = true;
            buttonAddImage.Click += new EventHandler(buttonAddImage_Click);

            flowLayoutPanelThumbs.Location = new Point(13, 43);
            flowLayoutPanelThumbs.Name = "flowLayoutPanelThumbs";
            flowLayoutPanelThumbs.Size = new Size(680, 444);
            flowLayoutPanelThumbs.TabIndex = 1;

            AutoScaleDimensions = new SizeF(6F, 13F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(705, 499);
            Controls.Add(flowLayoutPanelThumbs);
            Controls.Add(buttonAddImage);
            Name = "Form1";
            Text = "Form1";
            ResumeLayout(false);

        }

        public MainForm()
        {
            InitializeComponent();
        }

        private void buttonAddImage_Click(object sender, EventArgs e)
        {
            using (var dlg = new OpenFileDialog())
            {
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    flowLayoutPanelThumbs.Controls.Add(new PictureBox()
                    {
                        Image = new Bitmap(dlg.FileName),
                        Size = new Size(100, 100),
                        SizeMode = PictureBoxSizeMode.Zoom
                    });
                }
            }
        }
    }
}

Is there any way to "package" the image in the program?

Yes. You can add the image to your project as either content or embedded resouce with Add | Existing Item and then changing the properties of the item. For content, be sure to include the project content in your installer and the image will be copied to the installation folder. For embedded resources, it's only marginally trickier to reference the image.

@DaveAmour I'm actually trying to create a simple tower defense game with enemies running in the shortest path by programming a movement for these cloned pictureboxes, and whenever one is being created, it is programmed to move along the path.
The first step is to get these pictureboxes cloned and the image kept in the application, which I am still trying to do... :p

@deceptikon Thanks for helping out, I'll try to understand the concept and use it to put the image into the resources later on. :)

I've found this article on image embedding here : https://msdn.microsoft.com/en-us/library/aa984367
They mentioned "Import the System.Reflection and System.IO namespaces.", does anyone know how to check if I have em? I haven't done any importing but once I type "System.R" System.Reflection comes and the same for IO.
When I try to refer to "Tower_Defense_project.Tank.png" it doesn't seem to be there... Does anyone know what I'm doing wrong?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.