好的,基本上,我在类的函数中分配了一个数组值。但是在类执行之后,数组重置为空。这是我的代码:
Public Class MoisacDialog
Public imgArray(,) As Bitmap
Private Sub cmdCancel_Click(sender As Object, e As EventArgs) Handles cmdCancel.Click
DialogResult = DialogResult.Cancel
End Sub
Private Sub cmdOK_Click(sender As Object, e As EventArgs) Handles cmdOK.Click
Try
Dim rows As Integer = Convert.ToInt32(txtRows.Text)
Dim cols As Integer = Convert.ToInt32(txtCols.Text)
If rows > 0 And cols > 0 Then
ReDim imgArray(rows - 1, cols - 1)
For i As Integer = 0 To cols - 1
For j As Integer = 0 To rows - 1
Using fileImage As New OpenFileDialog
If fileImage.ShowDialog() = DialogResult.OK Then
imgArray(i, j) = CType(Bitmap.FromFile(fileImage.FileName), Bitmap)
End If
End Using
Next
Next
DialogResult = DialogResult.OK
Else
MessageBox.Show("Rows/columns entered are out of range.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Catch ex As FormatException
MessageBox.Show("Invalid rows/columns entered.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class
执行后cmdOK_Click
,整个数组imgArray
重置为空。如果我在调用表单中这样使用它:
Using sizeDialog As New MoisacDialog
If MoisacDialog.ShowDialog() = DialogResult.OK Then
Dim ImageArray(,) As Bitmap = sizeDialog.imgArray
_img = ImProc.PixelEffects.Moisac(ImageArray)
picImage.Image = CType(_img, Image)
End If
End Using
并使用调试视图,ImageArray
设置Nothing
在第三行之后,而在cmdOK_Click
.
更新:我将第二个片段的第 4 行更改为_img = ImageArray(0,0)
. 问题仍然存在,aNullReferenceException
在包含我的第二个代码段的代码中被抛出和处理。