Regarding Printing In VB.NET?


  • I have a problem that the current output image that is displayed in my form does not print in its actual size..i have an output image as rectangle..drawn in millimeter...displays in form..but when i print the actual image gets printed small..

    can anyone suggest me a answer......

    • Editado aashiq313 martes, 13 de marzo de 2012 4:11
    lunes, 12 de marzo de 2012 10:18


  • Hi Aashiq313,

    Welcome to the MSDN forum.

    Did you set the DPI of your project? If so, I’d suggest you check the DPI setting of your project. If its DPI is different to the system settings, you may get the scenario in your post. Could you please provide some codes about printing preview?

    The following is my codes of printing preview:

    Imports System.Windows.Forms
    Imports System.Drawing
    Imports System.Drawing.Printing
    Public Class Form1
    <System.Runtime.InteropServices.DllImport("gdi32.dll")> _
        Public Shared Function BitBlt(hdcDest As IntPtr, nXDest As Integer, nYDest As Integer, nWidth As Integer, nHeight As Integer, hdcSrc As IntPtr, _
     nXSrc As Integer, nYSrc As Integer, dwRop As Integer) As Long
        End Function
        Private memoryImage As Bitmap
        Private Sub CaptureScreen()
            Dim mygraphics As Graphics = Me.CreateGraphics()
            Dim s As Size = Me.Size
            memoryImage = New Bitmap(s.Width, s.Height, mygraphics)
            Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
            Dim dc1 As IntPtr = mygraphics.GetHdc()
            Dim dc2 As IntPtr = memoryGraphics.GetHdc()
            BitBlt(dc2, 0, 0, Me.ClientRectangle.Width, Me.ClientRectangle.Height, dc1, _
             0, 0, 13369376)
        End Sub
        Private Sub printDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles printDocument1.PrintPage
            e.Graphics.DrawImage(memoryImage, 0, 0)
        End Sub
        Private Sub printButton_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            PrintPreviewDialog1.Document = printDocument1
        End Sub
    End Class

    And I drew the image by the following codes:

        Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
            Dim units As GraphicsUnit = GraphicsUnit.Millimeter
            Dim rectangle1 As New Rectangle(10, 10, 60, 80)
            Dim rectangle2 As New Rectangle(30, 5, 80, 60)
            e.Graphics.PageUnit = GraphicsUnit.Millimeter
            e.Graphics.DrawRectangle(Pens.Black, rectangle1)
            e.Graphics.DrawRectangle(Pens.Red, rectangle2)
            If (rectangle1.IntersectsWith(rectangle2)) Then
                If Not (rectangle1.IsEmpty) Then
                    e.Graphics.FillRectangle(Brushes.Green, rectangle1)
                End If
            End If
        End Sub

    I hope this will be helpful and I'm looking forward to your feedback.

    Best regards,

    Shanks Zen
    MSDN Community Support | Feedback to us

    miércoles, 14 de marzo de 2012 3:42