成品展示
概述:点击dgv的任意列展现出当前点击列的图片
代码:
/// <summary>
/// 图片展示方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public class SetImg
{
public List<string> GetImgsrc()
{
List<string> listImgsrc = new List<string>();
string sql = "select imge from ws";
DataTable dt = DBHelper.Select(sql);//执行查询语句,返回一个表。
foreach (DataRow row in dt.Rows)
{
listImgsrc.Add(row["imge"].ToString());//将数据一条一条的加入的list中
}
return listImgsrc;//返回这个集合
}
}
SetImg st = new SetImg();
public System.Drawing.Image GetImage(string path)
{
System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open);
System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
fs.Close();
return result;
}
dgv点击事件:
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//System.Diagnostics.ProcessStartInfo processStartInfo = new System.Diagnostics.ProcessStartInfo();
//processStartInfo.FileName = "explorer.exe"; //资源管理器
//processStartInfo.Arguments = db.INIhelp.GetValue("DZ", "dz") + "\\" + list[e.RowIndex];
//System.Diagnostics.Process.Start(processStartInfo);
var list = st.GetImgsrc();
pic p = new pic(db.INIhelp.GetValue("DZ", "dz") + "\\" + list[e.RowIndex]);
p.ShowDialog();
}
form2窗体:图片使用prc控件

代码:
public partial class pic : Form //窗体之间传值
{
string imgstr = string.Empty;
public pic(string s)
{
InitializeComponent();
imgstr = s;
}
private void pic_Load(object sender, EventArgs e)
{
pictureBox1.ImageLocation = imgstr;
label1.Text = imgstr;
}
}

此篇博客介绍了如何通过C#代码实现数据表中图片的快速预览,用户只需点击DataGridView单元格,即可在Form2窗口的PictureBox中显示对应的图片。代码包括数据库查询、图片读取和控件操作,适合数据库管理和前端展示的需求。
窗体展示图片&spm=1001.2101.3001.5002&articleId=121264018&d=1&t=3&u=f043b12ccfd043249e4213dfa0bdce73)
2502

被折叠的 条评论
为什么被折叠?



