2010-06-25 34 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