English 中文(简体)
如何将PNG图像(作为内存流)呈现到.NET ReportViewer报表表面上
原标题:
  • 时间:2008-08-31 08:42:17
  •  标签:

我有一个动态创建的图像,我正在将其保存到流中,以便在ReportViewer表面上显示它。

设置:

  • Windows Client application (not WebForms)
  • Report datasource is an object datasource, with a dynamically generated stream as a property (CustomImage)
  • Report.EnableExternalImages = true
  • Image.Source = Database
  • Image.MIMEType = image/png
  • Image.Value = =Fields!CustomImage.Value

这不起作用,但没有报告任何错误,只是在报告表面显示了一个空的图像图标。所有其他字段都显示正确。

有人有这个场景的工作代码示例吗?

最佳回答

我正在做类似的事情,以便在报告上有一个不断变化的徽标,但我使用报告参数来传递值。如果图像是数据的一部分,我看不出为什么这种通用方法不起作用。

从本质上讲,图像是通过两个场传递的。第一个字段是MIME类型值,第二个字段是包含图像内容的Base64编码字符串。

步骤1:将图像转换为Base64编码。(我们的代码总是将<code>ImageFormat.Png</code>传递到此方法,以使MIME类型变得简单)

private static string ConvertImageToBase64(Image image, ImageFormat format)
{
    byte[] imageArray;

    using (System.IO.MemoryStream imageStream = new System.IO.MemoryStream())
    {
        image.Save(imageStream, format);
        imageArray = new byte[imageStream.Length];
        imageStream.Seek(0, System.IO.SeekOrigin.Begin);
        imageStream.Read(imageArray, 0, imageStream.Length);
    }

    return Convert.ToBase64String(imageArray);
}

步骤2:将图像和MIME类型传递给报告

reportParams[0] = new ReportParameter("ReportLogo", base64Logo);
reportParams[1] = new ReportParameter("ReportLogoMimeType", "image/png");

_reportViewer.LocalReport.SetParameters(reportParams);

步骤3:在报告中,在图像上设置以下财产(不带引号):

  • MIMEType: "=Parameters!ReportLogoMimeType.Value"
  • Value: "=System.Convert.FromBase64String(Parameters!ReportLogo.Value)"
  • UPDATE: As Gerardo s says below, the Image Source must be set to Database

Trap for young players: Often the images will look horrible and like they ve been scaled even though you re passing in an image which seems to be the "right size". This is because the reports are rendered for print (300 dpi) and not the screen (usually 72 or 92 dpi). The fix is to send in an image about 3 times too big, set it s correct size in the report and change the "Sizing" property on the image to "FitProportional".

问题回答

暂无回答




相关问题
热门标签