Display Dynamic Image in Crystal Report .NET
June 20, 2008 at 8:18 AM | Posted in Free Source Code | 29 CommentsTags: C#.Net, Free Source Code
Saifullah Al Azad
Create Your Badge
–> When file path store in database and need to show image in details section of crystal report
If you have imagepath that store in your database, Crystal Report .NET in Visual Studio 2003/2005 cannot display image file dynamically unless you use dynamic image location feature in Crystal Report XI.
But don’t worry, it can display with some work around for this.
1. You must have imagepath in your database.
2. Create new Dataset/XML Schema (xsd) to use as resource data in creating report. Add an additional field that is not in the table and which is of type base64Binary :
<xs:element name=”image_stream” type=”xs:base64Binary” minOccurs=”0″ />
3. When designing a report drag and drop the “image_stream” field in the region where you want it to appear.
4. Add the following method in you code:
private void AddImageColumn(DataTable objDataTable, string strFieldName)
{
try
{
DataColumn objDataColumn = new DataColumn(strFieldName, Type.GetType(“System.Byte[]”));
objDataTable.Columns.Add(objDataColumn);
}
catch (Exception ex)
{
Response.Write(“<font color=red>”+ex.Message+”</font>”);
}
}
5. And this one to load the image:
private void LoadImage(DataRow objDataRow, string strImageField, string FilePath)
{
try
{
FileStream fs = new FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] Image = new byte[fs.Length];
fs.Read(Image, 0, Convert.ToInt32(fs.Length));
fs.Close();
objDataRow[strImageField] = Image;
}
catch (Exception ex)
{
Response.Write(“<font color=red>”+ex.Message+”</font>”);
}
}
6. Before assigning the dataset to the “SetDataSource” of your report, add the following code:
AddImageColumn(ds.Tables[0], “image_stream”);
for (int index=0; index < ds.Tables[0].Rows.Count; index++)
{
if (ds.Tables[0].Rows[index][“image_path”].ToString() != “”)
{
if(File.Exists(this.Server.MapPath(ds.Tables[0].Rows[index][“image_path”].ToString())))
{
LoadImage(ds.Tables[0].Rows[index], “image_stream”, ds.Tables[0].Rows[index][“image_path”].ToString());
}
else
{
LoadImage(ds.Tables[0].Rows[index], “image_stream”, “C:\NoImage.jpg”);
}
}
else
{
LoadImage(ds.Tables[0].Rows[index], “image_stream”, “C:\NoImage.jpg”);
}
}
7. And finally to display report in crystal report viewer
crDoc.SetDatabaseLogon(UserID, Password, Server, Database);
crDoc.SetDataSource(ds.Tables[0]);
CrystalReportViewer1.ReportSource = crDoc;
–> When file path in your local hard disk and need to show in other section without details section of crystal report
public void SetLogo(CrystalDecisions.CrystalReports.Engine.ReportDocument rpt, system.drawing.image img, string prmName)
{
CrystalDecisions.Shared.ParameterField parameterField;
parameterField = rpt.ParameterFields(prmName);
string f = Application.StartupPath + “Logo.jpg”;
if (IO.File.Exists(f))
IO.File.Delete(f);
parameterField.CurrentValues.AddValue(SetReportLogo(img, f));
}
public string SetReportLogo(Image img, string f)
{
if (!IO.File.Exists(f)) {
Bitmap oBitmap;
oBitmap = new Bitmap(img);
oBitmap.Save(f, Drawing.Imaging.ImageFormat.Jpeg);
oBitmap.Dispose();
}
return f;
}
29 Comments »
RSS feed for comments on this post. TrackBack URI
Leave a reply to sport recipes Cancel reply
Create a free website or blog at WordPress.com.
Entries and comments feeds.
Thank you for your feedback.
I am stucked here “image_path”
if (ds.Tables[0].Rows[index][“image_path”].ToString() != “”)
{
if(File.Exists(this.Server.MapPath(ds.Tables[0].Rows[index][“image_path”].ToString())))
{
LoadImage(ds.Tables[0].Rows[index], “image_stream”, ds.Tables[0].Rows[index][“image_path”].ToString());
}
else
{
LoadImage(ds.Tables[0].Rows[index], “image_stream”, “C:\NoImage.jpg”);
}
}
else
{
LoadImage(ds.Tables[0].Rows[index], “image_stream”, “C:\NoImage.jpg”);
}
In details :
My page: http://localhost:2530/Report/CRT_Student.aspx
I have all image files in D:/Report/image directory
If i save the path as “D:\Report\image\*.jpg” or “D://Report//image//*.jpg” in DB IT creates run time path error (invalid virtual path)
If i save the path as “./Report/image/*.jpg” or “.//Report//image//*.jpg” in DB IT could not find the file so it always goes to else condition and displayes error picture.
Could you please help me out.
Thank you.
Comment by Sheahad— August 16, 2008 #
Hi Sheahad,
Sorry for late.
Ok, i think i guess ur problem. Is that u want to show all image which is in particular directory?
but this function is created for each image for each row in db. i. e-
In student table of ur database, each employee has a single image path(like C:\student\Roll1.jpg) and the image location must be that path which path is saved in ur student table. then u can create another column which can be contained the actual image of each student. But if u save path as “C:\student\*.jpg”, its not posible to generate with this function.
If i m right to catch ur problem then send me ur table column design and ur required report moke up. I must try to solved it.
Thanx
AAA
Comment by AAA— August 18, 2008 #
Hello,
I have a employee table. Where in the image field i save the image path.
So how can i show that image in crystal report.
If you have any sample file then it will be better to understand .
ARIF
Comment by Arif— November 29, 2008 #
Hi,
Can you tell more details to “Add an additional field that is not in the table and which is of type base64Binary : “?
1. Did you mean to edit the xml file to add it?
2. Where to add this line? I always got syntax error.
Many Thanks.
Comment by martin— December 7, 2008 #
Hi,
Can image_stream field be dragged from the dataset into the report, same as other db fields? If not, how can it be displayed on the report?
Thanks again,
-martin
Comment by martin— December 7, 2008 #
Nice Article.
visit my blog too.
(i am a student of software development)
Comment by Zakaria— December 9, 2008 #
Hi,
Thanks for your useful information.
My problem is the same, except that I use the Report object ogf crystal .Net and I load the report and then show it in a Report viewer.
in the report file (rpt) i have put an image and bind the Graphic property to the related field which is included of image path.
it runs successfully in my computer. but when i deploy it to real site, it doesn’t show the picture. for your reference, images are in another path in the network.
would you please help me?? it is so urgent and critical for me.
Cheers,
Ethan
Comment by Ethan— February 9, 2009 #
Hi .. friends..
I am showing 1000 images (25 images per page) by using .nets internal crystal report but i am getting error related to Temp Folder…
Please tell me how much images i can be show by using External crystal report (NOT .net internal crystal report)
Please reply as early as possible
Thanks in advance…
Comment by Atul— February 13, 2009 #
dudes!
common solution is that u need image in byte format to show on the report rather put a column afterwards or add a image in database in byte format and then show it. both ways work the same!
Comment by WAQAR ul Haq— March 10, 2009 #
Just become a member in few minutes. Wanna send you guys a warm hello!
Glad to be here with you!!
[url=http://www.usainstantpayday.com/]bad credit loans[/url]
Comment by Heespisfats— December 16, 2009 #
Can anyone recommend the best Patch Management system for a small IT service company like mine? Does anyone use Kaseya.com or GFI.com? How do they compare to these guys I found recently: N-able N-central security management
? What is your best take in cost vs performance among those three? I need a good advice please… Thanks in advance!
Comment by vofspoma— December 19, 2009 #
I am generating a crystal report in C#.net 2008 . i am connecting to Sql server database 2005
Comment by Dheeraj— January 12, 2010 #
I am generating a crystal report in C#.net 2008 .I have added a typed dataset with all required column . which is not connected to database .it has a column photo of type System.Byte[] . Now i am populating a dataset with a stored proceure which has path for Images. at run time am creating a dataset of type typedDataset (That i added ) . now i have designed it with designer and attached the photo field to report which on dragging converts in picture object .
But when i run my report all data is displaed except Images. I have already checked that Photo Column is having System.Byte[] array in it.
Please Guys help me and tell where i am wrong.
Thank you.
Dheeraj
Dheeraj@arkaaye.com
Comment by Dheeraj— January 12, 2010 #
I am generating a crystal report in C#.net 2008 .I have added a typed dataset with all required column . which is not connected to database .it has a column photo of type System.Byte[] . Now i am populating a dataset with a stored proceure which has path for Images. at run time am creating a dataset of type typedDataset (That i added ) . now i have designed it with designer and attached the photo field to report which on dragging converts in picture object .
But when i run my report all data is displaed except Images. I have already checked that Photo Column is having System.Byte[] array in it.
Please Guys help me and tell where i am wrong.
Thank you.
Dheeraj
Comment by Dheeraj— January 12, 2010 #
nice work..dude..
but is it possible to load images from other url (https://…com)
not from disk..if so
plz sujess me..
Comment by Jeevan— June 21, 2010 #
Yes it possible
Comment by saifullahazad— July 6, 2010 #
Thanks for your useful example. My question is how DataTable accept column with the same name it has in xsd means you add additional column “image_stream” to xsd then in code behind you add column with the same name to DataTable in xsd so I face an exception(This column already belongs to table)
Comment by Basma— October 19, 2010 #
Excellent Job
Comment by willayat— June 22, 2011 #
it’s giving me there id no row at position 0 at this line..
LoadImage(ds.Table1[0], “image_stream”, tb.Rows[index][“image1”].ToString());
help me out of this..
Comment by xyz— July 15, 2012 #
Dear Sir.
I m very hurt fr0m crystal rep0rt. Wel i m using vs 20o8 and cr with it. I made rep0rt they are w0rking fyn. When i am using ole object as pic. Plz tel me how to l0ad pic fr0m db. Db datatype is binary. Bt thr0ug paramer plz tel me.
Comment by Aamir— December 25, 2012 #
Hello there, I found your web site via Google even as looking for a
similar matter, your site got here up, it looks great.
I have bookmarked it in my google bookmarks.
Hi there, simply changed into aware of your blog thru Google, and located that it’s truly informative. I am gonna be careful for brussels. I will be grateful in case you proceed this in future. Lots of folks can be benefited from your writing. Cheers!
Comment by quit smoking— February 20, 2013 #
how about in vb.net visual studio 2010 how to display image in crystal report?
Comment by jariane— March 6, 2013 #
I don’t know if it’s just me or if perhaps everybody else
experiencing issues with your website. It appears as
though some of the text within your content are running off the screen.
Can someone else please comment and let me know if this is happening to them too?
This may be a problem with my web browser because
I’ve had this happen previously. Thanks
Comment by pożyczka pod zastaw działki— April 29, 2013 #
After I initially left a comment I seem to
have clicked on the -Notify me when new comments are added-
checkbox and now whenever a comment is added I recieve four emails with the same comment.
Is there a way you can remove me from that service? Thank you!
Comment by pregnancy lower back pain— May 30, 2013 #
I must thank you for the efforts you’ve put in writing this website. I really hope to view the same high-grade content from you in the future as well. In fact, your creative writing abilities has encouraged me to get my own site now 😉
Comment by flyer printing services— June 17, 2013 #
I have a willing analytical attention to get fine detail and may foresee troubles just before they will occur.
Comment by Anonymous— July 29, 2013 #
One bowling team we worked with ordered specially personalized cookies shaped like bowling pins, with the name and
high score of each league member from that season. Your best opportunity at finding gold that the old time prospectors left behind is in the deserts.
You already know how important owning a fishfinder is for a
succcessful fishing trip, but owning an Eagle is even better.
Comment by sport recipes— September 10, 2013 #
Thanks for your effort for posting “Display Dynamic Image in Crystal
Report .NET | .Net Bangladesh”. I reallymight undoubtedly be coming back for even more browsing
and writing comments in the near future. Thanks a lot,
Roger
Comment by Evelyn— January 22, 2014 #
Hi! Thanks for your useful post!
It works finally! Spent me few days on doing it!
Comment by tony anthony— October 16, 2014 #