can anyone help me please..

i'm stuck with this thing for 2 days.. i have try to find answer from one forum to another, and it is give me heavy headache.

what i want is that my panel size in my form has a function as print area, in my panel i have one picture box, some txtLabels and some textboxes.

i have try some codes people show, but when i preview it in previewDialog, my preview give me layout more bigger than my panel (i have prove it with add 1 text in each corner of my panel and previewdialog layout show bigger than my panel)

here is the picture :

http://i1184.photobucket.com/albums/z328/waluhkuning/for%20share/mypreview.jpg

and here is the code i use (note: this code just for print text, i want include picture box too) :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PrintPreviewDialog1.Document = PrintDocument1
        PrintPreviewDialog1.ShowDialog()

    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        For Each ctrl As Control In Me.Panel1.Controls
            Dim sf As New System.Drawing.StringFormat
            If TypeOf ctrl Is TextBox Then
                If DirectCast(ctrl, TextBox).TextAlign = HorizontalAlignment.Right Then
                    sf.Alignment = StringAlignment.Far
                Else
                    sf.Alignment = StringAlignment.Near
                End If
            ElseIf TypeOf ctrl Is Label Then
                If DirectCast(ctrl, Label).TextAlign = HorizontalAlignment.Right Then
                    sf.Alignment = StringAlignment.Far
                Else
                    sf.Alignment = StringAlignment.Near
                End If
            End If
            e.Graphics.DrawString(ctrl.Text, ctrl.Font, New SolidBrush(ctrl.ForeColor), New RectangleF(ctrl.Left, ctrl.Top + 15, ctrl.Width, ctrl.Height), sf)
        Next
    End Sub

i'll do appreciate if you can help me guys, thanks in advance..

Edited 4 Years Ago by artemix22: n/a

can anyone help me please..

i'm stuck with this thing for 2 days.. i have try to find answer from one forum to another, and it is give me heavy headache.

what i want is that my panel size in my form has a function as print area, in my panel i have one picture box, some txtLabels and some textboxes.

i have try some codes people show, but when i preview it in previewDialog, my preview give me layout more bigger than my panel (i have prove it with add 1 text in each corner of my panel and previewdialog layout show bigger than my panel)

here is the picture :

http://i1184.photobucket.com/albums/z328/waluhkuning/for%20share/mypreview.jpg

and here is the code i use (note: this code just for print text, i want include picture box too) :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PrintPreviewDialog1.Document = PrintDocument1
        PrintPreviewDialog1.ShowDialog()

    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        For Each ctrl As Control In Me.Panel1.Controls
            Dim sf As New System.Drawing.StringFormat
            If TypeOf ctrl Is TextBox Then
                If DirectCast(ctrl, TextBox).TextAlign = HorizontalAlignment.Right Then
                    sf.Alignment = StringAlignment.Far
                Else
                    sf.Alignment = StringAlignment.Near
                End If
            ElseIf TypeOf ctrl Is Label Then
                If DirectCast(ctrl, Label).TextAlign = HorizontalAlignment.Right Then
                    sf.Alignment = StringAlignment.Far
                Else
                    sf.Alignment = StringAlignment.Near
                End If
            End If
            e.Graphics.DrawString(ctrl.Text, ctrl.Font, New SolidBrush(ctrl.ForeColor), New RectangleF(ctrl.Left, ctrl.Top + 15, ctrl.Width, ctrl.Height), sf)
        Next
    End Sub

i'll do appreciate if you can help me guys, thanks in advance..

never mind, i found this, and this much better :

<System.Runtime.InteropServices.DllImport("gdi32.dll")> _
    Public Shared Function BitBlt(ByVal hdcDest As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, _
 ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As Integer) As Long
        End Function

'get the screenshot
Private memoryImage As Bitmap
Private Sub CaptureScreen()
        Dim mygraphics As Graphics = Me.Panel1.CreateGraphics()
        Dim s As Size = Me.Panel1.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.Panel1.ClientRectangle.Width, Me.Panel1.ClientRectangle.Height, dc1, _
         0, 0, 13369376)
        mygraphics.ReleaseHdc(dc1)
        memoryGraphics.ReleaseHdc(dc2)
End Sub
 
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        e.Graphics.DrawImage(memoryImage, 0, 0)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1_Click
            CaptureScreen()
            PrintDocument1.Print()
End Sub

Edited 4 Years Ago by artemix22: n/a

This question has already been answered. Start a new discussion instead.