Announcement

Collapse
No announcement yet.

System.ArgumentException Parameter is not valid beim Drucken

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • System.ArgumentException Parameter is not valid beim Drucken

    Hallo,

    ich bekomme beim Ausdrucken mehrerer Tabpage Inhalte ab der 12.Tabpage diesen Fehler.
    Drucke ich alles einzeln, funktioniert alles tadellos! Ich mach hier schon ewig rum. Vielleicht kann mich jemand in die richtige Richtung weisen

    fehler.jpg

    Vielen Dank schon mal im Voraus!

    Anbei mein Code

    Code:
     Private Sub AktuelleSeiteDruckenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AktuelleSeiteDruckenToolStripMenuItem.Click
            Try
                Dim MyPrintPreview As New PrintPreviewDialog()
                Dim Myprintdialog As New PrintDialog()
    
                If Myprintdialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                    Myprintdialog.PrinterSettings = Myprintdialog.PrinterSettings
                Else
                    Exit Sub
                End If
    
                Dim MyPrintDoc As New PrintDocument()
                MyPrintDoc.DocumentName = "Inspektionsliste Ausdruck"
                MyPrintDoc.PrinterSettings = Myprintdialog.PrinterSettings
                AddHandler MyPrintDoc.PrintPage, AddressOf Print_Page
    
    
                With PageSetupDialog1
                    .Document = MyPrintDoc
                    .PageSettings = MyPrintDoc.DefaultPageSettings
                    .PageSettings.Landscape = False
                    .PageSettings.PrinterSettings = Myprintdialog.PrinterSettings
                End With
    
                If PageSetupDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                    MyPrintDoc.DefaultPageSettings = PageSetupDialog1.PageSettings
                Else
                    Exit Sub
                End If
    
                MyPrintDoc.DefaultPageSettings = PageSetupDialog1.PageSettings 'CWA
    
                'MyPrintPreview.Document = MyPrintDoc
                'MyPrintPreview.WindowState = FormWindowState.Maximized
                'MyPrintPreview.ShowDialog()
                '        MyPrintDoc.Dispose()
                Dim g As PrintDocument = MyPrintDoc
                MyPrintDoc.Print()
    
                MyPrintDoc.Dispose()
            Catch ex As Exception
                MessageBox.Show(ex.ToString())
                Stop
            End Try
        End Sub
    
        Dim page As Integer = 0
    
        Private Sub Print_Page(ByVal sender As Object, ByVal MyPrintPageEvents As PrintPageEventArgs)
    
            TabControl1.SelectedIndex = page
    
            TabPage1.AutoScrollPosition = New Point(0, 0)
            Dim MyParentCntrl As Control = TabControl1.SelectedTab 'Get the parent control that the print buitton resides on
            TabControl1.SelectedTab = MyParentCntrl
            Dim bm As New Bitmap(MyParentCntrl.Parent.Bounds.Width, MyParentCntrl.Parent.Bounds.Height)
    
            bm = DrawAllSubControlsToBm(MyParentCntrl)
            'size for image to scale to automaticaly
            Dim MyScaleFactor As Decimal = 1
            MyScaleFactor = MyPrintPageEvents.MarginBounds.Height / bm.Height
    
            If bm.Width * MyScaleFactor > MyPrintPageEvents.MarginBounds.Width Then
                MyScaleFactor = MyPrintPageEvents.MarginBounds.Width / bm.Width
            End If
    
            Dim sourceRectangle As New Rectangle(0, 0, bm.Width, bm.Height) ' Position Gesamtausdruck!
            Dim destRetangle1 As New Rectangle(MyPrintPageEvents.MarginBounds.Left, MyPrintPageEvents.MarginBounds.Top, bm.Width * MyScaleFactor, bm.Height * MyScaleFactor)
    
            MyPrintPageEvents.Graphics.InterpolationMode = Drawing2D.InterpolationMode.Bicubic 'HighQualityBicubic
            MyPrintPageEvents.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            MyPrintPageEvents.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
            MyPrintPageEvents.Graphics.DrawImage(bm, destRetangle1, sourceRectangle, GraphicsUnit.Pixel)
    
    
            If page < 14 Then
                MyPrintPageEvents.HasMorePages = True
                page += 1
            End If
    
            If page = 14 Then
                MyPrintPageEvents.HasMorePages = False
            End If
        End Sub

  • #2
    Es knallt laut Stack in DrawAllSubControlsToBm (beim Erzeugen eines Bitmaps). Es würde also helfen diese Methode zu sehen.

    Comment


    • #3
      Oh, natürlich, sorry!

      Code:
        Private Function DrawAllSubControlsToBm(ByRef MyTopLevelCntrl As Control) As Bitmap
      
              'Dim My_Top_LevelCntrl_Bm As New System.Drawing.Bitmap(MyTopLevelCntrl.Parent.ClientRectangle.Width, MyTopLevelCntrl.Parent.Bounds.Bottom)
              Dim My_Top_LevelCntrl_Bm As New System.Drawing.Bitmap(4000, 4000)
              Dim My_Graphics_Drawing_Board As System.Drawing.Graphics
              'if the control ie tabpage has scroll bars and the page is not at 0,0 then the image gets skewed, we correct it with this
              Dim MyParentScrollPositionCorrection As Point = New Point(0, 0)
              Dim MyTempTabPage As TabPage
              If TypeOf MyTopLevelCntrl Is TabPage Then
                  MyTempTabPage = DirectCast(MyTopLevelCntrl, TabPage)
                  MyParentScrollPositionCorrection.X = MyTempTabPage.HorizontalScroll.Value
                  MyParentScrollPositionCorrection.Y = MyTempTabPage.VerticalScroll.Value
                  MyParentScrollPositionCorrection = MyTempTabPage.AutoScrollPosition
              End If
      
              'MyTopLevelCntrl.DrawToBitmap(My_Top_LevelCntrl_Bm, MyTopLevelCntrl.ClientRectangle)
              MyTopLevelCntrl.DrawToBitmap(My_Top_LevelCntrl_Bm, New Rectangle(0, 0, MyTopLevelCntrl.Bounds.Right, MyTopLevelCntrl.Bounds.Bottom))
              My_Graphics_Drawing_Board = System.Drawing.Graphics.FromImage(My_Top_LevelCntrl_Bm)
              My_Graphics_Drawing_Board.Clear(Color.White)
              'Set various modes to higher quality
              My_Graphics_Drawing_Board.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
              My_Graphics_Drawing_Board.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
              My_Graphics_Drawing_Board.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
              Dim MySorted_CntrlList As New List(Of Control)
              Dim My_Unsorted_CntrlList As New List(Of Control)
              'Create Sub Cntrl List
              For Each SubCntrl As Control In MyTopLevelCntrl.Controls
                  My_Unsorted_CntrlList.Add(SubCntrl)
              Next
              Do While (My_Unsorted_CntrlList.Count > 0)
                  Dim MyCntrl_to_Sort As Control
                  MyCntrl_to_Sort = GetCntrlList_Into_Correct_Z_Order(MyTopLevelCntrl, My_Unsorted_CntrlList)
                  MySorted_CntrlList.Add(MyCntrl_to_Sort)
                  My_Unsorted_CntrlList.Remove(MyCntrl_to_Sort)
              Loop
      
              'Get the maximum point to the Right and to the bottom of the drawn bit maps, I wish to clip off the white space so the image fits nicely into the print preview
              Dim MaxRightPointForClip As Integer = 0
              Dim MaxBottomPointForClip As Integer = 0
              Dim My_CntrlList_Count As Integer = MySorted_CntrlList.Count - 1
              For My_Current_Cntrl_Item_Index As Integer = My_CntrlList_Count To 0 Step -1
                  'Create New Bitmap the same size as current cntrl at list index
                  Dim My_SubItem_Image As New Bitmap(MySorted_CntrlList(My_Current_Cntrl_Item_Index).Width, MySorted_CntrlList(My_Current_Cntrl_Item_Index).Height)
                  'Draw The current Control at list index to the new Bitmap if the control is visible on the form
                  If MySorted_CntrlList(My_Current_Cntrl_Item_Index).Visible = True Then
                      MySorted_CntrlList(My_Current_Cntrl_Item_Index).DrawToBitmap(My_SubItem_Image, MySorted_CntrlList(My_Current_Cntrl_Item_Index).ClientRectangle)
                      My_Graphics_Drawing_Board.DrawImageUnscaled(My_SubItem_Image, New Point(MySorted_CntrlList(My_Current_Cntrl_Item_Index).Left - MyParentScrollPositionCorrection.X, MySorted_CntrlList(My_Current_Cntrl_Item_Index).Top - MyParentScrollPositionCorrection.Y))
                  End If
                  'Get the maximum point If visible
                  If MySorted_CntrlList(My_Current_Cntrl_Item_Index).Visible = True Then
                      If MaxRightPointForClip < MySorted_CntrlList(My_Current_Cntrl_Item_Index).Left + MySorted_CntrlList(My_Current_Cntrl_Item_Index).Width Then
                          MaxRightPointForClip = MySorted_CntrlList(My_Current_Cntrl_Item_Index).Left + MySorted_CntrlList(My_Current_Cntrl_Item_Index).Width
                      End If
                      If MaxBottomPointForClip < MySorted_CntrlList(My_Current_Cntrl_Item_Index).Top + MySorted_CntrlList(My_Current_Cntrl_Item_Index).Bounds.Height Then
                          MaxBottomPointForClip = MySorted_CntrlList(My_Current_Cntrl_Item_Index).Top + MySorted_CntrlList(My_Current_Cntrl_Item_Index).Bounds.Height
                      End If
                  End If
              Next
      
      
              'Crop The Image to remove white space right and bottom
              'frmPic.Show()
              'frmPic.PictureBox1.Image = My_Top_LevelCntrl_Bm
              MaxRightPointForClip = MaxRightPointForClip + 15 'Add buffer edge
              MaxBottomPointForClip = MaxBottomPointForClip + 15 'Add buffer Edge
              Dim CroppedBm As Bitmap = New Bitmap(MaxRightPointForClip, MaxBottomPointForClip)
      
              My_Graphics_Drawing_Board = System.Drawing.Graphics.FromImage(CroppedBm)
              'frmPic.Show()
              My_Graphics_Drawing_Board.DrawImageUnscaledAndClipped(My_Top_LevelCntrl_Bm, New Rectangle(0, 0, MaxRightPointForClip, MaxBottomPointForClip)) '.VisibleClipBounds(New Rectangle(10, 10, 100, 200))
      
              'frmPic.PictureBox1.Image = CroppedBm
      
              'Return My_Top_LevelCntrl_Bm
              Return CroppedBm ' My_Top_LevelCntrl_Bm
      
          End Function

      Comment


      • #4
        Du erzeugst ein 4000*4000 Pixel Bitmap mit 32bit Farbtiefe (als Standard wird PixelFormat.Format32bppArgb benutzt)
        Wenn ich rechnen kann sind das mal kurz 64MB. Und da du die nirgendwo explizit disposed werden die von GDI auch nicht kurzfristig wieder freigegeben.
        Wenn du das mehrmals machst wird dir vermutlich sehr schnell der Speicher ausgehen.

        a.) Du solltest dir überlegen ob du tatsächlich ein derart großes Bitmap brauchst (und in dieser Farbtiefe)
        b.) Dafür sorgen das die Resourcen für das Bitmap am Ende auch wieder freigegeben werden. Also das am Ende Dispose aufgerufen wird. Dazu ist es hilfreich den using Syntax zu verwenden um das disposen zu garantieren

        Comment


        • #5
          Danke Ralf für die schnelle Antwort!
          Ich habe es nun so versucht :
          Code:
            
          Dim My_Top_LevelCntrl_Bm As New System.Drawing.Bitmap(2000, 2000, PixelFormat.Format16bppRgb555)
          Leider ohne Erfolg. Ich werde mich nun an die dispose Geschichte machen. Auch wenn ich noch nicht genau weiß wie und wo ich das implementieren muss.

          Bin dir auf jeden Fall dankbar!!
          Zuletzt editiert von walti82w; 25.07.2014, 09:54.

          Comment


          • #6
            Nein, ich kriegs nicht hin.

            Ich habe das gleiche Problem, wenn ich ein nur 50x50 Bitmap erstelle. Ich verstehe das nicht. Ich müsste doch wenigstens ein paar Seiten weiter kommen. Egal was ich wo dispose, immer ist bei Seite 12 Schluss.

            Comment


            • #7
              ...nach entfernen und neu erstellen der Tabpage 12, funktioniert es.

              Der RAM verbrauch ist aber wie du schon sagtest, viel zu hoch.

              Comment

              Working...
              X