0

I am using a picturebox for a print preview. The preview using PrintDocument is inadequate, or more precisely, overadequate. In any event, I can not use it.

I tried copying the image and updating in the paint event. But this does not work. From what I've read it appears that using graphics methods does not create an image. Unfortunately all of the issues I've read through don't specifically cover my example nor explain whats really going on. It would be nice to know where this image i'm creating is going and how to repaint it in the paint event.

I've created a much simpler example than the code i'm using just for illustration purposes.

Form:

Public Class Form1

    Dim Outout As Image


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim PD As RFPrinting


        PD = New RFPrinting
        PD.Output = PictureBox1
        PD.Print(1)
        Outout = PictureBox1.Image

    End Sub


    Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint

        PictureBox1.Image = Outout

    End Sub

End Class

Designer:

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox()
        Me.Button1 = New System.Windows.Forms.Button()
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.BackColor = System.Drawing.SystemColors.ControlLightLight
        Me.PictureBox1.Location = New System.Drawing.Point(42, 28)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(544, 436)
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(511, 485)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(75, 23)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Button1"
        Me.Button1.UseVisualStyleBackColor = True
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(635, 533)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.PictureBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents Button1 As System.Windows.Forms.Button

End Class

PrintDoc:

Imports System.Drawing
Imports System.Drawing.Printing
Imports System.IO
Imports System.Windows.Forms


Public Class RFPrinting

    Inherits PrintDocument


    'Output
    Private mCanvas As PictureBox



    Public Property Output As PictureBox
        Get
            Return mCanvas
        End Get
        Set(value As PictureBox)
            mCanvas = value
        End Set
    End Property




    Private Sub PrintDocument_PrintPage(ByVal sender As Object, _
    ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Me.PrintPage


        If mCanvas IsNot Nothing Then
            e = New PrintPageEventArgs(mCanvas.CreateGraphics, New Rectangle(New Point(25, 25), New Size(New Point(825, 1075))), e.PageBounds, e.PageSettings)
        End If

        'Draw box
        e.Graphics.DrawRectangle(Pens.Gray, 20, 30, e.PageBounds.Width - 100, e.PageBounds.Height - 130)

        PrintHeader(e)


        e.HasMorePages = False

    End Sub




    Private Function PrintHeader(ByVal e As System.Drawing.Printing.PrintPageEventArgs) As Integer

        Const conTopCertification As Integer = 200
        Const conTopCustomer As Integer = conTopCertification + 80

        Dim PrintFont As Font
        Dim strText As String


        strText = "CERTIFICATION"
        PrintFont = New Font("Arial", 16, FontStyle.Bold)
        e.Graphics.DrawString(strText, PrintFont, Brushes.Black, (e.MarginBounds.Width - e.Graphics.MeasureString(strText, PrintFont).Width) / 2 + 60, conTopCertification)


        Return 0

    End Function

    Public Shadows Sub Print(ByVal intCount As Integer)

        Dim r As Integer


        For r = 1 To intCount
            MyBase.Print()
        Next

    End Sub


End Class

You can comment out the Paint event to see something in the picturebox, but it is not persistent.

Anyone know how to fix this?

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Github
  • 39
  • 6

1 Answers1

0

I needed to use a bitmap instead of creating a graphics object from the picturebox. For a complete description, please look here: How do I repaint my picturebox when the picture disappears?

Form:

Public Class Form1


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim Outout As Bitmap
        Dim PD As RFPrinting
        Dim Outout As Bitmap


        PD = New RFPrinting
        Outout = New Bitmap(850, 1100)
        PD.Output = Outout
        PD.Print(1)
        PictureBox1.Image = Outout

    End Sub

End Class

PrintDoc:

Imports System.Drawing
Imports System.Drawing.Printing
Imports System.IO
Imports System.Windows.Forms


Public Class RFPrinting

    Inherits PrintDocument


    'Output
    Private mCanvas As Bitmap

    Public Property Output As Bitmap
        Get
            Return mCanvas
        End Get
        Set(value As Bitmap)
            mCanvas = value
        End Set
    End Property

    Private Sub PrintDocument_PrintPage(ByVal sender As Object, _
    ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Me.PrintPage


        If mCanvas IsNot Nothing Then
            e = New PrintPageEventArgs(Graphics.FromImage(mCanvas), New Rectangle(New Point(25, 25), New Size(New Point(825, 1075))), e.PageBounds, e.PageSettings)
        End If

        'Draw box
        e.Graphics.DrawRectangle(Pens.Gray, 20, 30, e.PageBounds.Width - 100, e.PageBounds.Height - 130)

        PrintHeader(e)


        e.HasMorePages = False

    End Sub


    Private Function PrintHeader(ByVal e As System.Drawing.Printing.PrintPageEventArgs) As Integer

        Const conTopCertification As Integer = 200
        Const conTopCustomer As Integer = conTopCertification + 80

        Dim PrintFont As Font
        Dim strText As String


        strText = "CERTIFICATION"
        PrintFont = New Font("Arial", 16, FontStyle.Bold)
        e.Graphics.DrawString(strText, PrintFont, Brushes.Black, (e.MarginBounds.Width - e.Graphics.MeasureString(strText, PrintFont).Width) / 2 + 60, conTopCertification)


        Return 0

    End Function

    Public Shadows Sub Print(ByVal intCount As Integer)

        Dim r As Integer


        For r = 1 To intCount
            MyBase.Print()
        Next

    End Sub

End Class
Github
  • 39
  • 6