Imagelist value set to null in fullscreen picturebox form. Why? RRS feed

  • Question

  • I got a form with a listview for image icons to appear on. Have a imagelist that will store values and bind them to listview. And a picture box for the selected image to appear on and a full screen button. Trying to open image from listview to display in a picturebox and then click full screen button to display image in full screen. ImageLocation in frmFullScreen is null. Why? In frmMain It is being displayed using the file path and displays fine.

    I know I can’t use the Image property on one form and ImageLocation on another which is what I’m doing on this line in loadImageList() in a for loop.

    imageList1.Images.Add(single_file, Image.FromFile(single_file));

    Then I’m trying to read it at fullscreen in a picturebox in frmFullScreen_Load

    pictureBox1.ImageLocation = _objImageData.FileName;

    What am I doing wrong, please?

    Here is the code for my startup form. frmMain

    namespace PictureViewer
        public partial class frmMain : Form
            #region Object declarations
            string msg = "No image in picture box";
            frmFullScreen _objFrmFullScreen = new frmFullScreen();
            ImageData _objImgData = new ImageData();
            #endregion Object declarations
            #region Initializers
            public frmMain()
            //Displays larger instance of selected image in picture box.
            private void lstImages_SelectedIndexChanged(object sender, EventArgs e)
                //FOR i is less than the first image.
                for (int i = 0; i < lstImages.SelectedItems.Count; i++)
                    //GET filename from listview and store in index.
                    _objImgData.FileName = lstImages.SelectedItems[i].Text;
                    //Create larger instance of image.
                    pictureBox1.Image = Image.FromFile(_objImgData.FileName);
                    //Fill panel to the width and height of picture box.
                    panel1.AutoScrollMinSize = new Size(pictureBox1.Image.Width, pictureBox1.Image.Height);
                    _objImgData.ImageIndex = lstImages.SelectedIndices[0];
            #endregion Initializers
            #region Methods
            //TO DO: Append items to the end of list without throwing exceptions.
            public void loadImageList()
                _objImgData.Counter= 0;
                oFD1.InitialDirectory = "C:\\";
                oFD1.Title = "Open an Image File";
                oFD1.Filter = "JPEGS|*.jpg|GIFS|*.gif|PNGS|*.png|BMPS|*.bmp";
                //Open Dialog Box.
                var oldResults = oFD1.ShowDialog();
                if (oldResults == DialogResult.Cancel)
                //GET amount of filenames.
                int num_of_files = oFD1.FileNames.Length;
                //Temp array i created to solve the error.
                //Store filenames in string array.
                List<string> arryFilePaths = new List<string>(num_of_files);
                foreach (string single_file in oFD1.FileNames)
                    imageList1.Images.Add(single_file, Image.FromFile(single_file));                
                //BIND image list to listview.
                lstImages.LargeImageList = imageList1;
                for (int i = 0; i < _objImgData.Counter; i++)
                    //DISPLAY filename and image from image index param. 
                    lstImages.Items.Add(arryFilePaths[i], i);
            #endregion Methods       

    Code for my ImageData class.

    namespace Data
        public class ImageData
            public string FileName { get; set; }
            public int Counter { set; get; } = 0;
            public int ImageIndex { get; set; }
    Code for frmFullScreen. Taskbar class works so I commented it.
    namespace FullScreen
        public partial class frmFullScreen: Form
            Data.ImageData _objImageData = new Data.ImageData();
            public frmFullScreen()
            private void frmFullScreen_Load(object sender, EventArgs e)
                pictureBox1.ImageLocation = _objImageData.FileName;
                this.TopMost = true;
                this.WindowState = FormWindowState.Maximized;
                //and then to exit:
                this.FormBorderStyle = FormBorderStyle.Sizable;
                this.BackColor = System.Drawing.Color.Black;
                this.FormBorderStyle = FormBorderStyle.None;
            private void frmFullScreen_keyUp(object sender, KeyEventArgs e)
                if (e.KeyData == Keys.Escape)
                    this.DialogResult = DialogResult.Cancel;

    Kind regards,

    Jordan Nash

    Saturday, July 28, 2018 4:50 AM


  • You have the _objImageData member in frmFullScreen which is different from _objImgData.

    One of the solutions is to make it public and execute _objFrmFullScreen._objImageData = _objImgData before showing the _objFrmFullScreen form.

    • Marked as answer by SlenderMan564 Sunday, July 29, 2018 6:58 AM
    Saturday, July 28, 2018 2:01 PM

All replies