I 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.
- In your xsd, create a colomn 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.
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.
Good afternoon,just wana ask if what does "LoadLocation" in your code refers to?
ReplyDeleteIts actually pretty simple to import(or just call reference) images. I got this idea while implementing CSS to a report viewer page.
ReplyDeletestep1:
Use this in your report displaying aspx page
>html<
>head<
>style<
.classname
{
background:url(images/xyz.jpg) no-repeat left top; }
>style<
>/head<
>body<
>crp:crystalreportviewer load<
>/body<
>/html<
step2:
Insert a Textbox object in your .rpt file
step3:
Right click textbox object and select Format Object, under "CSS Class Name" give the classname mentioned in your aspx page.
step4:(optional)
If you are to use dynamic images, repeat step1 for all images and go to step3. Then create formulas to provide the respective classnames dynamically.
Its as simple as that.