【问题标题】:Displaying Images(from folder) and tables(from database) In the same Crystal Report在同一个 Crystal Report 中显示图像(来自文件夹)和表格(来自数据库)
【发布时间】:2014-06-03 18:32:20
【问题描述】:

我正在使用 VS2010(winform) 和 Access 数据库,在我的水晶报告中,我通过创建 DataSetPatient.xsd 文件并使用以下代码成功地显示了数据库中的表格,现在我想显示来自特定文件夹/文件夹路径的图像进入同一份报告,因为我是水晶报告的新手,请任何人一步一步告诉我我该怎么做

public partial class ViewR : Form
    {        
        DBHandling db=new DBHandling();

        public ViewR()
        {
            InitializeComponent();             
        }

        private void ViewR_Load(object sender, EventArgs e)
        {
            CrystalReportP objRpt;
            // Creating object of our report.
            objRpt = new CrystalReportP();
            DataSetPatient ds = new DataSetPatient(); // .xsd file name
            DataTable dt = DBHandling.GetPatient();
            ds.Tables[0].Merge(dt);
            objRpt.SetDataSource(ds);            
            crystalReportViewer1.ReportSource = objRpt;          

        }        
    }

【问题讨论】:

    标签: c# winforms crystal-reports


    【解决方案1】:

    试试这个方法

    首先:在数据集的数据表中创建一个新的列名(“Image”)并将DataType更改为System.Byte()

    第二个:读取图像文件转换成二进制数组并存储到你的数据表中,

    第三:现在您的数据表有来自数据库的数据和来自路径图像的图像数据,将此数据表分配给数据库,并报告源:

    代码:

    private void ViewR_Load(object sender, EventArgs e)
        {
            CrystalReportP objRpt;
            // Creating object of our report.
            objRpt = new CrystalReportP();
            DataSetPatient ds = new DataSetPatient(); // .xsd file name
            DataTable dt = DBHandling.GetPatient();
            dt = GetImageRow(dt, "YourImageName.Jpg"); 
            ds.Tables[0].Merge(dt);
            objRpt.SetDataSource(ds);            
            crystalReportViewer1.ReportSource = objRpt;          
    
        }       
    

    //通过这个函数你可以把你的图片附加到数据表中

    private DataTable GetImageRow(DataTable dt, string ImageName)
        {
    
            try
            {
    
                FileStream fs;
                BinaryReader br;
    
                if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + ImageName))
                {
                    fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + ImageName, FileMode.Open);
                }
                else
                {
                    // if phot does not exist show the nophoto.jpg file 
                    fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "nophoto.jpg", FileMode.Open);
                }
                // initialise the binary reader from file streamobject 
                br = new BinaryReader(fs);
                // define the byte array of filelength 
                byte[] imgbyte = new byte[fs.Length + 1];
                // read the bytes from the binary reader 
                imgbyte = br.ReadBytes(Convert.ToInt32((fs.Length)));
    
                dt.Rows[0]["Image"] = imgbyte;
    
    
                br.Close();
                // close the binary reader 
                fs.Close();
                // close the file stream 
    
    
    
    
            }
            catch (Exception ex)
            {
                // error handling 
                MessageBox.Show("Missing " + ImageName + "or nophoto.jpg in application folder");
            }
            return dt;
            // Return Datatable After Image Row Insertion
    
        }
    

    注意:首先:这里我将 path 作为 Application Statrup path ,你可以选择任何你想要的路径。 第二:这是运行时图像加载, 第三:这里我也解释了如何将图像转换为字节数组,当你想将图像存储在数据库中时它会很有用

    【讨论】:

      猜你喜欢
      • 2017-05-25
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      相关资源
      最近更新 更多