You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET WebForms中如何在RDLC报表显示SQL Server存储的BASE64图片

RDLC报表无法显示数据库中存储的Base64条码图片问题

背景流程

  1. 通过以下C#代码将资产编码转换为Base64格式的条码图片字符串:
private string GenerateBarcode(string BarCode)
{
    string generatebarcode = BarCode;

    GeneratedBarcode barcode = BarcodeWriter.CreateBarcode(generatebarcode, BarcodeWriterEncoding.Code128);

    barcode.ResizeTo(400, 120);
    barcode.SetMargins(10);
    barcode.ChangeBarCodeColor(Color.Black);

    Image MyBarCodeImage = barcode.Image;

    var myArray = (byte[])new ImageConverter().ConvertTo(MyBarCodeImage, typeof(byte[]));
    string temp_inBase64 = Convert.ToBase64String(myArray);

    return temp_inBase64;
}
  1. 将生成的Base64字符串存入SQL Server数据库,再通过数据集获取并绑定到报表表格。

RDLC报表已执行的配置步骤

  • 从工具箱拖入Image报表控件
  • 右键点击图片选择“图片属性”
  • 将图片源设置为“数据库”
  • 设置MIME类型为image/bmp
  • 指定字段表达式为=Fields!Code.Value,参数类型设为文本

环境信息

  • Visual Studio 2019 Community
  • .NET Framework 4.7.2
  • Microsoft.ReportViewer.WebForms 15.0.0.0

可能的遗漏点及解决方法

1. 图片源要求的是字节数组而非Base64字符串

RDLC的“数据库”图片源仅支持绑定字节数组,你当前存储的是Base64字符串,直接绑定无法被解析。解决方式二选一:

  • 方案一:修改存储逻辑,直接将条码的字节数组(myArray)存入数据库,报表直接绑定该字节数字段即可。
  • 方案二:在报表的图片字段表达式中,将Base64字符串转换为字节数组,表达式改为:
    =System.Convert.FromBase64String(Fields!Code.Value)
    
    注意:需确保Fields!Code.Value是完整无损坏的Base64字符串,无多余空格或截断。

2. 确认MIME类型与实际图片格式匹配

ImageConverter转换出的图片格式不一定是BMP,可在生成Base64前通过MyBarCodeImage.RawFormat确认实际格式,对应正确的MIME类型:

  • PNG对应image/png
  • JPEG对应image/jpeg
  • BMP对应image/bmp

3. 检查数据集字段是否完整

确保数据集返回的Code字段是字符串类型,且未被截断。如果数据库中存储的Base64字符串过长,可能导致字段被截断,无法完整解析为图片。

4. 验证Base64字符串有效性

将数据库中的Base64字符串复制出来,通过本地代码转换为图片,确认是否能正常还原,排除存储过程中出现的字符串损坏问题。

5. 检查ReportViewer控件配置

确保Web.config中已正确配置ReportViewer的处理程序:

<system.web>
  <httpHandlers>
    <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" validate="false" />
  </httpHandlers>
</system.web>
<system.webServer>
  <handlers>
    <add name="ReportViewerWebControlHandler" preCondition="integratedMode" verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
  </handlers>
</system.webServer>

同时确认报表使用的是本地模式(LocalReport),而非服务器模式。


内容的提问来源于stack exchange,提问作者M A K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 17:05:51