Error GDI+ when trying to print a report without a reportViewer RRS feed

  • Question

  • Hello,

    In one of my project, I must print some local report without making them visible in a report viewer.
    So I've take the exemple found there :

    I've just transform the function CreateStream to use a MemoryStream instead of a FileStream because, I'm not sure that all my users will have writing rigth on computer they will use when they will run the application.

    So the transformed function is like this

    Code Block

    private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
          Stream stream = new MemoryStream();
          return stream;

    I didn't get any errors on this function but, I have an other part of code that I get from the exemple that made an error
    This is the function where I get an error

    Code Block

    private void PrintPage(object sender, PrintPageEvenArgs ev)
    Metafile pageImage = new Metafile((Stream)m_streams[m_currentPageIndex]);
    ev.Graphics.DrawImage(pageImage, ev.PageBounds);
    ev.HasMorePages = (m_currentPageIndex < m_streams.Count);

    When I run my app, I get this error on the first line : A generic error occurred in GDI+

    So can you help me please?


    Friday, January 18, 2008 9:56 AM

All replies

  • I am having the exact same error. The stream seems to be filled with the correct data, but there is a generic GDI+ error that is spit out, has anyone been able to find an answer to this?

    I also noticed the this error pop up in the output window of VS2008:
    A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll

    I'm using VS2008 report that is a rdlc created in VS2008, and using a custom dataset object (essentially a datatable) as the source. I have also tried using an xml loaded into a dataset that used to work on 2005.
    Monday, July 20, 2009 10:31 PM
  • Actually, I switched from filestream to memory stream like Gaetan Karre was talking about and the problem no longer exists!
    Wednesday, July 22, 2009 3:04 PM
  • I had this problem today and I solved it so I'm posting the solution here becouse it might help someone.

    When the data is written to the stream, the last part of the data is written last, obviously. That means that the cursor that indicates the current possition on the stream is pointed to the end of the stream since that was the last place written to. So before passing the stream to the Metafile constructor you have to set the current position cursor to the start of the stream.

    In this case you could do something like

    foreach (MemoryStream str in m_streams)
       str.Position = 0;
    Thursday, July 23, 2009 6:25 PM
  • I am having the same problem. In one application it is running perfectly fine but in other application it gives me this error:

    A generic error occurred in GDI+.

    at line

    Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);

    m_streams is a private IList<Stream> m_streams.

    What should I do in this situation?
    Saturday, July 29, 2017 10:01 AM