将PictureBox添加到TabControl时,“参数无效”

时间:2019-02-06 10:05:21

标签: c# image visual-studio winforms bitmap

所以我的程序中出现此错误。下面的代码显示了我在加载时将TabControl和PictureBoxes动态添加到窗体的位置。 PictureBox的图像或者是我的程序拍摄的一组新的屏幕截图,或者如果用户的机票未完成,它将先前的屏幕截图加载到activeticket.Screenshots中,并使用这些屏幕截图。如果程序使用新的屏幕截图,则以下代码可以完美运行。但是,如果程序尝试加载以前的屏幕快照(以.png格式保存并以.bmp格式加载),则会抛出"Parameter not valid"异常,并且无法继续。

        //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();
        }

这是我加载图像的代码。请注意,ImageFilenames是包含图像路径的字符串列表。

       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);   
        }
    }

我尝试使用ImageConverter,已经尝试了15种不同的方式,包括pb.Image = Image.FromFile(file),但我没有运气!这是堆栈跟踪:

  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)

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

根据@Jimi的答案,我添加了这一行     Screenshots.Add((Bitmap)Screencaps.Clone()); 代替     Screenshots.Add(Screencaps);

然后我对代码的其他部分进行了一些编辑以适应此情况,并且成功了!谢谢!