2010-06-25 31 views
0

我想要显示的照片与水晶报表我想,以显示与水晶报表照片

我使用SQLSERVER 2008和C#,我存储的ImagePath在我databasetable(员工详细信息) 和我结合这个表晶报告通过数据集, 现在我怎么可以绑定照片没有每个记录在水晶报告

请任何身体帮助我,我急需它。

回答

2

我决定意识到有多少人有让用户上传图片到自己的水晶报表的麻烦后,这篇文章写的。举例来说,如果用户想要更改或上传他们的标志到水晶报告上。你怎么能这样做?开发人员可以如何为用户提供这种设施?

我们都知道用户不能定制水晶报告太多。一旦生成水晶报告,用户就没有太多的选择来根据需要更改字段。本文将演示用户如何上载或更改水晶报表上的图像。

这实际上是一个非常简单的例子。即使您有关于Crystal报告的新手知识,您也可以了解其工作原理。我正在使用Crystal报告11进行此演示。

要从头开始创建一个新的Windows应用程序并添加一个水晶报表查看器和一个浏览按钮。

此浏览按钮将允许用户添加图像到报告。将水晶报告添加到您的项目中。

报告架构:

我们都知道,我们需要提供的水晶报表的模式。以下是我们要为报告提供的报告模式的XML视图。

<?xml version="1.0" standalone="yes"?> 
     <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas- 
     microsoft-com:xml-msdata"> 
       <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:Locale="en-AU"> 
        <xs:complexType> 
         <xs:choice maxOccurs="unbounded"> 
          <xs:element name="Images"> 
           <xs:complexType> 
            <xs:sequence> 
             <xs:element name="path" type="xs:string" minOccurs="0" /> 
             <xs:element name="image" type="xs:base64Binary" minOccurs="0" /> 
            </xs:sequence> 
           </xs:complexType> 
          </xs:element> 
         </xs:choice> 
        </xs:complexType> 
       </xs:element> 
     </xs:schema> 

BLOB字段:

为图像添加到水晶报表(考虑像场是来自数据库),我们需要在架构BLOB字段。当你想在数据库中存储图像,文档或不同的自定义类型时,你可以使用BLOB字段。 BLOB代表二进制大对象。

<xs:element name="path" type="xs:string" minOccurs="0" /> 
<xs:element name="image" type="xs:base64Binary" minOccurs="0" /> 

如果你检查这个模式,我们可以看到,我们有一个名为Images两列pathimage表。 image列是类型Base64二进制。这是我们的BLOB字段。我们要做的是在用户选择要上传的图像时,我们将该图像作为字节流存储在BLOB字段中,然后将该数据集提供给报告。

CreateTable()方法将说明如何在程序中生成该模式。

生成报表的架构:

还有其他的方法,你可以创建模式,但是这会给你关于列字段一个清晰的概念。

创建表:

private void CreateTable() 
{ 
     //create a new data set. 
     this.DsImages = new DataSet(); 

     //create a new table with two columns and add the table to the dataset 
     DataTable ImageTable = new DataTable("Images"); 

     //in here the "path" column is not really needed. Image column is just enough. 
     ImageTable.Columns.Add(new DataColumn("path",typeof(string))); 

     //Important note 
     //Note the type of the image column. You want to give this column as a blob field to the crystal report. 
     //therefore define the column type as System.Byte[] 
     ImageTable.Columns.Add(new DataColumn("image",typeof(System.Byte[]))); 
     this.DsImages.Tables.Add(ImageTable); 
} 

如果您发现image列中,您可以看到,该列的类型是System.Byte[]。这非常简单。只需创建一个包含两列的表格。然后将其添加到数据集。现在你可以使用这条线创建模式:

this.DsImages.WriteXmlSchema(@"c:\temp\ImagesSchem a.xsd"); 

一旦我们有了现成的模式,我们可以把它提供给水晶报表。

在字段资源管理器中,我们可以看到图像表和两列路径和图像。将图像列拖到报告上时,您可以看到该字段的类型为IBlobFieldObject。这些字段将读取一串字节并将其转换回图像。所以我们的任务已经完成了。下面的代码显示了它如何将图像保存为BLOB字段中的字节流。

private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e) 
{ 
    try 
    { 
     //get the image file into a stream reader. 
     FileStream FilStr = new FileStream(this.openFileDialog1.FileName, FileMode.Open); 
     BinaryReader BinRed = new BinaryReader(FilStr); 

     //Adding the values to the columns 
     // adding the image path to the path column 
     DataRow dr = this.DsImages.Tables["images"].NewRow(); 
     dr["path"] = this.openFileDialog1.FileName; 

     //Important: 
     // Here you use ReadBytes method to add a byte array of the image stream. 
     //so the image column will hold a byte array. 
     dr["image"] = BinRed.ReadBytes((int)BinRed.BaseStream.Length); 
     this.DsImages.Tables["images"].Rows.Add(dr); 
     FilStr.Close(); 
     BinRed.Close(); 

     //create the report object 
     DynamicImageExample DyImg = new DynamicImageExample(); 

     // feed the dataset to the report. 
     DyImg.SetDataSource(this.DsImages); 
     this.crystalReportViewer1.ReportSource = DyImg; 
    } 
    catch(Exception er) 
    { 
     MessageBox.Show(er.Message,"Error"); 
    } 
} 

您在openFileDialogFileOk方法写这个。您使用BinaryReader.ReadBytes方法读取字节数组。

dr["image"] = BinRed.ReadBytes((int)BinRed.BaseStream.Length); 

如果你检查这行代码,你可以看到,我们在表中的列image分配的字节数组。这个字节数组是用户选择的图像。因此,当您将此数据集提供给水晶报表时,报表中的IBlobFieldObject会将此字节数组转换回图像。

这就是你需要在这个程序中理解的全部内容。下面列出了您需要了解的所有重要步骤。

  • 用户选择图像。
  • 我们将图像作为字节流保存在blob字段中。
  • 将数据集提供给包含BLOB字段的报告。
  • 报告会将字节流转换回图像并显示出来。

结论:

使用这个例子中,我们可以给用户定制自己想要上传到报告中的图像。如果用户想要更改报告中的图像或自行上传他们的徽标,情况会很好。用户每次想要更改报告中的图像时都不必联系他们的软件开发团队。在报表中移动字段或添加新字段时,无法在用户端自定义水晶报表。本文仅帮助您了解如何在运行时上传图像。它不会告诉您如何在运行时自定义水晶报表。我希望你能理解这篇文章的概念。

只需下载该程序并尝试。确保你有Crystal Reports 11.我在这里写的代码是基本的。你不应该有任何理解这个问题。 original source