LogoASPAlliance: Articles, reviews, and samples for .NET Developers
Importing Dynamic Images to the Crystal Report without Database Overhead using Visual Studio 2005
by Pradeep Shukla
Average Rating: 
Views (Total / Last 10 Days): 108248/ 183


When it comes to import a dynamic image to the Crystal report, one would like to follow the traditional approach of storing the image in the database and then pull it to the report.

But there are some overheads associated with using a database to store the binary/Images file. The primary being the database connections are more expensive and pulling your images out from the database may be time taking.

In this Article I would like to demonstrate how to achieve this without the intervation of the database.

To start with let say we have a dynamic chart that gets displayed in the aspx page and we need to display that chart in the report. Here we have another option i.e we can use the inbuilt chart feature of the Crystal Report. But it will not always be feasible if the chart is fully dynamic in nature. In one of my project I did face this issue. I simply can not have all the conditions for all the possible cases. So the simple workaround for me was just to save my image in the server at runtime and then import that to the Crystal Report and it worked fine.


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


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)


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


Dim rptTest1 As New ReportDocument
Current. Request.MapPath("App_Data/test1.bmp")))


ReportDocument rptTest1 = new ReportDocument();

Code for filling the data table with the Image

Listing 3


Public Class rptTest
  Public Shared Function ImageTable(ByVal ImageFile As StringAs 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)
    br = Nothing
    fs = Nothing
    Return data
  End Function
End Class


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);
    br = null;
    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.


In this article, we have seen how to import dynamic images to the crystal report without database overhead with the help of code examples.

Product Spotlight
Product Spotlight 

©Copyright 1998-2021  |  Page Processed at 2021-12-05 6:56:31 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search