【问题标题】:How to render a Bitmap property in a Reporting RDLC and render a non corrupted pdf file如何在报告 RDLC 中呈现位图属性并呈现未损坏的 pdf 文件
【发布时间】:2023-11-26 17:42:01
【问题描述】:

我必须在我的 rdlc 中渲染一个位图(使用 .Net Reporting),然后以不同的格式(包括 pdf)渲染这个文件。 DataSet 由 SeriesItemModels 对象 List 组成,该对象(列表的每个项目)具有一个定义为 Bitmap 类型的属性! 问题是我面临两个问题: - 似乎位图未在 rdlc 中呈现 - 文件扩展名消失,下载的文件已损坏 这是 Print 方法(我在渲染 pdf 文件的操作中调用的方法)

public class StatisticsStateModels
{       
    public static void Print(List<SeriesItemModels> items, string printFormat)
    {
        ReportViewer rptViewer = new ReportViewer();
        rptViewer.ProcessingMode = ProcessingMode.Local;

        rptViewer.LocalReport.EnableExternalImages = true;
        rptViewer.LocalReport.ReportPath = @HttpContext.Current.Request.PhysicalApplicationPath + "Content/States/Statistics.rdlc";
        rptViewer.LocalReport.DataSources.Add(new ReportDataSource("StatisticsModelsDataSet", StatisticsStateModels.GetStatistics(items)));

        HttpResponse response = HttpContext.Current.Response;
        DateTime now = DateTime.Now;
        string filename = now.Year + "" + now.Month + "" + now.Day + "" + now.Hour + "" + now.Minute + "" + now.Second + "" + now.Millisecond;





        Warning[] warnings;
        string[] streamIds;
        string mimeType = string.Empty;
        string encoding = string.Empty;
        string extension = string.Empty;

        switch (printFormat.ToLower())
        {
            case "pdf":
                byte[] pdfContent = rptViewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

                response.Clear();
                //response.ClearHeaders();
                response.Buffer = true;
                response.ContentType = mimeType;
                response.AddHeader("content-disposition", "attachment; filename=" + filename + "." + extension);
                // response.ContentType = "application/pdf";
                response.BinaryWrite(pdfContent);
                response.Flush(); // send it to the client to download
                response.End();

                break;
            default:
                break;
        }
    }
}

和DataSet方法

public static List<SeriesItemModels> GetStatistics(List<SeriesItemModels> items)
    {
        return items;
    }

最后,你有下面的 SeriesItem 模型

  public class SeriesItemModels
{
    public string name { get; set; }
    public List<object> data { get; set; }
    public Bitmap graph { get; set; }
    public SeriesItemModels() {
        this.name = "";
        this.graph = validBitMap();//be sure: this bitMap is external and valid!
        this.data = new List<object>();
    }
}

PS:validBitMap 生成完美且正确!我试图将它保存在服务器上,它可以工作!但尽管我努力,文件仍然损坏。

你能告诉我如何解决这些问题吗?为什么我的文件损坏了?

谢谢

【问题讨论】:

    标签: asp.net reporting rdlc httpresponse


    【解决方案1】:

    这是我所做的,并且效果很好: 首先,我使用 ImageConverter 将 Bitmap 图属性更改为 byte[] 数组(见下文)

    public class SeriesItemModels{
    public string name { get; set; }
    public List<object> data { get; set; }
    private BitMap _graph;
    public byte[] graph { get{
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(_bitmap, typeof(byte[]));
    }
    set{
        //...intentionaly ignored
     } }
        public SeriesItemModels() {
        this.name = "";
        this.graph = validBitMap();//be sure: this bitMap is external and valid!
        this.data = new List<object>();
       }
      }
    

    并且由于发布请求,文件已损坏!我已将请求更改为获取请求,现在文件正在正确下载(未损坏)

    【讨论】:

      最近更新 更多