RSS

Image in Crystal Reports

22 Nov

Screenshot - ImgInReport.gif

Introduction

This article will display the image in crystal report viewrBackground

Using the code

Simply open the source code in 2003.

 Collapse | Copy Code
    try { 
        // here i have define a simple datatable inwhich image will recide 

        DataTable dt = new DataTable(); 
        // object of data row 

        DataRow drow; 
        // add the column in table to store the image of Byte array type 

        dt.Columns.Add("Image", System.Type.GetType("System.Byte[]")); 
        drow = dt.NewRow; 
        // define the filestream object to read the image 

        FileStream fs; 
        // define te binary reader to read the bytes of image 

        BinaryReader br; 
        // check the existance of image 

        if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "10157.Jpg")) { 
            // open image in file stream 

            fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "10157.Jpg", FileMode.Open); 
        } 
        else { 
            // if phot does not exist show the nophoto.jpg file 

            fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "NoPhoto.jpg", FileMode.Open); 
        } 
        // initialise the binary reader from file streamobject 

        br = new BinaryReader(fs); 
        // define the byte array of filelength 

        byte[] imgbyte = new byte[fs.Length + 1]; 
        // read the bytes from the binary reader 

        imgbyte = br.ReadBytes(Convert.ToInt32((fs.Length))); 
        drow(0) = imgbyte; 
        // add the image in bytearray 

        dt.Rows.Add(drow); 
        // add row into the datatable 

        br.Close(); 
        // close the binary reader 

        fs.Close(); 
        // close the file stream 

        CrystalReport1 rptobj = new CrystalReport1(); 
        // object of crystal report 

        rptobj.SetDataSource(dt); 
        // set the datasource of crystalreport object 

        CrystalReportViewer1.ReportSource = rptobj; 
        //set the report source 

    } 
    catch (Exception ex) { 
        // error handling 

        Interaction.MsgBox("Missing 10157.jpg or nophoto.jpg in application folder"); 
    } 
// run the application to view image in report

Remember if you are using c# the paste the above code in button click event

Points of Interest

In this article you will be also able to convert the image into byte array

History

In this project after clicking the button the action are performed you are free to modify as per your requirement
Feel free for any querrrrrries.

enjoy .net

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

 

Referenced by: http://www.codeproject.com/KB/vb/Image_in_Crystal_Reports.aspx

Advertisements
 
Leave a comment

Posted by on November 22, 2011 in Crystal Report

 

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: