Question help needed with margins when printing

macvic211

Member
Joined
Oct 31, 2009
Messages
6
Location
Wales
Programming Experience
Beginner
I have a picturebox which displays a JPEG image (this image has been created using Photoshop to a full A4 size) I have figured out how to print, but when printed, the margins around the image are too large - i need the image to print to a full A4 page with just the printer's default margins.

This is the code I have used to print :

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
PrintDocument1.Print()
End If
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawImage(Me.PictureBox1.Image, e.MarginBounds)
End Sub


Any help with this problem is much appreciated.
 
Tip - create a print preview option and check it before it's printed - helps to save the planet ;)

Although you believe it's been sized for A4, you'll probably still have to enlarge it to fit the page. For example:-

VB.NET:
Option Explicit On
Option Strict On

Imports System.Drawing.Printing

Public Class Form1

    Public WithEvents pdPrinter As New PrintDocument

    Private Sub TestPrintPreview(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

        Using ppvwPrinter As New PrintPreviewDialog
            With ppvwPrinter
                .Document = pdPrinter
                .Width = Screen.PrimaryScreen.Bounds.Width
                .Height = Screen.PrimaryScreen.Bounds.Height
                .PrintPreviewControl.Zoom = 0.66
                .ShowDialog()
            End With
        End Using
    End Sub

    Private Sub Begin_Printing(ByVal sender As System.Object, _
    ByVal e As System.Drawing.Printing.PrintEventArgs) _
     Handles pdPrinter.BeginPrint
        pdPrinter.DocumentName = "Test Document"
        pdPrinter.PrintController = New Printing.StandardPrintController()
    End Sub

    Private Sub Test_PrintPage(ByVal sender As System.Object, _
    ByVal e As PrintPageEventArgs) Handles pdPrinter.PrintPage
        With e.Graphics
            .PageUnit = GraphicsUnit.Millimeter

            .DrawImage(PictureBox1.Image, 0, 0, Convert.ToInt32((e.PageSettings.PaperSize.Width / 100) * 25.4), Convert.ToInt32((e.PageSettings.PaperSize.Height / 100) * 25.4))
        End With
    End Sub

End Class

If you find that the PrintPreview and the actual print are not in the same place, look at this thread
 
Thanks for the quick response, but unfortunately, I'm still having problems! The jpeg image that needs to be printed is definitely sized to A4. I tried adding a print preview and page setup as you suggested and got the image to print centrally by altering the margins in the page setup from 10 to 3mm around each side. Is there a way to alter this by using code so that 3mm is the default setting of the program? The reason for this is that I do not want a print preview/page setup on the end program as it needs to run as simply as possible. Sorry if I'm asking simple questions, but as I said, I am just learning!
 
VB.NET:
        With pdPrinter
            .DefaultPageSettings.Margins.Left = Convert.ToInt32((3.0 / 25.4) * 100)
        End With

should work.

The main reason for using the Print Preview is to see what it's going to do. Once you have it done, just convert the code to print normally :)
 

Latest posts

Back
Top