我创建了一个FaceDetectionEvent,它是一个用户控件,并尝试以窗体形式添加它(仍在同一个项目中)。但是,不断出现这样的错误: 将用户控件添加到Windows窗体c时出错#
这是FaceDetectionEvent代码:
public partial class FaceDetectionEvent : UserControl
{
private System.Timers.Timer tListener;
private MySQL_DataAccess da = new MySQL_DataAccess();
private int iCurrentStatusIndex_ = 0;
private List<DataRow> lstFaceDetectionEvent = new List<DataRow>(20);
private ImageList cropImageList = new ImageList();
public FaceDetectionEvent()
{
InitializeComponent();
CreateColumns();
GetLastTwentyEvent();
tListener = new System.Timers.Timer(1000);
tListener.Elapsed += new System.Timers.ElapsedEventHandler(tListener_Elapsed);
tListener.Start();
}
public void GetLastTwentyEvent()
{
string strSQL = string.Format("SELECT * FROM av_status_log AS A LEFT JOIN avmediaserver AS B ON A.device_id=B.DeviceId "
+ "LEFT JOIN privilege_device AS C ON A.device_id = C.device_id "
+ "LEFT JOIN privilege_device_group AS D ON C.device_group_id = D.device_group_id "
+ "WHERE event_type_id = 8 ORDER BY A.db_time DESC LIMIT 20");
DataTable dt = da.GetInfoData(strSQL).Tables[0];
if (dt.Rows.Count > 0)
iCurrentStatusIndex_ = Convert.ToInt32(dt.Rows[0]["rowid"]);
foreach (DataRow dr in dt.Rows)
{
lstFaceDetectionEvent.Add(dr);
string strCroppedImage = GetCropImageBase64String(dr["memo"].ToString());
cropImageList.Images.Add(Base64ToImage(strCroppedImage));
}
ShowFDEvent();
}
void tListener_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
string strSQL = string.Format("SELECT * FROM av_status_log AS A LEFT JOIN avmediaserver AS B ON A.device_id=B.DeviceId "
+ "LEFT JOIN privilege_device AS C ON A.device_id = C.device_id "
+ "LEFT JOIN privilege_device_group AS D ON C.device_group_id = D.device_group_id "
+ "WHERE A.rowid > {0} AND event_type_id = 8 ORDER BY A.db_time DESC", iCurrentStatusIndex_.ToString());
DataTable dt = da.GetInfoData(strSQL).Tables[0];
if (dt.Rows.Count > 0)
iCurrentStatusIndex_ = Convert.ToInt32(dt.Rows[0]["rowid"]);
if (lstFaceDetectionEvent.Count >= 20)
{
lstFaceDetectionEvent.RemoveRange(0, dt.Rows.Count);
for (int i = 0; i < dt.Rows.Count; i++)
{
cropImageList.Images.RemoveAt(i);
}
}
foreach (DataRow dr in dt.Rows)
{
lstFaceDetectionEvent.Add(dr);
string strCroppedImage = GetCropImageBase64String(dr["memo"].ToString());
cropImageList.Images.Add(Base64ToImage(strCroppedImage));
}
ShowFDEvent();
this.Refresh();
}
public string GetCropImageBase64String(string pStrMemo)
{
XElement doc = XElement.Parse(pStrMemo);
string strCropImage = doc.Element("cropImage").Value;
return strCropImage;
}
public Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
}
private void CreateColumns()
{
ColumnHeader cropImageHeader = new ColumnHeader();
cropImageHeader.Text = "Crop Image";
cropImageHeader.Width = 150;
FDEventlistView.Columns.Add(cropImageHeader);
ColumnHeader timestampHeader = new ColumnHeader("Event Time");
timestampHeader.Text = "Event Time";
timestampHeader.Width = 150;
FDEventlistView.Columns.Add(timestampHeader);
ColumnHeader deviceNameHeader = new ColumnHeader("Device Name");
deviceNameHeader.Text = "Size";
deviceNameHeader.Width = 80;
FDEventlistView.Columns.Add(deviceNameHeader);
}
private void ShowFDEvent()
{
FDEventlistView.Items.Clear();
FDEventlistView.BeginUpdate();
int i = 0;
foreach (DataRow dr in lstFaceDetectionEvent)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = i;
ListViewItem.ListViewSubItem subitem = new ListViewItem.ListViewSubItem();
subitem.Text = dr["status_time"].ToString();
item.SubItems.Add(subitem);
subitem = new ListViewItem.ListViewSubItem();
subitem.Text = dr["device_name"].ToString();
item.SubItems.Add(subitem);
FDEventlistView.Items.Add(item);
i++;
}
FDEventlistView.EndUpdate();
}
}
你有任何想法,为什么?
在哪一行你会得到这个异常?你调试了你的代码吗? – 2014-10-03 08:21:24
我认为这可能是由于两件事情发生。1.如果您的代码使用多个配置文件,并且在运行时无法合并这些配置文件。 2.或者代码在运行时无法找到任何特定的dll。因此,我建议将当前程序需要的所有dll文件复制到bin文件夹中,然后尝试运行并查看。 – 2014-10-03 08:35:43
如果我通过编程方式添加usercontrol,它可以工作。我已经将所有的dll文件复制到bin文件夹中 – currarpickt 2014-10-03 08:37:55