views:

1650

answers:

1

I have a dynamically created image that I am saving to a stream so that I can display it on a ReportViewer surface.

Setup:

  • 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

This is not working, but is not reporting any errors, just showing an empty image icon on the report surface. All other fields are displaying correctly.

Does anyone have a working code sample of this scenario?

+6  A: 

I am doing something similar in order to have a changing logo on reports however I utilise report parameters to pass the value. I don't see any reason why this general method wouldn't work if the images were part of the data.

Essentially the images are passed over two fields. The first field is the MIME Type value and the second field is a Base64 encoded string containing the image content.

Step 1: Convert your image to Base64 encoding. (Our code always passes ImageFormat.Png to this method to make the MIME Type easy)

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);
}

Step 2: Pass the image and MIME Type to the report.

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

_reportViewer.LocalReport.SetParameters(reportParams);

Step 3: In the report set the following properties on the image (without the quotes):

  • MIMEType: "=Parameters!ReportLogoMimeType.Value"
  • Value: "=System.Convert.FromBase64String(Parameters!ReportLogo.Value)"

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".

Aydsman
Thnx a lot, t helped me very much!
Martijn