Click here to Skip to main content
13,248,343 members (37,819 online)
Click here to Skip to main content
Add your own
alternative version


53 bookmarked
Posted 11 Nov 2008

How to dynamically load images in Crystal Reports using Visual Studio 2005

, 19 Nov 2008
Rate this:
Please Sign up or sign in to vote.
How to dynamically load images in Crystal Reports in ASP.NET, using the TypedDataSet.


If you have an image path that is stored in your database, Crystal Reports .NET in Visual Studio 2003/2005 cannot display the image file dynamically unless you use the dynamic image location feature in Crystal Report XI.

I searched in the Web, and I found how to display this using a workaround.

Using the code

We must have a field (image path) in our database. We need to create a new DataSet/XML schema (XSD) (TypedDataSet) to use as resource data in creating a report, and add an additional field that is not in the table and which is of type base64Binary:

Or add it in XML:

< name=""image_stream"" type=""xs:base64Binary"" minoccurs=""0″">

When designing a report, drag and drop the “image_stream” field in the region where you want it to appear. Add CrystalReportViewer to your ASPX page. In the code-behind of your page, add the following method to load the image:

private void LoadImage(DataRow objDataRow, string strImageField, string FilePath)
        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));
        objDataRow[strImageField] = Image;
    catch (Exception ex)
        Response.Write("<font color=red>" + ex.Message + "</font>");

We need to fill the TypedDataSet, and before assigning this DataSet to the “SetDataSource” of our report, we need to add a few lines of code:

TypedDataSet ds = new TypedDataSet();

SqlConnection cn = new SqlConnection("Data Source=ServerName;" + 
                   "Initial Catalog=DataBaseName;User ID=UserName;" + 
SqlCommand Cmd = new SqlCommand();
SqlDataAdapter myAdapter = new SqlDataAdapter();

Cmd.CommandText = " Select * From TableName";
Cmd.Connection = cn;

myAdapter.SelectCommand = Cmd;

catch (Exception ex)

for (int index = 0; index < ds.Tables[0].Rows.Count; index++)
    if (ds.Tables[0].Rows[index]["image_path"].ToString() != "")
        string s = this.Server.MapPath(

        if (File.Exists(s))
            LoadImage(ds.Tables[0].Rows[index], "image_stream", s);
            LoadImage(ds.Tables[0].Rows[index], "image_stream", 
        LoadImage(ds.Tables[0].Rows[index], "image_stream", 

    // Finally display report in crystal report viewer
    ReportDocument crDoc = new ReportDocument();
    CrystalReportViewer1.ReportSource = crDoc;
catch (Exception ex)


I would like to thank the CodeProject team (moderators, users, visitors...) for all their effort. Thank you everybody.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Redouane Mounafia
Team Leader
Morocco Morocco
No Biography provided

You may also be interested in...


Comments and Discussions

AnswerRe: Thank-You Redouane - Just what I required. Pin
Redouane Mounafia6-Oct-11 22:54
memberRedouane Mounafia6-Oct-11 22:54 
Questionimage is not showing Pin
mayur csharp G4-Oct-11 2:28
membermayur csharp G4-Oct-11 2:28 
AnswerRe: image is not showing Pin
razzleblazzle6-Oct-11 10:09
memberrazzleblazzle6-Oct-11 10:09 
Questionwhat send the image to Crystall Reports? Pin
abbass0912873321-Apr-11 21:38
memberabbass0912873321-Apr-11 21:38 
Answerhelp me, there's an exception Pin
sarrenza14-Apr-11 17:19
membersarrenza14-Apr-11 17:19 
Generalpdf too large Pin
yi24-Oct-10 0:51
memberyi24-Oct-10 0:51 
GeneralFor desktop Pin
shailesh_j2-Dec-09 21:25
membershailesh_j2-Dec-09 21:25 
GeneralRe: For desktop Pin
Redouane Mounafia3-Feb-10 6:18
memberRedouane Mounafia3-Feb-10 6:18 
GeneralThanks Pin
Wags6-Aug-09 23:25
memberWags6-Aug-09 23:25 
GeneralRe: Thanks Pin
Redouane Mounafia7-Aug-09 9:30
memberRedouane Mounafia7-Aug-09 9:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 19 Nov 2008
Article Copyright 2008 by Redouane Mounafia
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid