【问题标题】:Why does my picturebox image not get updated and return null?为什么我的图片框图像没有更新并返回 null?
【发布时间】:2019-04-04 19:00:44
【问题描述】:

我正在使用图片框进行打印预览。使用 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 事件以在图片框中看到一些东西,但它不是持久的。

有人知道如何解决这个问题吗?

【问题讨论】:

    标签: .net vb.net


    【解决方案1】:

    我需要使用位图而不是从图片框创建图形对象。 完整描述请看这里:How do I repaint my picturebox when the picture disappears?

    表格:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 2015-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多