我正在使用图片框进行打印预览。使用PrintDocument进行的预览不足,或更准确地说,不足。无论如何,我不能使用它。
我尝试复制图像并在绘画事件中进行更新。但这是行不通的。从我阅读的内容来看,使用图形方法似乎无法创建图像。不幸的是,我读过的所有问题都没有具体说明我的示例,也没有解释真正的情况。知道我要创建的图像要去哪里以及如何在paint事件中重新绘制它会很高兴。
我创建了一个比我仅出于说明目的使用的代码简单得多的示例。
表格:
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
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
您可以注释掉Paint事件以在图片框中看到某些内容,但是它不是永久性的。
有人知道如何解决这个问题吗?
答案 0 :(得分:0)
我需要使用位图而不是从图片框创建图形对象。 有关完整的说明,请点击此处:How do I repaint my picturebox when the picture disappears?
表格:
bool _Has_unused_capacity() const _NOEXCEPT
{ // micro-optimization for capacity() != size()
return (this->_Myend() != this->_Mylast());
PrintDoc:
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