Now let's get into details. First of all say in our aspx
page we need to save the image to the server at the runtime. For this we need
to use the stream class to read the file as a byte and then save it to the
required path at a particular event. The event can vary from situation to
situation. e.g in my project I did this in the page_load event. Some would
definitely like to avail this just before producing the report.
Code for storing your Image in to the server
Listing 1
VB.NET
Dim drawingImage As System.Drawing.Image
drawingImage = System.Drawing.Image.FromStream(New System.IO.MemoryStream(byteArr))
Dim strPath As String
strPath = System.Web.HttpContext.Current.Request.MapPath("path")
drawingImage.Save(strPath, System.Drawing.Imaging.ImageFormat.Bmp)
C#
System.Drawing.Image drawingImage;
drawingImage = System.Drawing.Image.FromStream(new System.IO.MemoryStream (byteArr));
string strPath;
strPath = System.Web.HttpContext.Current.Request.MapPath("path");
drawingImage.Save(strPath, System.Drawing.Imaging.ImageFormat.Bmp);
After saving the image now its turn for designing the
report. For this we need a dataset(xsd) which will store the Image.
For this add a dataset to your project. Create a datatable
inside your dataset and add a column to the datatable for storing the image.One
thing to note here is the datatype for the column. It should be System.Byte[].
Figure 1
After Creating the DataSet we can start on designing the
report. Open the Crystal report, add the DataTable to the database fields of
your report and then drag and drop the image field to the report.
Now for populating the datatable with the data at the
runtime we will have a function inside a class that takes one paramater i.e the
Image that needs to be displayed and using the data row we will add it to the
datatable and finally return the datatable.
Now for displaying it actually in the report, set the
datasource of the crystal report with the data table. The below code
demonstrates this.
Code for setting the datasource of the Crystal Report
Listing 2
VB.NET
Dim rptTest1 As New ReportDocument
rptTest1.Load(System.Web.HttpContext.Current.Request.MapPath("App_Reports/rptpic1.rpt"))
rptTest1.Database.Tables("Images").SetDataSource(rptTest.ImageTable(System.Web.HttpContext.
Current. Request.MapPath("App_Data/test1.bmp")))
C#
ReportDocument rptTest1 = new ReportDocument();
rptTest1.Load(System.Web.HttpContext.Current.Request.MapPath("App_Reports/rptpic1.rpt"));
rptTest1.Database.Tables["Images"].SetDataSource(rptTest.ImageTable(System.Web.HttpContext.
Current.Request.MapPath("App_Data/test1.bmp")));
Code for filling the data table with the Image
Listing 3
VB.NET
Public Class rptTest
Public Shared Function ImageTable(ByVal ImageFile As String) As DataTable
Dim data As New DataTable
Dim row As DataRow
data.TableName = "Images"
data.Columns.Add("img", System.Type.GetType("System.Byte[]"))
Dim fs As New FileStream(ImageFile, FileMode.Open)
Dim br As New BinaryReader(fs)
row = data.NewRow()
row(0) = br.ReadBytes(br.BaseStream.Length)
data.Rows.Add(row)
br = Nothing
fs.Close()
fs = Nothing
Return data
End Function
End Class
C#
public class rptTest
{
public static DataTable ImageTable(string ImageFile)
{
DataTable data = newDataTable();
DataRow row;
data.TableName = "Images";
data.Columns.Add("img", System.Type.GetType("System.Byte[]"));
FileStream fs = new FileStream(ImageFile, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
row = data.NewRow();
row[0] = br.ReadBytes(br.BaseStream.Length);
data.Rows.Add(row);
br = null;
fs.Close();
fs = null;
return data;
}
}
When the program runs, the image gets added up to the data
table and then is pulled up by the crystal report without any database
overhead.