winform picturebox图像显示空的c#
我有一个包含图片盒的表单。当窗体加载时,默认图像加载良好。然后,当表单中的某些内容发生更改时,我会更新图像,从而更改正在显示的图像。这个图像的生成也很好,我可以在磁盘上看到图像并用画图等打开它。通常我所做的是在图像位置打开一个文件流,然后将图像设置到这个位置winform picturebox图像显示空的c#,c#,winforms,image,picturebox,C#,Winforms,Image,Picturebox,我有一个包含图片盒的表单。当窗体加载时,默认图像加载良好。然后,当表单中的某些内容发生更改时,我会更新图像,从而更改正在显示的图像。这个图像的生成也很好,我可以在磁盘上看到图像并用画图等打开它。通常我所做的是在图像位置打开一个文件流,然后将图像设置到这个位置 if (this.picPreview.Image != null) { this.picPreview.Image.Dispose(); this.picPreview.Image = null; } FileStr
if (this.picPreview.Image != null)
{
this.picPreview.Image.Dispose();
this.picPreview.Image = null;
}
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
this.picPreview.Image = System.Drawing.Image.FromStream(fs);
但无论我做什么,表格上的图像都是空白的。我尝试过刷新表单,刷新picturebox控件,将其visible属性设置为visible,但没有任何帮助
我创建了一个单独的表单,其中只包含一个picturebox,并将图像位置传递给表单,然后重复打开流的过程,然后将图像设置到该位置,这样效果会很好
没有异常被抛出AIK…调试器设置为在所有异常上中断
什么可能导致这种行为
任何建议都将不胜感激。我有另一个应用程序,它在backgroundworker线程中生成图像,而且运行良好
也许提供更多关于我正在尝试做的事情的背景信息会帮助我弄清真相。对于我的datagridview中的每一行,都有一到三列与其关联的图像。我已经提前生成了所有这些图像。使用SelectionChanged事件在网格中滚动,我会得到图片框中第一个图像列的预览图像。它工作得很好。我还有一些单元格,当单击时,会显示一个窗体窗口,其中包含组成主窗体上图像的图像预览。这也非常有效。我可以更改行并单击网格中的单元格,一切正常。基本上,我正在根据用户在绑定到datagrid的其他控件上选择的内容构建一个新图像
当我试图更改主窗体上图片框中的图像时,问题就出现了。我可以更新数据源,并看到网格值更新,但现在使用第三方软件重新生成的图像(我可以验证它是否在磁盘上,并且在更新发生后可以查看)消失了。一旦发生这种情况,我就不再在表单的图片框中获取图像,直到我关闭表单并重新打开,然后所有更新的数据都在那里,一切都恢复正常。更改选择以设置图像时调用的代码与用于更新新图像的代码完全相同。它是完全同步的。除了用一种全新的形式从头开始之外,我已经没有其他想法了
再次感谢所有建议
我将从顶部开始。总体流程如下:
打开包含绑定到SQL视图的datagrid的表单。dgv是只读的,一次只能选择一行。该视图将自动填充,以及绑定到轴网每列的控件。其中包括一些的文本框、其他的组合框和复选框。每行都有一组与其关联的图像。当表单加载时,我可以向下滚动视图,并且对于每一行,表单上的图片框中都会出现一个新图像。所有这些图像都已预生成。选择一行时,该行最多可能有三个图像,在这种情况下,将启用导航按钮以允许用户预览每个图像
我选择一行,更改窗体上的控件以更改选定行中的一个或多个单元格值。然后点击save按钮,更新数据库和网格中相应的数据。然后,我尝试更新此行的图像。此时,图片框消失了,我丢失了表单上的所有预览;在我关闭并重新打开表单之前,不会出现任何图像,在我进行保存之前,一切都会恢复正常
在试图解决这个问题时,我发现更新绑定的dgv会导致selectchanged事件多次引发。有代码可以处理绑定不完整或视图中未选择任何内容的情况。btnSave_Click处理程序中还有代码用于挂起selectionchanged事件处理程序,直到更新完成并重新生成映像。尽管如此,即使在视图中选择了我更新的行,但实际选择的行(箭头所在的位置以及所有控件显示的内容)第一行始终是更新后的“当前”行。我还不知道如何解决这个问题。下面是更改的选择和按钮保存事件处理程序的代码
以下是表单的屏幕截图:
以及selectionchanged和btn_save事件处理程序的代码:
/// <summary>
/// update the preview on a row selection change
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (!BindingComplete) return;
DataGridView dgv = (DataGridView)sender;
if (!dgv.Focused || dgv.CurrentRow == null) return;
// set the pic preview to the current row image(s)
// we need the record for the current index
DataRowView currentDataRowView = (DataRowView)dgv.CurrentRow.DataBoundItem;
if (currentDataRowView == null) return;
DataRow currentRow = currentDataRowView.Row;
LastSelectedIndex = dgv.SelectedRows[0].Index;
Debug.WriteLine("Current row in SelectionChanged: " + currentRow.ItemArray[0].ToString());
bool showBox = false, showProd = false, showWire = false;
string box, prod, wire;
string pdcProductName = currentRow.ItemArray[0].ToString();
showWire = !string.IsNullOrEmpty(wire = currentRow.ItemArray[7].ToString());
showBox = !string.IsNullOrEmpty(box = currentRow.ItemArray[8].ToString());
showProd = !string.IsNullOrEmpty(prod = currentRow.ItemArray[9].ToString());
// check for wirepath, box, and product. Enable the nav buttons if there is more than
// one label for this product. We need to check for LabelFileName being the same for both
// box and product, in which case there is one file for both which defaults to box
if ((showBox && showProd && prod == box) || showBox)
{
string targetFile = PreviewImagePath + pdcProductName + "_eBox.png";
if (picPreview.Image != null)
{
//picPreview.Image.Dispose();
//picPreview.Image = null;
}
// if the preview image doesn't exist yet use a default image
if (!File.Exists(targetFile))
{
// make the loading gif invisible
this.picLoading.Visible = true;
//picPreview.Image = AdminTILE.Properties.Resources.StandardPaper;
}
else
{
this.picLoading.Visible = false;
Debug.WriteLine("Opening file " + targetFile);
FileStream fs = new FileStream(targetFile, FileMode.Open, FileAccess.Read);
picPreview.Image = System.Drawing.Image.FromStream(fs);
Image imgCopy = (Image)picPreview.Image.Clone();
this.picPreview.Visible = true;
fs.Close();
// preview in another frame
if (frm.IsDisposed)
{
frm = new PreviewImage();
}
frm.PreviewLabel(imgCopy);
frm.Show();
//picPreview.ImageLocation = targetFile;
}
}
else if (showProd)
{
string targetFile = PreviewImagePath + pdcProductName + "_eBox.png";
if (picPreview.Image != null)
{
picPreview.Image.Dispose();
//picPreview.Image = null;
}
if (!File.Exists(targetFile))
{
// make the loading gif invisible
this.picLoading.Visible = true;
//picPreview.Image = AdminTILE.Properties.Resources.StandardPaper;
}
else
{
this.picLoading.Visible = false;
FileStream fs = new FileStream(targetFile, FileMode.Open, FileAccess.Read);
picPreview.Image = System.Drawing.Image.FromStream(fs);
fs.Close();
}
}
}
/// <summary>
/// update the database with the current selections
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSave_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count == 0)
{
MessageBox.Show("No record is selected to update");
return;
}
DialogResult result1 = MessageBox.Show("Saving Label Configuration. Are you sure?",
"IMPORTANT!", MessageBoxButtons.YesNoCancel);
// update the view
if (result1 == DialogResult.Yes)
{
// we need the record for the current index
DataRowView currentDataRowView = (DataRowView)dataGridView1.CurrentRow.DataBoundItem;
DataRow currentRow = currentDataRowView.Row;
string pdcProductName = currentRow.ItemArray[0].ToString();
Int32 currentIndex = dataGridView1.SelectedRows[0].Index;
Debug.WriteLine("Current index in Save:" + currentIndex.ToString());
string AgencyId="", LogoId="", WireId="";
SqlDataAdapter LabeledProductsDataTableAdapter =
new SqlDataAdapter("SELECT * FROM LabeledProducts",
printConfigTableAdapter.Connection);
SqlDataAdapter LogosDataTableAdapter =
new SqlDataAdapter("SELECT * FROM Logos",
printConfigTableAdapter.Connection);
if (vwTILEAdminTableAdapter.Connection.State != ConnectionState.Open)
{
printConfigTableAdapter.Connection.Open();
}
DataTable LogoDataTable = new DataTable();
LogosDataTableAdapter.Fill(LogoDataTable);
DataTable LabeledProductsDataTable = new DataTable();
LabeledProductsDataTableAdapter.Fill(LabeledProductsDataTable);
StringBuilder sql = new StringBuilder();
// Fill a table with the results of the
// data adapter and query the table instead of the database.
// An empty LogoDescription maps to an empty filename
DataRow dataRow;
if (cbAgency.SelectedItem != null)
{
sql.Append("LogoDescription = '").Append(cbAgency.SelectedItem).Append("'");
dataRow = LogoDataTable.Select(sql.ToString())[0];
AgencyId = dataRow.ItemArray[0].ToString();
sql.Clear();
}
if (cbPrivateLabel.SelectedItem != null)
{
sql.Append("LogoDescription = '").Append(cbPrivateLabel.SelectedItem).Append("'");
dataRow = LogoDataTable.Select(sql.ToString())[0];
LogoId = dataRow.ItemArray[0].ToString();
sql.Clear();
}
if (cbWire.SelectedItem != null)
{
sql.Append("LogoDescription = '").Append(cbWire.SelectedItem).Append("'");
dataRow = LogoDataTable.Select(sql.ToString())[0];
WireId = dataRow.ItemArray[0].ToString();
sql.Clear();
}
// PdcProductName is the primary key
sql.Append(@"UPDATE [dbo].[LabeledProducts]
SET [PdcProductName] = @pdcProd
,[LabelProductName] = @lblProd
,[LabelDescription] = @lblDesc
,[Power] = @pwr
,[Fabrication] = 0
,[UL_File_Number] = @ul
,[PrePrintedSerial] = @pps
,[ShowOrderOnLabel] = 0
,[PrivateLabelLogoId] = @plid
,[AgencyImageId] = @aid
,[WireDiagConfigId] = @wid
,[ReleasedForProduction] = @rfp
WHERE PdcProductName = '").Append(pdcProductName).Append("'");
using (SqlCommand command = new SqlCommand(sql.ToString(), vwTILEAdminTableAdapter.Connection))
{
if (vwTILEAdminTableAdapter.Connection.State != ConnectionState.Open)
vwTILEAdminTableAdapter.Connection.Open();
LabeledProductsDataTableAdapter.UpdateCommand = command;
LabeledProductsDataTableAdapter.UpdateCommand.Parameters.AddWithValue("@pdcProd", txtPdcProdName.Text);
LabeledProductsDataTableAdapter.UpdateCommand.Parameters.AddWithValue("@lblProd", txtLabeledProd.Text);
LabeledProductsDataTableAdapter.UpdateCommand.Parameters.AddWithValue("@lblDesc", txtLabelDesc.Text);
LabeledProductsDataTableAdapter.UpdateCommand.Parameters.AddWithValue("@pwr", txtPower.Text);
LabeledProductsDataTableAdapter.UpdateCommand.Parameters.AddWithValue("@ul", txtULFileNumber.Text);
LabeledProductsDataTableAdapter.UpdateCommand.Parameters.AddWithValue("@pps", cbPrePrintedSerial.Checked);
LabeledProductsDataTableAdapter.UpdateCommand.Parameters.AddWithValue("@plid", LogoId);
LabeledProductsDataTableAdapter.UpdateCommand.Parameters.AddWithValue("@aid", AgencyId);
LabeledProductsDataTableAdapter.UpdateCommand.Parameters.AddWithValue("@wid", WireId);
LabeledProductsDataTableAdapter.UpdateCommand.Parameters.AddWithValue("@rfp", cbReleased.Checked);
//int rowsAffected = LabeledProductsDataTableAdapter.Update(LabeledProductsDataTable);
int rowsAffected = command.ExecuteNonQuery();
// The DataViewManager returned by the DefaultViewManager
// property allows you to create custom settings for each
// DataTable in the DataSet.
DataViewManager dsView = this.tILEDataSet.DefaultViewManager;
// remove the selectionChanged event handler during updates
// every update causes this handler to fire three times!!!
this.dataGridView1.SelectionChanged -= new System.EventHandler(this.dataGridView1_SelectionChanged);
dataGridView1.DataSource = typeof(TILEDataSet.vwTILEAdminDataTable);
this.vwTILEAdminBindingSource.DataSource = typeof(TILEDataSet.vwTILEAdminDataTable);
this.vwTILEAdminBindingSource.DataSource = this.tILEDataSet.vwTILEAdmin;
this.dataGridView1.DataSource = this.vwTILEAdminBindingSource;
vwTILEAdminBindingSource.ResetBindings(false); // false for data change, true for schema change
this.vwTILEAdminTableAdapter.Fill(this.tILEDataSet.vwTILEAdmin);
// we need to reget the row after the update to pass to preview
currentIndex = LastSelectedIndex;
DataGridViewRow drv = this.dataGridView1.Rows[currentIndex];
currentRow = ((DataRowView)(drv.DataBoundItem)).Row;
// update the preview files
UpdatePreviewFiles(currentRow);
// try this
dataGridView1.ClearSelection();
// this doesn't work
dataGridView1.Rows[currentIndex].Selected = true;
// reset the selection changed handler once the update is complete
this.dataGridView1.SelectionChanged += new System.EventHandler(this.dataGridView1_SelectionChanged);
}
}
}
//
///更新行选择更改时的预览
///
///
///
private void dataGridView1\u SelectionChanged(对象发送方,事件参数e)
{
如果(!BindingComplete)返回;
DataGridView dgv=(DataGridView)发送方;
如果(!dgv.Focused | | dgv.CurrentRow==null)返回;
//将pic预览设置为当前行图像
//我们需要当前索引的记录
DataRowView currentDataRowView=(DataRowView)dgv.CurrentRow.DataBoundItem;
if(currentDataRowView==null)返回;
DataRow currentRow=currentDataRowView.Row;
LastSelectedIndex=dgv.SelectedRows[0]。索引;
Debug.WriteLine(“SelectionChanged中的当前行:+currentRow.ItemArray[0].ToString());
bool-showBox=false,showProd=false,showWire=false;
线盒、针、线;
字符串pdcProductName=currentRow.ItemArray[0].ToString();
showWire=!string.IsNullOrEmpty(wire=currentRow.ItemArray[7].ToString());
showBox=!string.IsNullOrEmpty(box=currentRow.ItemArray[8].ToString());
showProd=!string.IsNullOrEmpty(prod=currentRow.ItemArray[9].ToString());
//检查f
this.picPreview.Image = Image.FromFile(imagePath);