Mehrere Charts auf ein A4 Blatt drucken.

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 23 Antworten in diesem Thema. Der letzte Beitrag () ist von Amelie.

    Hallo

    So nach langem tüfteln bin ich soweit gekommen. Es wird zwar immer als letztes ein leerBlatt ausgegeben aber das ist noch verschmerzbar ;)

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private Sub PrintChart(sender As Object, e As Printing.PrintPageEventArgs)
    2. ' Initialize variables
    3. Static currentPageIndex As Integer = 0
    4. Dim yPos As Single = e.MarginBounds.Top
    5. Dim chartWidth As Integer = CInt(e.PageSettings.PrintableArea.Width)
    6. Dim chartHeight As Integer = CInt(chartWidth * 0.75 / 2)
    7. Dim chartsPerPage As Integer = 2
    8. Dim chartIndex As Integer = 0
    9. For i As Integer = currentPageIndex To cmb_Wettertyp.Items.Count - 1 Step 1
    10. ' Set the selected item in the ComboBox
    11. cmb_Wettertyp.SelectedIndex = i
    12. ' Draw the chart
    13. Using chartImage As New Bitmap(Chart1.Width, Chart1.Height)
    14. Chart1.DrawToBitmap(chartImage, New Rectangle(0, 0, Chart1.Width, Chart1.Height))
    15. e.Graphics.DrawImage(chartImage, e.MarginBounds.Left, yPos, chartWidth, chartHeight)
    16. chartIndex += 1
    17. If chartIndex Mod 2 = 0 Then
    18. yPos += chartHeight
    19. If chartIndex < cmb_Wettertyp.Items.Count Then
    20. e.HasMorePages = True
    21. currentPageIndex = i + 1
    22. Exit Sub
    23. End If
    24. Else
    25. yPos += chartHeight
    26. End If
    27. End Using
    28. Next
    29. ' Reset the page index
    30. currentPageIndex = 0
    31. End Sub

    Bilder
    • chart-1.jpg

      174,44 kB, 641×868, 50 mal angesehen
    Asperger Autistin. Brauche immer etwas um gewisse Sachen zu verstehen. :huh:

    Amelie schrieb:

    Es wird zwar immer als letztes ein leerBlatt ausgegeben

    VB.NET-Quellcode

    1. Dim chartIndex As Integer = 0
    Der erste gültige Chart-Index ist 1, nicht aber 0 ("Bitte wählen").
    Sieh Dir das mal an:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private Sub PrintChartButton_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
    2. ' Create a PrintDocument object
    3. Dim pd As New Printing.PrintDocument()
    4. ' Set the printer settings
    5. pd.DefaultPageSettings.Landscape = Paperformat ' Set the page orientation to landscape
    6. pd.DefaultPageSettings.PaperSize = New PaperSize("A4", 827, 1169) ' Set the paper size to A4
    7. ' Add an event handler to print the chart
    8. AddHandler pd.PrintPage, AddressOf PrintChart
    9. AddHandler pd.BeginPrint, AddressOf BeginPrint
    10. ' Print the chart
    11. pd.Print()
    12. End Sub
    13. Private currentPageIndex As Integer
    14. Private chartIndex As Integer
    15. Private Sub BeginPrint(sender As Object, e As Printing.PrintEventArgs)
    16. ' Reset the page index
    17. currentPageIndex = 0
    18. chartIndex = 1
    19. End Sub
    20. Private Sub PrintChart(sender As Object, e As Printing.PrintPageEventArgs)
    21. ' Initialize variables
    22. Dim yPos As Single = e.MarginBounds.Top
    23. Dim chartWidth As Integer = CInt(e.PageSettings.PrintableArea.Width)
    24. Dim chartHeight As Integer = (chartWidth * 3) \ 8
    25. e.Graphics.DrawString($"Seite {currentPageIndex + 1}", SystemFonts.CaptionFont, Brushes.Red, e.MarginBounds.Left, yPos)
    26. yPos += SystemFonts.CaptionFont.Height
    27. ' 2 Charts pro Seite
    28. For i As Integer = 0 To 1
    29. ' Set the selected item in the ComboBox
    30. cmb_Wettertyp.SelectedIndex = chartIndex
    31. ' Draw the chart
    32. Using chartImage As New Bitmap(Chart1.Width, Chart1.Height)
    33. Chart1.DrawToBitmap(chartImage, New Rectangle(0, 0, Chart1.Width, Chart1.Height))
    34. e.Graphics.DrawImage(chartImage, e.MarginBounds.Left, yPos, chartWidth, chartHeight)
    35. e.Graphics.DrawString($"{chartIndex}: {cmb_Wettertyp.SelectedItem}", SystemFonts.CaptionFont, Brushes.Red, e.MarginBounds.Left, yPos)
    36. chartIndex += 1
    37. yPos += chartHeight
    38. If chartIndex = cmb_Wettertyp.Items.Count Then
    39. Exit Sub
    40. End If
    41. If i = 1 Then
    42. e.HasMorePages = True
    43. currentPageIndex += 1
    44. End If
    45. End Using
    46. Next
    47. End Sub
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Obwohl es anscheinend nicht mehr relevant ist:
    Änderungen am Originalprojekt

    VB.NET-Quellcode

    1. Private ReadOnly Images As New List(Of Image)
    2. Private Sub PrintChart(sender As Object, e As Printing.PrintPageEventArgs)
    3. ' Draw the chart on a bitmap image
    4. ' Calculate the dimensions of the chart on the printed page
    5. Dim RowIndex = 0
    6. For i = 0 To Images.Count - 1
    7. Dim chartWidth As Integer = CInt(225 * 1.5)
    8. Dim chartHeight As Integer = CInt(150 * 1.5)
    9. ' Draw the chart on the printed page
    10. ' gespeicherte Bilder durchgehen und platzieren
    11. e.Graphics.DrawImage(Images(i), CInt(e.MarginBounds.Left + 250 * 1.5 * (i Mod 2)), CInt(e.MarginBounds.Top + (175 * 1.5 * RowIndex)), chartWidth, chartHeight)
    12. If i Mod 2 = 1 Then RowIndex += 1
    13. Next
    14. End Sub
    15. Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'alles drucken
    16. For i = 1 To cmb_Wettertyp.Items.Count - 1
    17. cmb_Wettertyp.SelectedIndex = i 'ComboBox-Selektion ändern
    18. Await Threading.Tasks.Task.Delay(150) 'dem GUI Zeit geben, sich zu aktualisieren
    19. Dim chartImage As New Bitmap(Chart1.Width, Chart1.Height)
    20. Chart1.DrawToBitmap(chartImage, New Rectangle(0, 0, Chart1.Width, Chart1.Height)) 'Bild aus Chart erstellen
    21. Images.Add(chartImage) 'Bild merken
    22. Next
    23. PrintChartButton_Click(Me, EventArgs.Empty)
    24. End Sub


    Ich hab den Code weder aufgeräumt, noch den Button umbenannt oder mich um die Entsorgung der Bilder nach dem Drucken gekümmert.
    Dateien
    • WetterAppUI.zip

      (390,09 kB, 53 mal heruntergeladen, zuletzt: )
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.