Generic Error occured in GDI+

DPavelka

Member
Joined
Mar 1, 2008
Messages
6
Programming Experience
10+
I am trying to print a report generated by the built in report designeer. I get a message saying it is printing page 1 (but nothing happens) then i get the message "a Generic Error occured in GDI+". Does anyone know how to get more detailed information on this error?
 
GDI+ is notorious for giving vague errors. The majority of them that you'll see from it are either the generic error you received or one saying that an out of memory exception occurred, even if you have plenty of memory left.

If you post the part of your code that is erroring, we can look through it and try to figure out whats going wrong.
 
Code cause GDI+ error

I have a report (InspectionReport.RDLC) that i can view with viewer but
I get the GDI+ error while trying to print a existing report directly to the printer. The call to print is;
VB.NET:
Dim Report As Microsoft.Reporting.WinForms.LocalReport = New Microsoft.Reporting.WinForms.LocalReport()

Report.ReportPath = "C:\InspectionReport.rdlc"
PrintReport = New Reporting

Me.Inspection_ReportTableAdapter.Fill(Me.InspectionReportDS.Inspection_Report)
Report.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource _
               ("InspectionReportDS_Inspection_Report", Me.InspectionReportDS.Inspection_Report))
'              InspectionReportViewer.PrintDialog()
PrintReport.Export(Report)
PrintReport.m_currentPageIndex = 0
PrintReport.Print()

I have commented with **** the line in code that causes the error
The reporting class code is;
VB.NET:
Imports System.IO
Imports System.Data
Imports System.Text
Imports System.Drawing.Imaging
Imports System.Drawing.Printing
Imports System.Collections.Generic
Imports Microsoft.Reporting.WinForms

Public Class Reporting

    Implements IDisposable

    Public m_currentPageIndex As Integer
    Public m_streams As IList(Of Stream)

    Public Function CreateStream(ByVal name As String, _
       ByVal fileNameExtension As String, _
       ByVal encoding As Encoding, ByVal mimeType As String, _
       ByVal willSeek As Boolean) As Stream
        Dim stream As Stream = _
            New FileStream("C:\" + name + "." + fileNameExtension, FileMode.Create)
        m_streams.Add(Stream)
        Return stream
    End Function

    Public Sub Export(ByVal report As LocalReport)
        Dim deviceInfo As String = _
          "<DeviceInfo>" + _
          "  <OutputFormat>EMF</OutputFormat>" + _
          "  <PageWidth>8.5in</PageWidth>" + _
          "  <PageHeight>11in</PageHeight>" + _
          "  <MarginTop>0.25in</MarginTop>" + _
          "  <MarginLeft>0.25in</MarginLeft>" + _
          "  <MarginRight>0.25in</MarginRight>" + _
          "  <MarginBottom>0.25in</MarginBottom>" + _
          "</DeviceInfo>"
        Dim warnings() As Warning = Nothing
        m_streams = New List(Of Stream)()

        Try

            report.Render("Image", deviceInfo, AddressOf CreateStream, warnings)

        Catch e As System.Exception

            Dim inner As Exception = e.InnerException

            While Not (inner Is Nothing)


                MsgBox(inner.Message)

                inner = inner.InnerException

            End While

        End Try


    End Sub

    Public Sub PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
'******* THE NEXT LINE GENERATES THE ERROR *****************
        Dim pageImage As New Metafile(m_streams(m_currentPageIndex))

        ev.Graphics.DrawImage(pageImage, ev.PageBounds)
        m_currentPageIndex += 1
        ev.HasMorePages = (m_currentPageIndex < m_streams.Count)

    End Sub

    Public Sub Print()

        Const printerName As String = "Laser on Tiger"
        '        Const printerName As String = "Microsoft Office Document Image Writer"

        If m_streams Is Nothing OrElse m_streams.Count = 0 Then
            Return
        End If

        Dim printDoc As New PrintDocument()

        ' printDoc.PrinterSettings..PrinterName = printerName

        If Not printDoc.PrinterSettings.IsValid Then
            Dim msg As String = String.Format( _
                "Can't find printer ""{0}"".", printerName)
            Console.WriteLine(msg)
            Return
        End If

        AddHandler printDoc.PrintPage, AddressOf PrintPage
        printDoc.Print()

    End Sub

    Public Overloads Sub Dispose() Implements IDisposable.Dispose

        If Not (m_streams Is Nothing) Then
            Dim stream As Stream
            For Each stream In m_streams
                stream.Close()
            Next
            m_streams = Nothing
        End If

    End Sub

End Class
 
Last edited by a moderator:
Back
Top