将图像从图片框移动到另一种形式的另一个图片框

时间:2014-10-18 19:52:28

标签: vb.net

我试图将图片从PictureBox移动到另一个PictureBox中由WebCam捕获的另一个Form,但我的代码无效。

Public Class Form12
Private _capture As Emgu.CV.Capture
Private _captureInProgress As Boolean
Dim form23 As Form23

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    form23.SetPictureBoxImage(captureImageBox.Image)
    form23.Show()
End Sub

Private Sub captureButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles captureButton.Click
        If (_capture Is Nothing) Then
            Try
                _capture = New Emgu.CV.Capture
            Catch excpt As NullReferenceException
                MessageBox.Show(excpt.Message)
            End Try
        End If
        If (Not _capture Is Nothing) Then
            If _captureInProgress Then
                Me.captureButton.Text = "Start Capture"
                RemoveHandler Application.Idle, New EventHandler(AddressOf Me.ProcessFrame)

            Else
                captureButton.Text = "Capture"
                AddHandler Application.Idle, New EventHandler(AddressOf Me.ProcessFrame)
            End If
            _captureInProgress = Not _captureInProgress
        End If
End Sub

Private Sub ProcessFrame(ByVal sender As Object, ByVal arg As EventArgs)

    Dim frame As Emgu.CV.Image(Of Emgu.CV.Structure.Bgr, Byte) = Me._capture.QueryFrame
    Dim grayFrame As Emgu.CV.Image(Of Emgu.CV.Structure.Gray, Byte) = frame.Convert(Of Emgu.CV.Structure.Gray, Byte)()
    Dim smoothedGrayFrame As Emgu.CV.Image(Of Emgu.CV.Structure.Gray, Byte) = grayFrame.PyrDown.PyrUp

    captureImageBox.Image = frame.Bitmap
End Sub

Private Sub ReleaseData()
    If (Not _capture Is Nothing) Then
        _capture.Dispose()
    End If
End Sub

第二表格

Public Class Form23
Public Sub SetPictureBoxImage(ByVal image As Bitmap)
    PictureBox1.Image = image
End Sub
End Class

所有网络摄像头工作正常,只是传输图像不是。对不起,这里只是一个新手。刚从教程中得到这些代码。谢谢!

1 个答案:

答案 0 :(得分:1)

表单是类 - 它在所有表单的顶部都是这样说的:

Public Class Form817

因此,应该创建它们的实例,这就是您的代码应该在任何地方使用的内容:

Dim myFrm As Form817            ' DECLARES the variable
myFrm = New Form817             ' Initialize myFrm as an instance of Form817

' short method:
Dim myFrm As New Form817

问题在于:

form23.SetPictureBoxImage(captureImageBox.Image)
form23.Show()

VB不区分大小写并且您的代码没有创建和实例,因此第一行是引用类,而不是实例。当您通常执行类似Form23.Show - 而不创建实例的操作时,VB会为您创建一个具有相同名称的实例。这称为默认表单实例,应该避免(始终)。

您的图片传输失败,因为代码引用了一件事(Form23),但显示了另一件事(Form23的新实例)。