我正在使用 VS2013 开发带有 SQL Server 数据库的 Windows 窗体应用程序。
我在数据库表中有一个列来存储图像名称:
在我的应用程序中,我创建了一个按钮来从我的计算机中选择图像并将该图像保存到应用程序启动路径:
private void buttonX1_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Image only. | *.jpg; *.jpeg; *png; *.gif;";
dlg.InitialDirectory = @"E:\";
dlg.Multiselect = false;
string a = null;
if (dlg.ShowDialog() == DialogResult.OK)
{
string[] tmp = dlg.FileNames;
foreach (string i in tmp)
{
FileInfo fi = new FileInfo(i);
string[] xxx = i.Split('\\');
string des = Application.StartupPath + @"\Images\" + xxx[xxx.Length - 1];
string desfolder = Application.StartupPath + @"\Images\";
imagename = xxx[xxx.Length - 1].ToString();
System.IO.Directory.CreateDirectory(desfolder);
File.Delete(des);
imageuploaded.Image = Image.FromFile(dlg.FileName);
//over.
fi.CopyTo(des);
imageList1.Images.Add(imagename, Image.FromFile(des));
//Process.Start("explorer.exe", desfolder);
}
MessageBox.Show("Thành công ");
}
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
该代码将从计算机加载图像,保存到启动路径中的文件夹“images”,并将图像添加到 imagelist1。
之后,我有一个按钮可以将图像名称插入“图像”列(在 SQL 服务器数据库中)。我有这段代码可以为我的网格使用 imagelist :
public PrdMan()
{
InitializeComponent();
GridPanel panel = superGridControl1.PrimaryGrid;
GridColumn column = panel.Columns["image"];
column.EditorType = typeof(MyGridImageEditControl);
column.EditorParams = new object[] { imageList1, ImageSizeMode.Zoom };
//superGridControl1.PrimaryGrid.Columns[8].Visible = false;
//superGridControl1.PrimaryGrid.Columns[2].CellStyles = "dd/MM/yyyy";
//styleManager1.ManagerStyle = eStyle.Metro;
//
//this.Location = new Point(0, 0);
//this.Size = Screen.PrimaryScreen.WorkingArea.Size;
}
和加载网格的代码:
this.mPhamTableAdapter.Fill(this.beautyMaDataSet.MPham);
this.superGridControl1.PrimaryGrid.DataSource = this.beautyMaDataSet.MPham;
我的问题是:当我加载图像并将其插入数据库的“图像”列时:成功,网格将显示图像。但是当我关闭应用程序(发布后)或停止调试(在 VS 中)然后重新打开(或再次调试)。网格不会显示我的图像
即使图像仍在文件夹中:
并且 imagelist 在列表中没有图像:
我不知道我的问题是什么。你能支持我如何:
1/ 使用 C# 将图像从 PC 添加到启动路径的文件夹并将图像名称保存到 SQL 服务器(将图像绑定到网格)。
2/ 将启动路径文件夹中的图像绑定到图像列表并使用它。
谢谢你的支持。