首页 > 解决方案 > C# Base64 字符串可以转换为图像但不显示在 .rdlc 图像控件中

问题描述

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

这是我的 base64 字符串(_sticker 即时中的 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 中,我创建了一个图像控件并将“使用此图像”字段设置为:

=Convert.FromBase64String(Parameters!barcodeBase64.Value)

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

我在互联网上找到了很多解决方案,但似乎没有一个对我有用。有什么我错过的吗?

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

谢谢。

标签: c#winformsbase64reportrdlc

解决方案


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

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

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

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


推荐阅读