C# Base64 字符串可以转换为图像但不显示在 .rdlc 图像控件中

C# Base64 string can convert to image but not showing in .rdlc Image controls

我使用了一个库来生成条码位图,然后将其转换为 Base64 字符串。这个字符串工作正常,我可以在我找到的所有在线工具中将它转换回图像。但是当我通过 Convert.FromBase64String() 将它放在 RDLC 报告中的图像控件上时,图像根本不显示。

这是my base64 string(_sticker instant中的Base64Barcode)

然后我将此字符串传递到我的 ReportViewer(与我的 .rdlc 报告连接)

private void FormPreview_Load(object sender, EventArgs e)
{
    ReportParameter[] parameters = new ReportParameter[]
    {
        new ReportParameter("palletNo", _sticker.PalletNo)              
        new ReportParameter("qrBase64", _sticker.Base64QR, true),
        new ReportParameter("barcodeBase64", _sticker.Base64Barcode, true)
    };

    this.reportViewer.LocalReport.SetParameters(parameters);
    this.reportViewer.RefreshReport();
}

在 .rdlc 中,我创建了一个图像控件并将字段 "Use this image" 设置为:

=Convert.FromBase64String(Parameters!barcodeBase64.Value)

TextBox 控件工作正常。但是 Image 控件只显示一个白色的小盒子,里面有一个红色的 X。

我在互联网上找到了很多解决方案,但 none 对我来说似乎可行。有什么我错过的吗?

我使用 Visual Studio 2019 Community,.NET Framework 4.5,ZXing 库 0.16.4.0,Microsoft.ReportViewer.WinForms 14.0.0.0

谢谢。

要使用报表设计器在 RDLC 报表中将 Base64 字符串显示为图像:

  • 从工具箱中删除 Image 报表控件。
  • 右键单击图像并选择 图像属性
  • 将图片来源设为Database
  • 将 MIME 类型设置为合适的值,例如 image/bmp
  • 将此字段设置为您拥有的图像参数,例如=Parameters!MyImage.Value。参数类型应该是Text.

然后在 运行 时,分配 Base64 字符串作为参数的值。

注:当图片源设置为External时,参数的值应设置为绝对URL,LocalReport.EnableExternalImages 属性 报告查看器应设置为 true.