Я использую Picturebox для предварительного просмотра. Предварительный просмотр с помощью PrintDocument неадекватен, а точнее, слишком адекватен. В любом случае, я не могу его использовать.
Я попытался скопировать изображение и обновить его в событии рисования. Но это не работает. Из того, что я прочитал, кажется, что использование графических методов не создает изображения. К сожалению, все проблемы, которые я прочитал, не охватывают конкретно мой пример и не объясняют, что на самом деле происходит. Было бы неплохо знать, куда движется это изображение, которое я создаю, и как его перерисовать в событии рисования.
Я создал гораздо более простой пример, чем код, который я использую только для иллюстрации.
Форма:
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
Дизайнер:
<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
ПринтДок:
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
Вы можете закомментировать событие Paint, чтобы увидеть что-то в окне изображения, но оно не будет постоянным.
Кто-нибудь знает, как это исправить?
Мне нужно было использовать растровое изображение вместо создания графического объекта из изображения. Полное описание смотрите здесь: Как перекрасить фотобокс, когда изображение исчезает?
Форма:
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
ПринтДок:
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