Click here to Skip to main content
Click here to Skip to main content

Image in Crystal Reports

By , 29 Oct 2007
 
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

About the Author

TangoCharli (Anant Tiwari)
Software Developer (Senior) Avenues Technologies Pvt Ltd
India India
Member
MCTS Microsoft Certified Technology Specialist
C-DAC
Computer Engineer

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membersupriya chaladi28 Jan '13 - 22:12 
THANK U IT HELPED ME ALOT
GeneralThnk umemberkiransolkar24 Dec '12 - 21:05 
Great Solution,
Veryy much thanks Wink | ;) Thumbs Up | :thumbsup:
QuestionBlob FieldmemberRaghavendra Rao 55815 Nov '12 - 1:01 
Hi Dude,
You have given a nice article for displaying images in reports..
 
But how to create Blob field. While selecting Report preview there displays an error
Questiondatabase expert querymemberNikhil S Soni23 Oct '12 - 0:40 
how did u add img field in database expert field explorer in crystal report..?
 
I dont get it how to do it..?
 
plzz help me doing that sort of work...
AnswerRe: database expert querymemberkiransolkar24 Dec '12 - 20:41 
Me too not getting how to add img field in database expert field in Crystal report
Plz help
GeneralMy vote of 5membermanoj kumar choubey23 Feb '12 - 19:13 
Nice
Generalwant image to be shown on special point of Reportmemberedinazem2 May '11 - 21:34 
Dear friends
I want the image be shown on special point (place) of the report, say , an image place holder is needed in the report, how can I implement it ?
GeneralMy vote of 5groupsohebrapati19 Mar '11 - 1:31 
Solve my Problem
AnswerSimple Solution - BlobField - How answeredmemberIndomitablePhoenix10 Jan '11 - 12:02 
Unsure | :~ So a lot of people were wondering how to create the IBlobField in the Crystal Reports Confused | :confused:
Here's a simple solution Shucks | :->
 
1. Creat a VARBINARY(MAX)/Image in the database (SQL)
[Image is more recommended as for the size it would hold & as it works for both images & pdf files]
(*)for more details: {http://www.teratrax.com/sql_guide/data_types/sql_server_data_types.html}

2. Include the database (using the Database Expert) datasource for the Crystal Report
3. Drag the column onto the Crystal Report & it will automatically become a IBlobField
 
i.e. the image is converted to Byte() type to be read with the binary reader; hence you store the binary data in the database (the converted image) - this also allows you to store the images in the database instead of image files.
Just make the conversion of the image @ the time of data-entry, then save it (for eg. using a dataAdapter update query) in the database. Now, everytime you view the data in the Crystal report field - it will show the image with no need for run-time conversion.
---------------------------------------------------------------------------------------------------------------------------------
In case of run-time solution on how to create the Dataset that allows the dragging of the IBlobField OMG | :OMG:
Here's another solution Shucks | :->
{adapted from the post: Image in Crystal Reports without logon to database popup by Huseyin Altindag which has been posted on 15:34 20 Feb '10}
 
1.Compile the sample code after making the following addition/change:
Add the following line after to the addition of the dt to the ds:
ds.WriteXmlSchema(Server.MapPath("mydataset.xsd"))
 
or ds.WriteXml("mydataset.xml")
 
(*)for more details:{http://dotnetslackers.com/articles/ado_net/MappingDataSetToXMLAndBackwards.aspx}
{http://code.google.com/p/ndbunit/wiki/QuickStartGuide}
 
2.After running/executng the code; Go to the path where you set (in this case it's: "Server.MapPath" & look for the file "mydataset.xsd") & edit the name of the dataset or any attribute in it in the xml file then save it.
 
3.Now "Add a new item" to the project & set the "File path" of the dataset to the xml file in step 2. (this will be save as a dataset - from an XML file)
 
Now you can use that data set (byte column) & drag it to the crystal report to be used for the conversion @ run-time.
 
Note: the run-time solution helps reduce the overhead & trouble for the SQL Server in handling images in addition to run-time loading, also it is better to just store the file path of the image/pdf rather than the actual file (the storage would grow exponentially with the addition o records).
 
Roll eyes | :rolleyes: Hope this helps Smile | :)

modified on Wednesday, January 12, 2011 5:57 PM

Generalpdf too largememberyi23 Oct '10 - 23:44 
Hi,
 
My problem is that : my PDF generated from Crystalreport with BlobField from dataset is too lagre.
I think to remplace the blobfield by icroleobject picture but I don't know how...
 
Maybe an idea ??
 
Thanks a lot,

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 30 Oct 2007
Article Copyright 2007 by TangoCharli (Anant Tiwari)
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid