Click here to Skip to main content
15,892,005 members
Articles / Programming Languages / Visual Basic
Article

Image in Crystal Reports

Rate me:
Please Sign up or sign in to vote.
4.50/5 (28 votes)
29 Oct 2007 422.8K   27.9K   74   55
This is sample small project to display image in crystal report
Screenshot - ImgInReport.gif

Introduction

This article will display the image in crystal report viewrBackground

Using the code

Simply open the source code in 2003.

    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.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Avenues Technologies Pvt Ltd
India India
MCTS Microsoft Certified Technology Specialist
C-DAC
Computer Engineer

Comments and Discussions

 
GeneralAccess denied exception Pin
nihad aliyev26-Nov-08 1:36
nihad aliyev26-Nov-08 1:36 
AnswerRe: Access denied exception - FileMode for FileStream Pin
IndomitablePhoenix11-Jan-11 8:29
IndomitablePhoenix11-Jan-11 8:29 
GeneralGreat !!! Pin
lcsistemas8-Oct-08 13:29
lcsistemas8-Oct-08 13:29 
Questionhow to make Image object on crystal report Pin
Deevan.net12-Sep-08 2:08
Deevan.net12-Sep-08 2:08 
AnswerRe: how to make Image object on crystal report Pin
hugoflores9-Apr-09 5:13
hugoflores9-Apr-09 5:13 
GeneralRe: how to make Image object on crystal report Pin
Dan Duggan26-Nov-09 6:38
Dan Duggan26-Nov-09 6:38 
AnswerRe: how to make Image object on crystal report - Answered Pin
IndomitablePhoenix11-Jan-11 8:44
IndomitablePhoenix11-Jan-11 8:44 
GeneralImage in Crystal Reports Pin
dkinjal_2823-Jul-08 19:18
dkinjal_2823-Jul-08 19:18 
Hello Mr, Anant Tiwari

Thanks a lot for your good help to display image in crystal report.
I am working with Microsoft VB.net 2005 with Access database. your code is working fine for display image dynamically. I have one problem, I also want to display text data with image in crystal report.

To display text data in crystal report i am using dataset which contains two tables of microsoft access database.
now i am confused that how can i add datatable created in your code to my data set at runtime??

i am giving my code for display text data in crystal report. please guide me how to merge your code with it?? because we can set only one Datasource of crystal report. For displaying image in report i have used your code. both code seperately working fine but i am not able to merge it.

Dim report As New ClientbyName
da1 = New OleDbDataAdapter("select * from CompanyMaster", con)
da1.Fill(ds, "CompanyMaster")
da2 = New OleDbDataAdapter("select * from PartyMaster order by PartyID", con)
da2.Fill(ds, "PartyMaster")
report.RecordSelectionFormula = "{PartyMaster.PartyType}='Customer'"
report.SetDataSource(ds)
With frmReport_View
.CrystalReportViewer1.ReportSource = report
.ShowDialog()
End With

Please guide and reply earliest on renishl@yahoo.com also.

Thanks a lot in advance.
AnswerRe: Image in Crystal Reports Pin
IndomitablePhoenix11-Jan-11 8:38
IndomitablePhoenix11-Jan-11 8:38 
GeneralGreat Pin
Member 458242511-Mar-08 5:15
Member 458242511-Mar-08 5:15 

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.