emmademontford
emmademontford

Reputation: 122

"Parameter not valid" when adding PictureBox to TabControl

So I'm getting this error in my program. The below code shows where I dynamically add TabControl and PictureBoxes to a form on load. The images for the PictureBoxes are either a new set of screenshots that my program takes, or if the user has an unfinished ticket, it will load the previous screenshots into activeticket.Screenshots and use those instead. If the program takes new screenshots, the below code works perfectly. However, if the program tries to load previous screenshots, which are saved in a .png format and loaded in a .bmp format, I get the "Parameter not valid" exception thrown and can't continue.

        //IF COUNT OF SCREENSHOTS IN ACTIVE TICKET IS ZERO
        //TAKE NEW SCREENSHOTS AND ASSIGN TO TABS 
        //ELSE ASSIGN EXISTING SCREENSHOTS TO TABS

        List<Bitmap> screenshots;

        if (activeticket.Screenshots.Count == 0)
        {
            screenshots = clsTools.TakeScreenshotList();
            foreach (Bitmap bmp in screenshots)
            {
                activeticket.Screenshots.Add(bmp);
            }
        }

        //REMOVE DEFAULT TAB
        tabControl1.TabPages.Remove(Tab1);

        foreach (Bitmap bmp in activeticket.Screenshots)
        {
            TabPage tp = new TabPage();
            PictureBox pb = new PictureBox()
            {
                Dock = DockStyle.Fill,
                SizeMode = PictureBoxSizeMode.StretchImage,
                Image = bmp
            };

            tp.Controls.Add(pb); //PARAMETER NOT VALID ON THIS LINE
            tabControl1.Controls.Add(tp);
            tp.Text = "Screen";
            CheckBox cb = new CheckBox()
            {
                Text = "Include in ticket",
                Anchor = AnchorStyles.Bottom | AnchorStyles.Right,
                AutoSize = true,
                Location = new Point(558, 345),
                Checked = true
            };
            tp.Controls.Add(cb);
            cb.BringToFront();
        }

Here is my code for loading the images. Please note that ImageFilenames is string list containing the paths for the images.

       internal void LoadScreenCaptures()
       {
          foreach (string file in ImageFilenames)
          {
             var Screencaps = new Bitmap(file);
             Screenshots.Add(Screencaps);
             Screencaps.Dispose(); //DISPOSE IS HERE SO I CAN DELETE FILES BELOW
       }

        foreach (string file in ImageFilenames)
        {
            File.Delete(file);   
        }
    }

I've tried using an ImageConverter, I've tried about 15 different ways including pb.Image = Image.FromFile(file) and I'm having no luck! Here's the stack trace:

  at System.Drawing.Image.get_FrameDimensionsList()
   at System.Drawing.ImageAnimator.CanAnimate(Image image)
   at System.Drawing.ImageAnimator.ImageInfo..ctor(Image image)
   at System.Drawing.ImageAnimator.Animate(Image image, EventHandler onFrameChangedHandler)
   at System.Windows.Forms.PictureBox.Animate(Boolean animate)
   at System.Windows.Forms.PictureBox.Animate()
   at System.Windows.Forms.PictureBox.OnParentChanged(EventArgs e)
   at System.Windows.Forms.Control.AssignParent(Control value)
   at System.Windows.Forms.Control.ControlCollection.Add(Control value)
   at System.Windows.Forms.TabPage.TabPageControlCollection.Add(Control value)
   at SETL.StartPage.StartPage_Load(Object sender, EventArgs e) in C:\Users\aaminahabdallah\Documents\Code\Ticketing\UI\Main UI\StartPage.cs:line 179
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Any help would be appreciated!

Upvotes: 1

Views: 319

Answers (1)

emmademontford
emmademontford

Reputation: 122

As per @Jimi answer, I added the line Screenshots.Add((Bitmap)Screencaps.Clone()); in place of Screenshots.Add(Screencaps);

I then did a bit of editing of the other parts of my code to fit around this, and it worked! Thank you!

Upvotes: 1

Related Questions