2016-12-01 83 views
0

我如何在沒有圖像路徑的水晶報告中顯示圖像。 我有ID(int)和Pic(圖像)的數據庫卡。以編程方式將圖像添加到Crystall報告

我添加數據表到水晶報表dtCard與列= ID類型=字符串 PIC類型=字節()

這裏我的代碼顯示報告:

======== ========================================================

Dim report As New reportCard 

Dim path As String = Application.StartupPath & "\docReportCard.rpt" 

report.Load(path) 

Dim dt As New DataTable("dtCard") 

dt.Columns.Add("Id") 
dt.Columns.Add("pic") 

// dtData return value from database 

Dim row As DataRow = dt.NewRow 
row("Id") = dtData.Rows(0).Item("id") 
row("pic") = DirectCast(dtData.Rows(0).Item("pic"), Byte()) 

dt.Rows.Add(row) 

report.Database.Tables("dtCard").SetDataSource(dt) 
CrystalReportViewer1.ReportSource = report 

======= ===========================================

當我運行代碼,沒有任何錯誤,但圖像無法顯示。 Crystal報告中只有「System.Byte()」。

我該如何解決這個問題? 我想從我的水晶報告中顯示數據庫的Pic。

最好的問候, surbakti

+0

在你的模式t他的圖像列的數據類型應該是

+0

這裏代碼來自我的模式:。但仍然不顯示圖像。任何想法? – Surbakti

+0

如果您在設計時將模式設置爲報表的數據源,則圖像列的數據類型應爲IBlobFieldObject –

回答

0

1種形式和1個CrystalReport創建一個新項目。該表單應該有1個按鈕和1個CrystalReportViewer控件。將此代碼粘貼到窗體的代碼窗口中。

Public Class Form1 
Dim table As New DataTable("test") 
Dim column As DataColumn 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    table = New DataTable("test") 
    With table 
     column = New DataColumn 
     With column 
      .ColumnName = "id" 
      .DataType = GetType(Integer) 
      .AutoIncrement = True 
      .AutoIncrementSeed = 0 
      .AutoIncrementStep = 1 
     End With 
     .Columns.Add(column) 

     column = New DataColumn 
     With column 
      .ColumnName = "pic" 
      .DataType = GetType(Byte())     
     End With 
     .Columns.Add(column) 
    End With 

    ' create XSD file for report datasource 
    ' uncomment this line on first run to create the datasource schema of your crystal report document. 
    ' Once schema is created, point your crystal report document's datasource to its location 
    ' After setting the data source, when you drag the "pic" column, its datatype will be IBlobFieldObject. 
    'table.WriteXmlSchema("E:\TMP\TestReport.XSD") 

    ' uncomment the following on succeeding runs, i.e. after setting the data source in the crystal report file 
    'ShowReport() 
End Sub 






Private Sub ShowReport() 
    Dim row As DataRow 
    row = table.NewRow 
    row("pic") = GetImageData(<PATH YO YOUR IMAGE FILE>) 
    table.Rows.Add(row) 
    table.AcceptChanges() 

    Dim crpt As New crpt 
    crpt.SetDataSource(table) 

    crv.ReportSource = crpt 
End Sub 






Public Function GetImageData(ByVal cFileName As String) As Byte() 
    Dim fs As System.IO.FileStream = _ 
      New System.IO.FileStream(cFileName, _ 
      System.IO.FileMode.Open, System.IO.FileAccess.Read) 
    Dim br As System.IO.BinaryReader = New System.IO.BinaryReader(fs) 
    Return (br.ReadBytes(Convert.ToInt32(br.BaseStream.Length))) 
End Function 
End Class 

名稱的控件如下所示:

按鈕= Button1的 的CrystalReportViewer = CRV

表= Form1中 CrystalReport = crpt

TestReport.XSD包含以下內容:

<?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:MainDataTable="test" msdata:UseCurrentLocale="true"> 
<xs:complexType> 
    <xs:choice minOccurs="0" maxOccurs="unbounded"> 
    <xs:element name="test"> 
     <xs:complexType> 
     <xs:sequence> 
      <xs:element name="id" msdata:AutoIncrement="true" type="xs:int" minOccurs="0" /> 
      <xs:element name="pic" type="xs:base64Binary" minOccurs="0" /> 
     </xs:sequence> 
     </xs:complexType> 
    </xs:element> 
    </xs:choice> 
</xs:complexType> 
</xs:element> 
</xs:schema> 
相關問題