how to create dynamic Image in Crystal Report using C#

No.of Views7265
Bookmarked0 times
Downloads 
Votes0
By  dotnetfish   On  13 Sep 2010 08:09:13
Tag : Crystal Reports , How to
I found many developers facing the same problem as i do - dynamic include image to crystal report with Visual Studio 2005 . This is how we solve our problem in C# when developing a desktop window application.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

I found many developers facing the same problem as i do - dynamic include image to crystal report with Visual Studio 2005 . This is how we solve our problem in C# when developing a desktop window application.

Implementation Steps

  1. In your xsd, create a column that does not exist in your dataset, example, [LogoImage], and set the datatype to “System.Byte[]”. You might find the datatype option only have “System.Byte”, you can manually type in “[]”.
  2. Add the following code in your form.
  3. private void AddImageColumn(DataTable objDataTable, string strFieldName)
        {try{
                DataColumn objDataColumn = new DataColumn(strFieldName,
                Type.GetType("System.Byte[]"));
                objDataTable.Columns.Add(objDataColumn);
            }catch (Exception ex)
            {//handler}
        }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)
            {//Handler}
        }

  4. After your getting your dataset from database and before bind to the report view, do this
  5. private void BindReport()
        {
            AddImageColumn(myDataTable, "LogoImage");for (int index = 0; index < myDataTable.Rows.Count; index++)
            {if (myDataTable.Rows[index]["LogoLocation"].ToString() != "")
                {if (File.Exists(myDataTable.Rows[index]["LogoLocation"].ToString()))
                    {
                        LoadImage(myDataTable.Rows[index], "LogoImage",
                        myDataTable.Rows[index]["LogoLocation"].ToString());
                    }else{
                        LoadImage(myDataTable.Rows[index], "LogoImage","C:\\NoImage.jpg");
                    }
                }else{
                    LoadImage(myDataTable.Rows[index], "LogoImage", "C:\\NoImage.jpg");
                }
            }
        }

  6. Then in your Crystal Report, just drag this column into the report.

Hope this solve your problem.thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
dotnetfish
Occupation-
Company-
Member Type-Senior
Location-United States
Joined date-05 Sep 2010
Home Page-
Blog Page-dotnetfish.blogspot.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</