如何在cristal报表查看器中显示blob图像 [英] How to show a blob image in cristal report viewer
问题描述
我在visual studio 2012下使用VB.NET。
我有一个包含数据网格视图,图片和按钮打印的表单。
我创建了cristal报表查看器来打印datagridview和图片的内容,为此,我添加了一个数据表,我添加了datagridview列的字段和Image的字段(Image字段类型是字节())。
问题是所有datagridview字段都显示但图像没有显示。
我有什么试过:
当我点击Print按钮时,datagridview内容会显示在Cristal Report Viewer中,但是图片没有显示(我已经拖了cristal报表查看器的数据表中的Image字段与其他字段一样。)
我尝试了另一种方法,即在Cristal Report View>>中创建一个关键点。右Clic>>格式对象>>图片>> X2>>数据表中图像字段上的双clic。同样使用这种方法我得到错误blob字段不能在公式中使用。
这是按钮的代码PRINT:
Dim dt 作为 新 DataTable
使用 dt
.Columns.Add( code)
.Columns.Add( productnom)
.Columns.Add( category)
.Columns。添加( supplier)
.Columns.Add( qte)
.Columns.Add( < span class =code-string> Image, GetType (字节()))
结束 使用
pathUrl = Application.StartupPath& \ logo.png
Dim img As Image = Image.FromFile(pathUrl)
Dim dr As DataRow = dt.NewRow()
dr( 图像)= imageToByteArray(img)' 函数imageToByteArray已创建
dt.Rows.Add(dr)
对于 每个 dgr As DataGridViewRow 在 我。 Datagridview1.Rows
dt.Rows.Add(dgr.Cells( 0 )。值,dgr.Cells( 1 )。值,dgr.Cells( 2 )。值,dgr.Cells( 3 )。值,dgr.Cells( 4 )。值)
下一步
Dim reportDocument 作为 CrystalDecisions.CrystalReports.Engine.ReportDocument
reportDocument = 新 CrystalReport1
reportDocument.SetDataSource(dt)
FormPrint.CrystalReportViewer1.ReportSource = reportDocument
FormPrint.ShowDialog()
FormPrint.Dispose()
和代码function imageToByteArray是:
公共 Function imageToByteArray(imageIn As System.Drawing.Image) As 字节()
Dim ms As 新 MemoryStream()
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif)
返回 ms.ToArray()
结束 功能
Plz,如果有人可以帮助我的话。谢谢
我通过改变PNG中函数imageToByteArray()中的ImageFormat而不是GIF来解决问题因为cristal报告不接受GIF格式。
更新的功能是:
公共 函数 imageToByteArray(imageIn As System.Drawing.Image)作为 字节()
Dim ms As New MemoryStream()
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.PNG)
返回 ms.ToArray()
结束 功能跨度>
Hi,
I use VB.NET under visual studio 2012.
I have a form that contain a datagridview, a picture and a button print.
I created the cristal report viewer to print the content of datagridview and the picture, for that, I added a datatable in witch I added fields for the datagridview columns and a field for the Image ( the Image field type is bytes()).
The problem is that all datagridview fields are displayed but the image is not displayed.
What I have tried:
When I click on the button Print, the datagridview content is shown in the Cristal Report Viewer but the image is not shown ( I already drag the Image field in the datatable to the cristal report viewer as I did with the other fields ).
I tried another method, which is to create a pivcture in the Cristal Report View >> Right Clic >> Format Object >> Picture >> X2 >> double clic on the image field in the datatable . Also with this method I got error " a blob field can not be used in a formula".
This is the code of the button PRINT :
Dim dt As New DataTable
With dt
.Columns.Add("code")
.Columns.Add("productnom")
.Columns.Add("category")
.Columns.Add("supplier")
.Columns.Add("qte")
.Columns.Add("Image", GetType(Byte()))
End With
pathUrl = Application.StartupPath & "\logo.png"
Dim img As Image = Image.FromFile(pathUrl)
Dim dr As DataRow = dt.NewRow()
dr("Image") = imageToByteArray(img) ' The function imageToByteArray is already created
dt.Rows.Add(dr)
For Each dgr As DataGridViewRow In Me.Datagridview1.Rows
dt.Rows.Add(dgr.Cells(0).Value, dgr.Cells(1).Value, dgr.Cells(2).Value, dgr.Cells(3).Value, dgr.Cells(4).Value)
Next
Dim reportDocument As CrystalDecisions.CrystalReports.Engine.ReportDocument
reportDocument = New CrystalReport1
reportDocument.SetDataSource(dt)
FormPrint.CrystalReportViewer1.ReportSource = reportDocument
FormPrint.ShowDialog()
FormPrint.Dispose()
And the code of the function imageToByteArray is :
Public Function imageToByteArray(imageIn As System.Drawing.Image) As Byte()
Dim ms As New MemoryStream()
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif)
Return ms.ToArray()
End Function
Plz, if someone could help me . Thanks
I resolved the problem by changin the ImageFormat in the functionimageToByteArray ()in PNG instead of GIF because cristal reports don't accept the GIF format.
the updated function is :
Public Function imageToByteArray(imageIn As System.Drawing.Image) As Byte() Dim ms As New MemoryStream() imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.PNG) Return ms.ToArray() End Function
这篇关于如何在cristal报表查看器中显示blob图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!