Display image Dynamically on crystal report


This is very common requirement to display image on crystal report from database. Generally we store image path in database not whole image, because it increase the size of the database. I had the same requirement few days ago and search lot about the same. Finally I got the solution and decide to publish the post.

Here I put the code in C#, so just copy and paste the code and change it as per your need.

dsOrgDetails = objOrganisationMasterBal.GetOrganisationDetails();

if (dsOrgDetails.Tables[0].Rows.Count > 0)

{

DataRow drow;

dt.Columns.Add(“OrganizationImage”, System.Type.GetType(“System.Byte[]”));

dt.Columns.Add(“OrganizationName”, System.Type.GetType(“System.String”));

drow = dt.NewRow();

FileStream fs;

BinaryReader br;

if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + “OrganisationLogo\\” + dsOrgDetails.Tables[0].Rows[0][“Logo”].ToString()))

{

// open image in file stream

fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + “OrganisationLogo\\” + dsOrgDetails.Tables[0].Rows[0][“Logo”].ToString(), FileMode.Open);

}

else

{

// if photo does not exist show the nophoto.jpg file

fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + “OrganisationLogo\\noimages.jpg”, FileMode.Open);

}

br = new BinaryReader(fs);

// define the byte array of file length

byte[] imgbyte = new byte[fs.Length + 1];

// read the bytes from the binary reader

imgbyte = br.ReadBytes(Convert.ToInt32((fs.Length)));

drow[0] = imgbyte;

//drow[0] = br.Read(imgbyte, 0, imgbyte.Length);

drow[“OrganizationName”] = dsOrgDetails.Tables[0].Rows[0][“OrganisationName”].ToString();

// add the image in bytearray

dt.Rows.Add(drow);

dt.AcceptChanges();

// add row into the data table

br.Close();

// close the binary reader

fs.Close();

}

First get the data from database, here I mean get the image path from the database. Then create DataTable for temporary conversation of path into image. In DataTable, create column of type System.Byte[].

Then create objects of FileStream and BinaryReader. Check that the file is exists or not, if exists then open the file in FileStream’s object. Then assign the FileStream’s object to the BinaryReader and go ahead as per the code.

Now, add the generated DataTable with Image in your DataSet which is assigned to the crystal report’s datasource.

Now, see at crystal report you can find the “OrganizationImage”  in Field Explorer of Crystal Report. Just Drag n Drop the image on crystal report. That’s it.

In some cases, at run time Image is not displayed on crystal report using above code. So for that do one thing.

Comment out this line : drow[0] = imgbyte;

and Uncomment this line : drow[0] = br.Read(imgbyte, 0, imgbyte.Length);

Now, compile the solution and run the report. Again Drag n Drop the image on report. And do the reverse, i mean Uncomment drow[0] = imgbyte this line and Comment  drow[0] = br.Read(imgbyte, 0, imgbyte.Length); this line, and re-compile the solution and run the report. You will get the image on your report.

Advertisements

5 Responses to Display image Dynamically on crystal report

  1. rakeshbajania says:

    good article

  2. rakeshbajania says:

    vert good work

  3. George says:

    I tried the code in my asp.net 3.5 web application with embedded Crystal Reports. I am not able to get the image displayed.
    NO Error Message also. Only a box indicating the boundary of the image is visible!

    Can you please help?

    • rakeshbajania says:

      OK. But give some detail specification how can I help you?
      What I do with my code is as specified below:
      1) Get the data from the database and stored it in dataset.
      2) Created one datatable to store only Image and OrganizationName in my case.
      3) Then using FileStream and BinaryReader, get the bytes of image and store it in datatable’s Image column which is created on step 2.
      4) Then add this table to the dataset.
      5) You will get this table on crystal report then and just drag and drop the Image from the Field Explorer.
      That’s it.
      If still not succeed then send me your sample code and database. I will do at my end and send it to you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: