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:
You are really so helpful !
Thank you man...:-)
Have a nice year ahead !
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.
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
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
Hi. Thanks a lot for the code, you saved my day!
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...
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.
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.
Of course it works !
I did'nt think to edit the Format Object. I only looked at the field property.
Many thanks,
Ety.
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
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.
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!!!!...
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
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.
Does this approach support gif files or just jpg?
Excelent! ;)
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
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"
Hi,
This was working only with Jpg format. Not supporting for other(gif,bmp etc)
Thanks, helped me!
Its Work Great for VS2003 bundled version, Thanks for your Coding
Sathugudi Software- Saba
Thanks. I try it
Crystal Report XI can solve the problem, but can't preview in VS2005. Who can preview CR XI in VS2005?
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.
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
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?
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.
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.
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
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"...
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.
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.
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...
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.
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
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
Hi sachin,
Every thing works fine..Thanks ya.
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
Really Good,
Mind blowingggggggg.....
good.
Well written article.
Thanks!
This was very helpful!!!
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
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!..
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!..
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
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
Thank you, very, very much!
This was what I was looking for.
Ramon
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
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?
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
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
Thanks a lot for this post. This is very helpful to me. You saved my day. wheeww. Thanks a lot man... wheeww
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?
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.
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.
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
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
What is mean by that? Do you want to display a link to Excel file?
How do you achieved it with static Excel?
Perfect solution. Thank you!
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
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.
This would be in your XSD file and surrounded by the appropriate tags:
Is this applicable for vb.net 08 windows appli???? thank you. :)
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. :)
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
Post a Comment
<< Home