Page 1 of 1

Print Issue

Posted: Thu Sep 07, 2017 2:23 pm
by Richardjmanning
Hello guys,
Was wondering if someone could help me. I am trying to use pagesetup and printdocument and printsetupdialog all at once.
What i want is to be able to do change settings using pagesetup and apply this to my printdocument and then print this using printsetup
HOWEVER any code i try i have this weird error where every time i click a dialog it just opens anouther instance of itsself.

Could someone give me example code of how to do this please.

Re: Print Issue

Posted: Thu Sep 07, 2017 4:41 pm
by CodenStuff
Hello,

Welcome to the frustrating world of printing from winforms lol

I'm not sure how to best help you on this but I can point you in the direction of some excellent resources that may help you:

https://docs.microsoft.com/en-us/dotnet ... nt-support

And a good example of using the pagesetupdialog:

https://msdn.microsoft.com/en-us/librar ... -snippet-2

If possible could you post your code/source-files so I can take a look and try and find where your problem is.

..and welcome to the site :)

Re: Print Issue

Posted: Fri Sep 08, 2017 9:14 am
by Richardjmanning
Code: Select all
    Private Sub QR_Client_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        PrintDocument1.PrinterSettings = PageSetupDialog1.PrinterSettings
        PageSetupDialog1.Document = PrintDocument1
    End Sub

    Private Sub XylosButton2_Click(sender As Object, e As EventArgs) Handles XylosButton2.Click
        Try
            PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
            PageSetupDialog1.PrinterSettings = PrintDocument1.PrinterSettings

            If PageSetupDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
                PrintDocument1.PrinterSettings = PageSetupDialog1.PrinterSettings
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub XylosButton1_Click(sender As Object, e As EventArgs) Handles XylosButton1.Click
        Try
            PrintDocument1.PrinterSettings = PrintDocument1.PrinterSettings
            PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings()
            PrintDocument1.Print()
        Catch ex As Exception
            MsgBox("Printing Problem" & Chr(13) & ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Using bmp As Bitmap = New Bitmap(QRCode.Image.Width, QRCode.Image.Height)

            Dim rect As New Rectangle(0, 0, QRCode.Image.Width, QRCode.Image.Height)
            Me.DrawToBitmap(bmp, rect)

            Dim l, t, w, h As Integer
            Dim ratio As Single = CSng(bmp.Width / bmp.Height)

            If ratio > e.MarginBounds.Width / e.MarginBounds.Height Then
                w = e.MarginBounds.Width
                h = CInt(w / ratio)
                t = CInt(e.MarginBounds.Top + (e.MarginBounds.Height / 2) - (h / 2))
                l = e.MarginBounds.Left
            Else
                h = e.MarginBounds.Height
                w = CInt(h * ratio)
                l = CInt(e.MarginBounds.Left + (e.MarginBounds.Width / 2) - (w / 2))
                t = e.MarginBounds.Top
            End If

            e.Graphics.DrawImage(bmp, l, t, w, h)
        End Using
    End Sub


Re: Print Issue

Posted: Fri Sep 08, 2017 9:44 am
by Richardjmanning
I Dont really know how to explain the issue. Baysically. The print dialog shows up fine. But anytime i click in it at all even blank areas it just opens anouther print dialog

Re: Print Issue

Posted: Fri Sep 08, 2017 4:44 pm
by CodenStuff
Not sure what's causing the problem, the following code works fine for me:
Code: Select all
Private Sub XylosButton2_Click(sender As Object, e As EventArgs) Handles XylosButton2.Click
        Try
            PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
            PageSetupDialog1.PrinterSettings = PrintDocument1.PrinterSettings

            If PageSetupDialog1.ShowDialog = DialogResult.OK Then
                PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
                PrintDocument1.PrinterSettings = PageSetupDialog1.PrinterSettings
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub