日期:2014-05-19  浏览次数:20883 次

WIN程序,如何动态绑定图片?
ListBox里要求根据数据库给定的值,动态绑定四张图片,我原来是通过反射来动态加载程序集中嵌入资源的四张图片.可是总提示出错.因此还发贴问过,原贴:http://community.csdn.net/Expert/topic/5643/5643689.xml?temp=1.278102E-03
现在还有别的什么办法可以实现动态绑定嵌入资源的四张图片吗?
哪位朋友帮我解决,一起结贴给分.谢谢.

------解决方案--------------------
一种思路:图片采用文件,加载采用路径就可以了
------解决方案--------------------
我的思路同樓上的
這是我的方法
1.獲得圖片路徑並調用方法設置圖片
string picturePath=UserParameter.ImagePath+this.dataCenter1.CurrentDataRow[ "service_empl_sfz "].ToString().Trim()+ "e.jpg ";
ShowPicture(this.panel1,this.pictureBox1,picturePath,PictureBoxSizeMode.StretchImage);

2.相關方法
#region 獲得人員圖片資料
/// <summary>
/// 獲得指定人員的DataTable類型的圖片資料
/// </summary>
/// <param name= "emplIndex "> 系統編號 </param>
/// <param name= "imageFilename "> 所對應的圖片文件路徑和名稱 </param>
/// <returns> DataTable </returns>
public static DataTable getPersonnelImage(string emplIndex,string imageFileName)
{
DataTable result=new DataTable( "Hr_Report_IndexVsImage ");
DataColumn dc=new DataColumn( "empl_index ",typeof(string));
result.Columns.Add(dc);
dc=new DataColumn( "empl_image ",typeof(Byte[]));
result.Columns.Add(dc);
byte[] image=getImage(imageFileName);
if(image!=null)
{
DataRow dr=result.NewRow();
dr[0]=emplIndex;
dr[1]=image;
result.Rows.Add(dr);
}
return result;
}

/// <summary>
/// 根據文件名,讀取圖象文件為byte[]值
/// </summary>
/// <param name= "fileName "> 图像的文件名 </param>
/// <returns> 圖象文件的byte[]類型的值;否則,為null </returns>
private static byte[] getImage(string fileName)
{
byte[] result=null;
try
{
if(File.Exists(fileName))
{
FileStream fs = new FileStream(fileName, FileMode.Open); // 创建文件流
BinaryReader br = new BinaryReader(fs); // 创建二进制读取器

result=br.ReadBytes((int)br.BaseStream.Length);

br = null;
fs = null;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message+ "\r\n\r\n "+fileName, "Error ",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
return result;
}
#endregion