IntroductionI found many developers facing the same problem as i do - dynamic include image to crystal report with Visual Studio 2005 . This is how we solve our problem in C# when developing a desktop window application. Implementation Steps- In your xsd, create a column that does not exist in your dataset, example, [LogoImage], and set the datatype to “System.Byte[]”. You might find the datatype option only have “System.Byte”, you can manually type in “[]”.
- Add the following code in your form.
private void AddImageColumn(DataTable objDataTable, string strFieldName)
{try{
DataColumn objDataColumn = new DataColumn(strFieldName,
Type.GetType("System.Byte[]"));
objDataTable.Columns.Add(objDataColumn);
}catch (Exception ex)
{//handler}
}private void LoadImage(DataRow objDataRow, string strImageField, string FilePath)
{try{
FileStream fs = new FileStream(FilePath, System.IO.FileMode.Open,
System.IO.FileAccess.Read);byte[] Image = new byte[fs.Length];
fs.Read(Image, 0, Convert.ToInt32(fs.Length));
fs.Close();
objDataRow[strImageField] = Image;
}catch (Exception ex)
{//Handler}
}
- After your getting your dataset from database and before bind to the report view, do this
private void BindReport()
{
AddImageColumn(myDataTable, "LogoImage");for (int index = 0; index < myDataTable.Rows.Count; index++)
{if (myDataTable.Rows[index]["LogoLocation"].ToString() != "")
{if (File.Exists(myDataTable.Rows[index]["LogoLocation"].ToString()))
{
LoadImage(myDataTable.Rows[index], "LogoImage",
myDataTable.Rows[index]["LogoLocation"].ToString());
}else{
LoadImage(myDataTable.Rows[index], "LogoImage","C:\\NoImage.jpg");
}
}else{
LoadImage(myDataTable.Rows[index], "LogoImage", "C:\\NoImage.jpg");
}
}
}
- Then in your Crystal Report, just drag this column into the report.
Hope this solve your problem.thank you for reading. |