我正在做类似的事情,以便在报告上有一个不断变化的徽标,但我使用报告参数来传递值。如果图像是数据的一部分,我看不出为什么这种通用方法不起作用。
从本质上讲,图像是通过两个场传递的。第一个字段是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
".