AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=1097&pId=-1
Importing Dynamic Images to the Crystal Report without Database Overhead using Visual Studio 2005
page
by Pradeep Shukla
Feedback
Average Rating: 
Views (Total / Last 10 Days): 85261/ 51

Introduction

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.

Steps

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 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)
    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.

Conclusion

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-2024 ASPAlliance.com  |  Page Processed at 2024-04-27 12:28:57 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search