Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Following is code for image display in a crystal report. This is not working.

Please reply on this.
C#
SQLQuery4 = " Select ImageS from tblForm14 where  " + query5;
                    SQLQuery4 = SQLQuery4.Substring(0, SQLQuery4.Length - 4);
                    SqlCommand cmd = new SqlCommand(SQLQuery4, Cn3);
                    string ss = (string)cmd.ExecuteScalar();
                    BoxObject Box1;
                   
                    PictureObject pct;
                    pct = cryRtp.ReportDefinition.Sections["Section3"].ReportObjects["Picture1"]as PictureObject; 
                 
                    //rpt.sections("Section1").reportobjects("picture1")=loadpicture("c:\test.bmp")

                    //pictureBox1.ImageLocation = @"D:/Rider/" + dt.Rows[0][17].ToString();
                    //Box1 = (BoxObject)cryRtp.ReportDefinition.Sections["Section3"].ReportObjects["Box1"];
                    DataTable dt = new DataTable();
                    DataRow drow;
                    dt.Columns.Add("Image", System.Type.GetType("System.Byte[]"));
                    drow = dt.NewRow();
                    FileStream fs;
                    BinaryReader br;
                    if (File.Exists(@"D:/Rider/" + ss))
                    {
               
                        fs = new FileStream(@"D:/Rider/" + ss, FileMode.Open); 
                    }
                    else
                    {
                        fs = new FileStream(@"D:/Rider/" + "NoPhoto.jpg", FileMode.Open);
                    }
                    br = new BinaryReader(fs);
                    byte[] imgbyte = new byte[fs.Length + 1]; 
                    imgbyte = br.ReadBytes(Convert.ToInt32((fs.Length)));
                    drow[0] = imgbyte;
                    
                    dt.Rows.Add(drow);
                    br.Close();
                    fs.Close();
                  
                    cryRtp.SetDataSource(dt);
                     crystalReportViewer1.ReportSource = cryRtp;
                
                 cryRtp.SetDataSource(D8);
                 cryRtp.SetDataSource(D9);
                 cryRtp.SetDataSource(d10);
                    crystalReportViewer1.ReportSource = cryRtp;

}
Posted
Updated 27-Apr-11 2:32am
v2
Comments
walterhevedeich 27-Apr-11 10:31am    
Any errors so far?
Nagy Vilmos 27-Apr-11 11:23am    
What is not working?
- code doesn't build?
- builds but won't run?
- runs but gives unexpected results?

fallow these steps to view an image in Chrystal report.

1.Insert a picture box in crystal report at design time.
2.In data set a column name Photo
3.Sett this column properties data type to System.Byte[] which have blank at design time.its only for add link at design time from field explorer.
4.In cs file add also a column named Photo with data type System.Byte[] which have fill the data set which have bind to your Chrystal report.

ds.Tables[0].Columns.Add("Photo", System.Type.GetType("System.Byte[]"));

                FileStream fs = new FileStream(PHotoPath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                byte[] Image = new byte[fs.Length];
                fs.Read(Image, 0, Convert.ToInt32(fs.Length));
                fs.Close();
                ds.Tables[0].Rows[0]["Photo"] = Image;


5.Finally set your data sources u do usually.

Logic behind is that we add a data set column with our record.
 
Share this answer
 
v2
Comments
RaviRanjanKr 28-Apr-11 5:08am    
Always wrap your code in "pre" tags :)
Hello are you store image in table if yes then add a data table from project - add new item - data - accept by default dataset1.xsd and Right click on datatable add collumn having name image right click on image column and click on property of column change the datatype System.Byte[] then add your add a crystal report Click on accept default setting when you choose data Click on Project Data - ADO.net DataSet Select the DataTable which you add.

Drage the image column add by you in datatable, because the column is not display in datatable1 at the time you add crystal report click on next button - finish and drag the image column from field explorer - datatable1



bind the crystal report on your button click and write the below code
private void button1_Click(object sender, EventArgs e)
       {
           CrystalReport1 crRpt = new CrystalReport1();
           SqlDataAdapter adp = new SqlDataAdapter("select picture as image from Table1", con);
           DataSet1 ds = new DataSet1();
           adp.Fill(ds, "Table1");
           crRpt.SetDataSource(ds.Tables[1]);
           crystalReportViewer1.ReportSource = crRpt;
           crystalReportViewer1.Refresh();
       }
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900