热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:将PictureBox添加到TabControl时“参数无效”

篇首语:本文由编程笔记#小编为大家整理,主要介绍了将PictureBox添加到TabControl时“参数无效”相关的知识,希望对你有一定的参考价值。

篇首语:本文由编程笔记#小编为大家整理,主要介绍了将PictureBox添加到TabControl时“参数无效”相关的知识,希望对你有一定的参考价值。



所以我的程序中出现了这个错误。下面的代码显示了在加载时动态地将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 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:UsersaaminahabdallahDocumentsCodeTicketingUIMain UIStartPage.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)

任何帮助,将不胜感激!


答案

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

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



推荐阅读
author-avatar
杰仔
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有