<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/platform.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d8260084\x26blogName\x3dInspirone\x26publishMode\x3dPUBLISH_MODE_BLOGSPOT\x26navbarType\x3dBLUE\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttps://inspirone.blogspot.com/search\x26blogLocale\x3den_GB\x26v\x3d2\x26homepageUrl\x3dhttp://inspirone.blogspot.com/\x26vt\x3d5304759022731441925', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe", messageHandlersFilter: gapi.iframes.CROSS_ORIGIN_IFRAMES_FILTER, messageHandlers: { 'blogger-ping': function() {} } }); } }); </script>

Inspirone

"I maintain that Truth is a pathless land, and you cannot approach it by any path whatsoever, by any religion, by any sect. Truth, being limitless, unconditioned, unapproachable by any path whatsoever, cannot be organized; nor should any organization be formed to lead or to coerce people along any particular path. You must climb towards the Truth. It cannot be 'stepped down' or organized for you." - author: Jiddu Krishnamurthi

Tuesday, January 09, 2007

Loading images dynamically in Crystal Report

Having come across a few issues related to the above I thought it might be worth to post the solution here so as to share it with other delvelopers.

Visual Studio 2003/2005 does not readily allow you to load an image file on a Crystal Report dynamically unless you use "Dynamic Image Location" feature which is available in Crystal Reports Developer XI. But there is a cheaper work around for this. The solution is to dynamically add an binary column to the XSD dataset and in the dataset containing the data to be printed. Then load the image file as binary data into the dataset.

1. In a column in your database table store the path to the image that needs to be displayed in a crystal report document, in this example the image path column name is "ImagePath".

2. Create a new Dataset/XML schema(xsd) in VS.Net that maps the fields in the database table. This should be the normal procedure when creating a report. In this XSD DataSet add an additional field that is not in the table and which is of type base64Binary :

xs:element name="Image" type="xs:base64Binary" minOccurs="0"

Note that the opening and closing tags are missing above since it won't be printed on this site. Just add enclose the above in "< />"

3. When designing the report based on this DataSet, drag and drop the "Image" field in the region where you want it to appear.

4. Add the following procedures to your code:

Private Sub AddImageColumn(ByVal objDataTable As DataTable, ByVal strFieldName As String)
Try
'create the column to hold the binary image
Dim objDataColumn As DataColumn = New DataColumn(strFieldName, Type.GetType("System.Byte[]"))
objDataTable.Columns.Add(objDataColumn)
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub

'*... and this one to load the image in the above added field:
Private Sub LoadImage(ByVal objDataRow As DataRow, ByVal strImageField As String, ByVal FilePath As String)
Try
Dim fs As System.IO.FileStream = New System.IO.FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
Dim Image() As Byte = New Byte(fs.Length) {}
fs.Read(Image, 0, CType(fs.Length, Integer))
fs.Close()
objDataRow(strImageField) = Image
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub

5. Before assigning the dataset to the "SetDataSource" of your report, add the following code:

' Fill the dataset "DS" as required with data to be displayed on the report
'* Begin code to add
AddImageColumn(DS.Tables(0), "Image")
' Loop to load the image for each row
For index As Integer = 0 To DS.Tables(0).Rows.Count - 1
If Not String.IsNullOrEmpty(DS.Tables(0).Rows(index).Item("ImagePath").ToString) Then
LoadImage(DS.Tables(0).Rows(index), "Image", _
DS.Tables(0).Rows(index).Item("ImagePath").ToString)
Else
'* You could load a default image here, like "no image to display"
'* if you have a standard one then un-comment the code below and change the image path
'LoadImage(DS.Tables(0).Rows(index), "Image", "c:\MyDefaultImage.JPG")
End If
Next
'* End code to add
rptDoc.SetDataSource(DS.Tables(0))
' Display report in a report viewer control



64 Comments:

Anonymous Anonymous said...

You are really so helpful !
Thank you man...:-)

Have a nice year ahead !

09 January, 2007 09:47  
Anonymous Anonymous said...

Hi

A slight modification to the code.

This line:
Dim Image() As Byte = New Byte(fs.Length) {}

should be
Dim Image() As Byte = New Byte(fs.Length - 1) {}

VB.NET array declaration specifies the upper bound element while C# states the number of elements.

Another of those tricks that Bill Gates likes to mess up with.

20 January, 2007 12:01  
Anonymous Anonymous said...

Hey,
that was a really cool hint.
I just tried to do exactly what you've been doing here.
I really got messed up and wondered why the ---- there was nothing like base64binary in this type-schema of the Dataset.
You saved me a lot of time!
Thanks a lot

08 March, 2007 16:39  
Blogger Sonam K said...

I am unable to load the photo from the path stored in SQL 2000 server. I am using VS2005 and the crystal report bundled with VS. I am using a stored proc to load the student details, which also contains the image location. Since I will be printing multiple cards on one sheet (A4 size), I need to ensure that the pictures are displayed properly. Some student photo will not be there. The picture name and student id are similar. The pictures are in JPG format. Please help at the earliest possible

06 July, 2007 14:51  
Anonymous Anonymous said...

Hi. Thanks a lot for the code, you saved my day!

03 August, 2007 01:03  
Blogger inspirone said...

hi sonamk, the code I posted should work for you as well. You just need to populate your dataset with the results retrieved by your stored proc and everything else is the same...

03 August, 2007 10:47  
Anonymous Anonymous said...

Hi,

Well done ! It works really fine.

I just have a question. Have you an idea to keep the aspect ratio of images. Typically, I want to display images having distinct formats ?

Thank's in advance,

Ety.

31 August, 2007 14:05  
Blogger inspirone said...

Hi Ety,

thanks for the comment. I'm not sure if this would solve your issue but if you right-click on the Image field and select Format Object, you can check the option Can Grow. This should normally ensure that the aspect ratio of the image remains unchanged. Please let me know if this helps.

31 August, 2007 15:36  
Anonymous Anonymous said...

Of course it works !

I did'nt think to edit the Format Object. I only looked at the field property.

Many thanks,

Ety.

31 August, 2007 15:57  
Anonymous Anonymous said...

Hi,
ur code is really helpful!!!...
but im getting the following error:

"URI formats are not supported"

actually my photos are on server...& i hav to display those photos in crystal report

14 September, 2007 13:23  
Blogger inspirone said...

Hi Ankit, it seems that this work around does not support URL as image paths. You will need to host the images on a local server and map a drive to the server. Hope this helps. Please let me know if it works.

14 September, 2007 13:51  
Anonymous Anonymous said...

Hi.....
thanx a million!!!!!...
it WORKED!!!!

it was coz of some security reasons i was not storing images locally....but my boss said no prob!!!!...

14 September, 2007 16:37  
Anonymous Anonymous said...

Nice post Inspirone.

I would like to request for your help. I have a dynamic images which was generated from OWC, my question is that, is it possible to print this images without using data source just link only?
Thanks

18 September, 2007 08:44  
Blogger CicheR said...

Very nice.

A little hint:

----------------------------------
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
----------------------------------

This code go agains bests practices, beacause this does nothing more than lose information of the original exception.

19 September, 2007 22:33  
Anonymous Anonymous said...

Does this approach support gif files or just jpg?

24 September, 2007 03:38  
Anonymous Anonymous said...

Excelent! ;)

24 October, 2007 21:55  
Blogger aparna said...

hello,
everybody

myself is aparna
i have used the above code but it is giving me an error
"input string was not in correct format"

please help me

03 November, 2007 12:08  
Blogger aparna said...

hello,
inspireone

i have used the code you have given byt it is giving me an error
"Input string was not in a correct form"

03 November, 2007 12:10  
Anonymous Anonymous said...

Hi,

This was working only with Jpg format. Not supporting for other(gif,bmp etc)

03 March, 2008 15:40  
Anonymous Anonymous said...

Thanks, helped me!

05 March, 2008 14:28  
Anonymous Anonymous said...

Its Work Great for VS2003 bundled version, Thanks for your Coding

Sathugudi Software- Saba

08 March, 2008 08:21  
Blogger Tai Nguyen said...

Thanks. I try it

Crystal Report XI can solve the problem, but can't preview in VS2005. Who can preview CR XI in VS2005?

14 April, 2008 13:04  
Blogger Sachin said...

Hi,

I am able to display images on crystal report run time, but the aspect ratio is maintained as I set property can grow. But the images get shrinked.

Is there any solution to show the images in their original size?

Regards,
Sachin Devre.

20 June, 2008 11:18  
Blogger inspirone said...

Hi Sachin, are you setting the property can grow to the image object or to the section of the report? Ensure that both have this property set to true. Else even if your image can grow it will be limited by the amount of space allocated by the report section or vice versa. Please let me know how it goes. Thanks

21 June, 2008 14:32  
Blogger Sachin said...

Thanks,
I have set 'Can Grow' property of object; but not able to set this property for section. When I click on section expert, it doesn't shows such
property.
I have worked with several crystal reports and whenever I set the can-grow property of any object, section gets expand if required without setting this property to section.
Can you please let me know how to set the 'Can Grow' property of section?

23 June, 2008 08:01  
Blogger Sachin said...

Hi,

after investigation we found that Crystal report shows images with their print size and not the actual size.
The images for which we had issue, were processed in our system. While processing the images, their print size were getting changed. We are modifying the image processing function to maintain print size to its actual size.

21 July, 2008 09:24  
Anonymous Anonymous said...

Hi there. I've been unable to get your code to work. When I add the Image element to the dataset code, it won't show up in my crystal report for me to drag it over to the report. Any help would be appreciated.

04 August, 2008 17:39  
Blogger Sachin said...

Hi,

You have to configure a database with image type field in Crystal report file. In my case I have used XSD file for database and had field with data type 'base64Binary' for image along with other required fields.

Once you configure such database with your .rpt file, It will show list of available fields. Then you can drag and drop image field in .rpt file

05 August, 2008 18:07  
Blogger inspirone said...

Hi Jane, Crystal Report in VS 2003 (not too sure if its same in 2005) sometimes plays tricks with you. When you update your XSD dataset after you have already bind it to your report, it would not show you the new fields. I suggest that you Update your XSD, Save it and close your IDE. Then open the project again again and Refresh the datasource of your report. Sometimes it work after you do it a few times. Why this happen? Well let's consider this as the spice in a developers life :) Ah and yes in case you can no longer see your field explorer just hit "Ctrl + T"...

06 August, 2008 10:05  
Blogger IMG POP said...

hi inspireone,

nice article mate...i have a question..i need to display the idcards in two columns like i have posted an image...could u plz help how can i achieve this..

http://www.imgpop.com/images/l95sja00s4et8zojjsve.jpg

Thanks,
GOPI.

07 August, 2008 11:09  
Blogger IMG POP said...

hi inspireone,

nice article mate...i have a question..i need to display the idcards in two columns like i have posted an image...could u plz help how can i achieve this..

http://www.imgpop.com/images/l95sja00s4et8zojjsve.jpg

Thanks,
GOPI.

07 August, 2008 11:10  
Blogger inspirone said...

hi gopi, I don't think you will be able to do that. Even with sub reports it will be difficult to put 2 in a same section and have them display data from a different records...

07 August, 2008 11:27  
Blogger Sachin said...

Hi,

You need to set layout of your Crystal report file. In section expert for section 'Details', go to tab 'Lay out'. If tab is not visible, select option 'Format with multiple columns' and set the details to get multiple column layout.

07 August, 2008 12:52  
Blogger IMG POP said...

Thanks for your quick replies...

@inspirone: There should be a way to do this..because i have seen a sample report which have the output of two columns of ID Cards..

@Sachin: This may be the solution..but i am not able to figure it out..i have changed the layout and set the width properties..still i am not getting...i am posting my sample code link..could anyone please correct the code...

http://imgpop.com/ImgCode.rar

Thanks,
GOPI

07 August, 2008 14:44  
Blogger IMG POP said...

Thanks for your quick replies...

@inspirone: There should be a way to do this..because i have seen a sample report which have the output of two columns of ID Cards..

@Sachin: This may be the solution..but i am not able to figure it out..i have changed the layout and set the width properties..still i am not getting...i am posting my sample code link..could anyone please correct the code...

http://imgpop.com/ImgCode.rar

Thanks,
GOPI

07 August, 2008 14:44  
Blogger IMG POP said...

Hi sachin,

Every thing works fine..Thanks ya.

08 August, 2008 15:49  
Blogger Hannibal said...

HI,
I want to load the image from the path stored in the web.config.(I am using Crystal reports 2005,C# 2.0). Is it posible to load the image from the path specified in the web.config?

Thank you

01 September, 2008 08:39  
Anonymous Anonymous said...

Really Good,
Mind blowingggggggg.....
good.

09 September, 2008 09:57  
Anonymous Anonymous said...

Well written article.

12 November, 2008 04:14  
Anonymous Anonymous said...

Thanks!
This was very helpful!!!

20 November, 2008 01:57  
Anonymous Anonymous said...

Hi,

You said "drag and drop the Image field in the region where you want it to appear", but I didn't see the Image field in the DataSet. Please help.

Thanks,
-martin

07 December, 2008 21:30  
Anonymous Anonymous said...

This one doesn't support URL-based pictures/images , it only works on local/LAN-network based pictures ... If you used this via "http" request you will only get an error "URI formats are not supported".. ^_^V , but nice one!..

08 January, 2009 07:14  
Anonymous Anonymous said...

This one doesn't support URL-based pictures/images , it only works on local/LAN-network based pictures ... If you used this via "http" request you will only get an error "URI formats are not supported".. ^_^V , but nice one!..

08 January, 2009 07:14  
Blogger எவனோ said...

How can I do same for Windows application. I did all the steps written here, but getting error "the report application server failed"

This happening when I place the image stream field in crystal report
Im not getting any error in the code where I load the image into the field, but getting it when I try to see the report in the viewer

13 January, 2009 03:34  
Anonymous Anonymous said...

Hi, i have win applicaiton developed in VS2008 and crystal report.I already implemented your code and working fine for most of the images. I have thousands of product images which I am displaying on report. In report some of the pages its displaying "Request could not be submitted for background processing [guid].rpt". After tracking I found the image causing the error. That image is jpg and everything looks fine. I just opened that image and change size of the image and its working fine.
But its not possible to change the size of all images as even i dont know what should be size of image that crystal will not cause error lol. Even I cant make Can Grow option in report as image is big in size but i want to fit to particular size.
Any possible solution please.
Thanks

21 February, 2009 09:40  
Anonymous Ramon said...

Thank you, very, very much!

This was what I was looking for.

Ramon

06 April, 2009 14:23  
Anonymous surbhi_india said...

please help me to sort out the problem in crystal report....

I generated this report before but at this time i wnted to make few changes in it but when we run this report it is not displaying the modified formatalthough it's taking the updated data 4m the table but not giving the changes made to it.. kindly helpp

17 April, 2009 15:36  
Anonymous Anonymous said...

Hi,

I am using the code you provided and it works. however, the pictures lose resolution. I think the number of colours decrease somehow.
It is a 200k jpg pictures.

does someone know what could be the reason?

27 May, 2009 19:40  
Anonymous Melinda said...

I am using Visual Studio 2008. I tryed to change my the schema xlm datatype from string to base64binary in notepad. When I opened up VS I got and error "type 'http://www.w3.org/2001/xmlschema:base64binary' is not declared" . I have looked around but not sure what I am missing to make the base64binary work. Thanks

22 August, 2009 00:47  
Anonymous Melinda said...

am using Visual Studio 2008. I tryed to change my the schema xlm datatype from string to base64binary. When I opened up VS I got and error "type 'http://www.w3.org/2001/xmlschema:base64binary' is not declared" . I have looked around but not sure what I am missing to make the base64binary work. Thanks

22 August, 2009 01:17  
Anonymous AL (,, o ,,) said...

Thanks a lot for this post. This is very helpful to me. You saved my day. wheeww. Thanks a lot man... wheeww

08 January, 2010 20:12  
Blogger Curtis Cooper said...

I am using VS2005. I create a field in my XSD called "Image" and set the Default value to "" but when I try to set the DataType, there is NO base64Binary option... I tried typing it in, but got error that it doesn't exist. Can anyone how has done this help?

15 March, 2010 19:54  
Anonymous Anonymous said...

As EVERYONE else has said, Thank you for saving hours of time and being the only one of 25 or so voices I tried on the net for a solution that worked! Cheers.

15 March, 2010 23:52  
Anonymous RainMan said...

Hi,

The code works fine for text data. However, if you include dynamic images in the report, a runtime error occurs: "The request could not be submitted for background processing".

Appreciate your help. Thanks.

15 September, 2010 05:32  
Anonymous topsy kretts said...

I'm totally newbie. I still don't know how to apply your codes in mine.
Could you send me your complete code in zip to my email so that i can get a clear picture of your code, please? I'm badly in need for it.

np_world@yahoo.com

26 October, 2010 19:18  
Blogger Unknown said...

Hi, i want to Load an Excel File dynamically as an OLE Object into a Crystal Report. WIll it work in a similar way?

Thanks

05 January, 2011 21:13  
Blogger Sachin said...

What is mean by that? Do you want to display a link to Excel file?
How do you achieved it with static Excel?

27 April, 2011 21:21  
Blogger Monomongoose said...

Perfect solution. Thank you!

11 May, 2011 22:31  
Anonymous someone said...

i really still cant get it :( i hope you can help me.. when my reportviewer comes up, no image file is shown , no errors, huhuh help me please

29 May, 2011 13:57  
Anonymous Regine said...

i guess you really have a good sample here. hope it could help me too :) im trying to generate library card with pictures.. still a beginner so i cnt understand sum of ur instructions..

xs:element name="Image" type="xs:base64Binary"
-- where will i code this line???

hoping you can help.. thanks in advance. god speed.

14 June, 2011 21:32  
Blogger inspirone said...

This would be in your XSD file and surrounded by the appropriate tags:

14 June, 2011 21:37  
Anonymous Regine said...

Is this applicable for vb.net 08 windows appli???? thank you. :)

14 June, 2011 21:38  
Anonymous Regine said...

Hello again!

im now trying your code.. i just have a question.. can i use tables from different database? 'coz the info on the library card are on ODBC and this one for the image is on XSD file.. is that possible?

THANKS ALOT. :)

17 June, 2011 21:07  
Anonymous chitti76 said...

Hi there, I have VS 2003, and in SQL 2000 the entire image is saved. When i retrieve the dataset, i write to an XML file. Using crystal which is embedded into VS 2003 is it possible to read these images. Please help
thanks

09 September, 2011 22:14  

Post a Comment

<< Home