外部图像未加载到RDLC报告中:Windows窗体应用程序VB.net

问题描述

我正在使用vb.net和Microsoft sql服务器作为后端的Windows窗体应用程序上工作。至于报告,我使用的是Microsoft的rdlc,直到我对这个明显的问题感到震惊为止,这对我来说还是令人满意的。

因此,在报告中,我使用的是外部图像,这些图像是通过本地文件路径加载的,并作为参数传递。我从数据库中检索这些路径,并将其传递给报告。这种方法我有效了很长时间,直到停止工作为止。现在的情况是,外部图像未加载到报表中(显示红叉而不是图像)。据我所知,此问题与PC上的权限有关,因为以前相同的代码和配置对我有用,但我无法解决。我到处搜寻无止境,肯定会寻求帮助。

我的报告代码为:

  Try
            With Me.ReportViewer1.LocalReport
                .DataSources.Clear()
                .ReportPath = Application.StartupPath & "\RptArticle.rdlc"
                
                .EnableExternalImages = True


                Dim imagepathstring As String = System.IO.Path.Combine(folder & "\ArticlePics",imagepath)
                Dim imgparameter As New ReportParameter
                imgparameter = New ReportParameter("ImagePath","file://" & imagepathstring,True)
                .SetParameters(imgparameter)


                Dim barpathstring As String = System.IO.Path.Combine(folder & "\BarCode",barpath)
                Dim barcode As New ReportParameter("Barcode","file://" & barpathstring,True)
                .SetParameters(barcode)

                .DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("DSArticleAccessory",Accdt))

                .DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("DSArticleSize",AccSdt))

                .DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("DSArticleColour",AccCdt))

                .DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("DSArticleColour",AccCdt))

                .DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("DSArticle",dt))



            End With

            Me.ReportViewer1.ZoomMode = ZoomMode.PageWidth

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        Me.ReportViewer1.RefreshReport()

当我加载报告时,输出为:

警告:如果将报表发布到没有UnattendedExecutionAccount的报表服务器,或者未启用目标图像的匿名访问,则不会显示具有外部URL引用的图像。 (rsWarningFetchingExternalImages) 警告:图片“ Image1”的MIMEType属性的值为“ application / octet-stream”,这不是有效的MIMEType。 (rsInvalidMIMEType) 警告:图像“ Image1”的ImageData属性的值为“”,这不是有效的ImageData。 (rsInvalidExternalImageProperty) 警告:图片“ Image2”的MIMEType属性的值为“ application / octet-stream”,不是有效的MIMEType。 (rsInvalidMIMEType) 警告:图像“ Image2”的ImageData属性值为“”,这不是有效的ImageData。 (rsInvalidExternalImageProperty) 线程0x3948已退出代码为0(0x0)。

解决方法

除了使用外部图像,您还可以将图像作为字符串作为参数传递给报表。

首先使用以下方法将图像转换为Base64字符串:

Public Function ImageToBase64(ByVal image As Image,ByVal format As System.Drawing.Imaging.ImageFormat) As String
   Dim base64String As String = ""
   Using ms As New System.IO.MemoryStream()
      image.Save(ms,format)
      Dim imageBytes As Byte() = ms.ToArray()
      base64String = Convert.ToBase64String(imageBytes)
   End Using
   Return base64String
End Function

所以这个:

Dim imagepathstring As String = System.IO.Path.Combine(folder & "\ArticlePics",imagepath)
Dim imgparameter As New ReportParameter
imgparameter = New ReportParameter("ImagePath","file://" & imagepathstring,True)
.SetParameters(imgparameter)


Dim barpathstring As String = System.IO.Path.Combine(folder & "\BarCode",barpath)
Dim barcode As New ReportParameter("Barcode","file://" & barpathstring,True)
.SetParameters(barcode)

将是这样的:

Dim imagepathstring As String = System.IO.Path.Combine(folder & "\ArticlePics",ImageToBase64(Image.FromFile(imagepathstring),<THE IMAGE FORMAT OF YOUR IMAGE>))
.SetParameters(imgparameter)


Dim barpathstring As String = System.IO.Path.Combine(folder & "\BarCode",ImageToBase64(Image.FromFile(barpathstring),<THE IMAGE FORMAT OF YOUR IMAGE>))
.SetParameters(barcode)

然后,在您的报告中为以下项设置以下内容:

ImagePath

MIMEType = select the correct MIME type from the dropdown list
Source = Database
Value = <Expression>

并在“表达式”窗口中:

=System.Convert.FromBase64String(Parameters!ImagePath.Value)

Barcode

MIMEType = select the correct MIME type from the dropdown list
Source = Database
Value = <Expression>

=System.Convert.FromBase64String(Parameters!Barcode.Value)