如何使用Crystal Report显示照片?

时间:2010-06-25 16:20:38

标签: crystal-reports-2008

我正在使用SQL Server 2008和C#,我将imagepath存储在我的数据库表(员工详细信息)中,并将此表与Crystal Report通过数据集绑定。 现在,如何将照片与Crystal Report中的每条记录绑定?

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:2)

我决定在知道有多少人让用户将图像上传到他们的水晶报表时遇到了麻烦。举例来说,如果用户想要将其徽标更改或上传到水晶报表上。你怎么能这样做?开发人员如何向用户提供此工具?

我们都知道用户无法定制水晶报告。生成水晶报告后,用户无法根据自己的意愿更改字段。本文将演示用户如何在Crystal报表上上传或更改图像。

这实际上是一个非常简单的例子。即使您对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的表和两列pathimageimage列的类型为Base64 Binary。这是我们的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");

一旦我们准备好架构,我们就可以将它提供给水晶报告。

在现场资源管理器中,我们可以看到Images表和两列路径和图像。将图像列拖到报表上时,您可以看到该字段的类型为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");
    }
}

您可以使用FileOk的{​​{1}}方法进行撰写。您可以使用openFileDialog方法读取字节数组。

BinaryReader.ReadBytes

如果检查这行代码,可以看到我们在表的dr["image"] = BinRed.ReadBytes((int)BinRed.BaseStream.Length); 列中分配了一个字节数组。该字节数组是用户选择的图像。因此,当您将此数据集提供给crystal报表时,报表中的image会将此字节数组转换回图像。

这是您在此计划中需要了解的全部内容。下面我列出了您需要了解的所有重要步骤。

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

<强>结论:

使用此示例,我们可以让用户自定义要上传到报告的图像。在用户想要更改报表上的图像或通过他们自己上传徽标的情况下,这将是一个很好的实现。用户每次想要更改报表上的图像时都不必联系他们的软件开发团队。在跨报表移动字段或添加新字段时,无法在用户端自定义水晶报表。本文仅帮助您了解如何在运行时上载图像。它没有说明如何在运行时自定义晶体报告。我希望你理解这篇文章的概念。

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