2016-02-23 20 views
2

尋求一些幫助。動態添加多個圖像到vb.net中的水晶報表(WinForms)

我以前使用DataSet和子窗體將單個圖像添加到Crystal Report。

我認爲我能夠複製這個多個圖像。

我所創建的數據集「形象」與名爲IMG1 8行 - > img8

我已經創建了一個基於該數據集的子報表。

在我的VB代碼,我設置了子報表的數據源如下:

初始呼叫:

alertReport.OpenSubreport("AlertImages").SetDataSource(CreateImages(alertID)) 

創建圖像:

Private Function CreateImages(ByVal alertID As Integer) As DataSet 

     Dim data As New DataSet() 

     data.Locale = Globalization.CultureInfo.InvariantCulture 
     data.Tables.Add("Images") 
     data.Tables(0).Columns.Add("img", System.Type.GetType("System.Byte[]")) 

     Try 

      Dim path As String = String.Format("{0}\{1}", HighAlertPath, alertID.ToString()) 

      If (Directory.Exists(path)) Then 

       Dim cnt As Integer = 1 

       For Each fi As FileInfo In New DirectoryInfo(path).GetFiles 

        If (cnt <= 8) Then 

         If (fi.Extension = ".jpg" Or fi.Extension = ".png" Or fi.Extension = ".bmp") Then 

          Dim row As DataRow = GetImageRow(data.Tables(0), fi.FullName) 
          data.Tables(0).Rows.Add(row) 
          cnt += 1 

         End If 

        End If 
       Next 

      End If 

     Catch ex As Exception 
     End Try 

     Return data 

    End Function 

獲取圖像行:

Private Function GetImageRow(ByVal tbl As DataTable, ByVal fileName As String) As DataRow 

    Using fs As New FileStream(fileName, FileMode.Open) 
     Using br As New BinaryReader(fs) 

      Dim row As DataRow = tbl.NewRow() 
      row(0) = br.ReadBytes(CInt(br.BaseStream.Length)) 

      Return row 

     End Using 
    End Using 

End Function 

我c確認文件位置確實具有圖像,並將它們添加到數據表中,但出於某種原因它們不顯示在子報告中。

我需要命名行,因爲它們被添加到數據表,因爲我的報表預計領域IMG1 - > img8

+0

任何人都可以闡明這一個給我的光? –

回答

0

我建議你在運行時創建一個PictureObject,加上它的圖片,然後添加它給報告。

看看這個代碼:

Dim crxReport as CRAXDRT.Report 
Dim objOLE As CRAXDRT.OLEObject 
Dim objPic As StdPicture 

'set all your CRAXDRT report and application objects, recordsets, etc 

Set objPic = LoadPicture("myPath\myPic.bmp") 

'here I am adding a picture object to the first section of the report 
'now spacing here might get weird, you need to play with this and 
'figure out where this object will go 

Set objOLE = crxReport.Sections(1).AddPictureObject("myPath\myPic.bmp",0, 0) 

'Set attributes of the image, we are dynamically creating it 
'again you need to adjust accordingly 

With objOLE 
    .Height = objPic.Height * 500/1000 
    .Width = objPic.Width * 500/ 1000 
    .Left = (crxReport.Sections(1).Width) - objPic.Width 
End With 

Set objOLE = Nothing 
Set objPic = Nothing 

http://www.xtremevbtalk.com/database-and-reporting/109005-inserting-picture-objects-run-time-crystal-report.html