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

问题描述 投票:1回答:1

所以我的程序中出现了这个错误。下面的代码显示了在加载时动态地将TabControl和PictureBoxes添加到表单的位置。 PictureBoxes的图像是我的程序所采用的一组新屏幕截图,或者如果用户有未完成的票证,它会将之前的屏幕截图加载到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)

任何帮助,将不胜感激!

c# image visual-studio winforms bitmap
1个回答
1
投票

根据@Jimi的回答,我添加了一行Screenshots.Add((Bitmap)Screencaps.Clone());取代Screens.Add(Screencaps);

然后,我对我的代码的其他部分做了一些编辑,以适应这个,它工作了!谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.