views:

970

answers:

2

Ok, so i am working on exporting my SSRS 2008 Reports to an image. What I would like to do is Export each individual page as an image. From my code, I can only get it to export the first page of the report. Any help would greatly be appreciated.

    Dim warnings As Microsoft.Reporting.WebForms.Warning()
    Dim streamids As String()
    Dim mimeType, encoding, extension As String

    Dim deviceInfo As XElement = _
      <DeviceInfo>
          <OutputFormat>JPEG</OutputFormat>
      </DeviceInfo>

    Report.ServerReport.SetParameters(Parameters)
    Dim bytes As Byte() = Report.ServerReport.Render("Image", deviceInfo.ToString(), mimeType, encoding, extension, streamids, warnings)

    Dim FileStream As New MemoryStream(bytes)
    Dim ReportImage As New System.Drawing.Bitmap(FileStream)

    ReportImage.Save(Server.MapPath("/Testing.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg)
+4  A: 

Hi, in one of my projects I use the following code to obtain one stream per page. Unfortunately I don't use VB.NET, but you should be able to translate this from C# to VB. Note: This works on SSRS2005 - I'm not sure it also works for SSRS2008! Also, I'm using the code to directly print the report without having to use the report viewer, so I'm creating an EMF device info - you might have to change this.

This base code was found somewhere on the Web after Googling for hours - I'd like to credit the author, but I didn't bookmark the link - sorry.

CultureInfo us = new CultureInfo("en-US");
string deviceInfo = String.Format(
      "<DeviceInfo>" +
      "  <OutputFormat>EMF</OutputFormat>" +
      "  <PageWidth>{0}cm</PageWidth>" +
      "  <PageHeight>{1}cm</PageHeight>" +
      "  <MarginTop>{2}cm</MarginTop>" +
      "  <MarginLeft>{3}cm</MarginLeft>" +
      "  <MarginRight>{4}cm</MarginRight>" +
      "  <MarginBottom>{5}cm</MarginBottom>" +
      "</DeviceInfo>",
      Math.Round(m_pageSize.Width, 2).ToString(us),
      Math.Round(m_pageSize.Height, 2).ToString(us),
      Math.Round(m_marginTop, 2).ToString(us),
      Math.Round(m_marginLeft, 2).ToString(us),
      Math.Round(m_marginRight, 2).ToString(us),
      Math.Round(m_marginBottom, 2).ToString(us));

m_reportStreams = new List<Stream>();
try
{
    // Tell SSRS to store one stream per page on server
    NameValueCollection urlAccessParameters = new NameValueCollection();
    urlAccessParameters.Add("rs:PersistStreams", "True");

    // Render first page
    Stream s = viewer.ServerReport.Render("IMAGE", deviceInfo, urlAccessParameters, out mime, out extension);
    m_reportStreams.Add(s);

    // Loop to get other streams
    urlAccessParameters.Remove("rs:PersistStreams");
    urlAccessParameters.Add("rs:GetNextStream", "True");
    do
    {
        s = viewer.ServerReport.Render("IMAGE", deviceInfo, urlAccessParameters, out mime, out extension);
        if (s.Length != 0) m_reportStreams.Add(s);
    }
    while (s.Length > 0);

    // Now there's one stream per page - do stuff with it
}
finally
{
    foreach (Stream s in m_reportStreams)
    {
        s.Close();
        s.Dispose();
    }
    m_reportStreams = null;
}

EDIT
Forgot to mention that viewer is a programmatically created instance of the ReportViewer control initialized to render the report that you're trying to print/save.

Thorsten Dittmar
I tried this and I am getting an error: Execution '<GUID>' cannot be found
Russ Bradberry
A quick Google showed the following URL that might help you. Others are available too when you google your error message:http://social.msdn.microsoft.com/forums/en-US/sqlreportingservices/thread/0ab399df-56b3-4ac8-a703-4001e7a5fb18
Thorsten Dittmar
I have, and I have tried just about every recommended solution offered. I'll accept your answer as correct then post a new question with my problem.
Russ Bradberry
A: 

Thank you !!! it is just what I was looking for.

Misael