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,
AnswerRe: pdf too large [modified]memberIndomitablePhoenix11 Jan '11 - 7:38 
Have you tried to set the following attributes for the IBlobField Confused | :confused: :
{right-click the IBlobField & select "Format Object"}
 
1) "Can Grow" should be deselected if you want the size to be finite & not dependant on the size of the image
(if it is selected, the crystal report will adjust the document to be enlarged according to the actual image size Shucks | :-> )
-------------- a) you could also add conditions for the "Can Grow" property (click on the button with a pen & "X" sign on it) -------------- using the "Formula Workshop - Format Formula Editor - Can Grow" tool to specify those conditions
 
2) "Close border on page break" should be selected
 
3) "Keep object together" should be selected
 
4) In the "Picture" tab, you can set the "Scaling", or the "Crop From" or the "Size" attributes to manipulate your image display in the report
 
5) you can add a special section for the image by adding subsections in the report (right click on an empty area in the report, Insert -> Section) [OR] (right-click on a section header, "Selection Expert" or "Insert section below") & then control/set the section properties {you can set "Suppress blank section" for the particular section}
 
Also, attempt other document type conversion (other than PDF : .doc, .rtf, .xls) & see if the same problem occurs. You can attempt to extract to formats which aren't too large & then converting those to pdf either by using macros or plug-ins or active-X components....depends on your application.
-------------Tip: When converting the document it is better using the Crystal Report Viewer; but you can also do
-------------this programmatically to be done @ run-time.
 
Often the case is that some design elements (from the toolbox) being not properly set cause layout inconsistencies (i had a problem with showing bi-directional text when converting the crystal report into pdf Sigh | :sigh: - the non-english text would be at one side & the other english text on the other with space between them in one line to mimic a justified layout but OMG | :OMG: one side wouldn't appear when converted Dead | X| ); these could be fixed by separating elements that are of different types ( D'Oh! | :doh: in this case creating separate textboxes for each type of text D'Oh! | :doh: ) or other work-arounds.
 
For other possible solutions on how to create a blobfield for the image - check out this link: http://www.eggheadcafe.com/community/aspnet/2/10119558/converting-binary-image-to-bitmap-image.aspx Big Grin | :-D
 
Roll eyes | :rolleyes: I hope this helps Laugh | :laugh:

modified on Tuesday, January 11, 2011 1:45 PM

Generali want image should come from database from msaccessmemberkrunal108915 Sep '10 - 8:45 
i want to save a image in database & reteiv image from data base using code in vb. net
 
anybody pls help
thanks in advance
AnswerRe: i want image should come from database from msaccessmemberIndomitablePhoenix11 Jan '11 - 7:55 
Try setting it as a "Picture" field.
 
You can find other possible answers here:
http://www.tek-tips.com/viewthread.cfm?qid=1411438
 
Roll eyes | :rolleyes: Hope this helps Big Grin | :-D
GeneralBlob Fieldmemberenzo583 Aug '10 - 19:44 
I'm working with VS2005
how can I insert blob field in CR
Thanls
AnswerRe: Blob Field [modified]memberIndomitablePhoenix11 Jan '11 - 7:56 
kindly read my latest post (Titled: Simple Solution - BlobField - How answered) on how to set the BlobField above - I was using VS2005 as well Big Grin | :-D

modified on Tuesday, January 11, 2011 2:03 PM

GeneralMiticomemberpanicemi8 Jun '10 - 4:41 
Grandioso!
GeneralImage in Crystal ReportsmemberHuseyin Altindag16 Feb '10 - 9:12 
Hi,
 
How can I get rid "database login" popup which I think is related to the database definition file .ttx and ?
Thanks
 
HA
GeneralRe: Image in Crystal Reports without logon to database popupmemberHuseyin Altindag20 Feb '10 - 9:34 
I found a workaround for the “logon to the data source” popup running the crystal report in case you do not know the “Server/User ID/Password/Database”.
Add 1,2,3
1. DataSet dset = new DataSet();
 
DataTable dt = new DataTable("idcard"); //
DataRow drow = null;
 
//Add columns to table collection
dt.Columns.Add("image", System.Type.GetType("System.Byte[]"));
 

drow = dt.NewRow();
2. dset.Tables.Add(dt);
3. dset.WriteXmlSchema("mydataset.xml");
 
and run the application, it will create a new XML-file(in my case) "mydataset.xml" in bin/debug and open it and change the element name or add new element name and save.Now go back to Visual Studio and add a new crystal report. If report wizard runs, select blank report.
In Field explorer right click->Database expert->Create New Connection->ADO.NET. In ADO.NET File Path chose in bin/debug/"mydataset.xml” and click finish.You can now use the crystal report without the login-popup.it works fine for me .
 
Huseyin Altindag
AnswerApply by use sub report no need to implement BLOB fieldmemberpakpoompok10 Feb '10 - 15:43 
===============================================================================
'PREVIEW BUTTON --------------------------------
If InStr(Application.StartupPath, "bin") > 0 Then
Path = Application.StartupPath.Substring(0, Len(Application.StartupPath) - 4)
Else
Path = Application.StartupPath
End If
Dim pic As String = gbPictureFolder & removeSign(Me.product.Trim) & ".jpg"
If Not File.Exists(pic) Then
pic = Path & "\Image\noimage.jpg"
End If
Dim ds As New DataSet
Dim dt As DataTable
ds.Tables.Add(ImageTable(pic))
dt = ImageTable(pic)
 
With crReportDocument
.Load(Path & "\Report\BOMSheet.rpt")
.SetDataSource(reportTable)
pdv.Value = Me.title.Trim
pv.Add(pdv)
.DataDefinition.ParameterFields("ReportName").ApplyCurrentValues(pv)
 
'SETUP DIRECT DATA SOURCE TO SUBREPORT----------------------------------------
'APPLY BY USE SUB REPORT===================================
Dim srpt As New ReportDocument
srpt = .OpenSubreport("ImgReport.rpt")
srpt.SetDataSource(dt)

End With
 
With CrystalReportViewer1
.ReportSource = crReportDocument
.Zoom(0)
End With
GeneralBlob FieldmemberMember 44854195 Dec '09 - 13:46 
How to create blob field in crystal report.
i have made a dataset in which i created a column with System.Byte() data type.But it is not working..
Plz. solve my problem its urgent.
Thanks
GeneralRe: Blob FieldmemberMember 607717516 Dec '09 - 2:49 
did u get the solutions for this?
GeneralRe: Blob FieldmemberMember 448541928 Dec '09 - 18:35 
Is there anhyone to solve my problem .its urgent.i have to complete my project with in a week.my rest of all project have completed.i have only problem with dis blob field
AnswerRe: Blob Field - AnsweredmemberIndomitablePhoenix11 Jan '11 - 8:06 
This might be too late but for the solution Dead | X| --- kindly read my latest post (Titled: Simple Solution - BlobField - How answered) on how to set the BlobField above Blush | :O
Generalblob field object [modified]memberapol524 p4 Nov '09 - 14:22 
where can i get the blob field object? i am using vs net 2005
 
modified on Wednesday, November 4, 2009 10:30 PM

AnswerRe: blob field object - AnsweredmemberIndomitablePhoenix11 Jan '11 - 8:09 
kindly read my latest post (Titled: Simple Solution - BlobField - How answered) on how to set the BlobField above - I was using VS2005 as well

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

Permalink | Advertise | Privacy | Mobile
Web04 | 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