使用锚定图像(来自 MemoryStream)调整表单大小会导致 'System.ArgumentException' 和 'The application is in break mode'
Resizing form with anchored image (from MemoryStream) causes 'System.ArgumentException' and 'The application is in break mode'
我有一个 TcpListener
得到源源不断的 byte array
。由此,我将 byte() 转换为 MemoryStream 并提供一个 PictureBox 来显示图像。那很好用。
如果我将 PictureBox 上的锚点设置为 top/right/bottom/left
,这意味着图像将在表单展开时展开,然后 然后我实际展开 表单,我会收到以下错误:
An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll
Additional information: Parameter is not valid.
此外,
The application is in break mode
代码
' Enter the listening loop.
While True
Dim client As TcpClient = Await server.AcceptTcpClientAsync()
Dim stream As NetworkStream = client.GetStream
Dim MS As New MemoryStream
Await stream.CopyToAsync(MS)
Await ViewImage(MS)
client.Close()
End While
查看图片功能:
Public Async Function ViewImage(ms As MemoryStream) As Task
Try
Dim myimage As Bitmap
myimage = New Bitmap(ms)
PictureBox1.Image = myimage
PictureBox1.Refresh()
myimage.Dispose()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Function
请注意,我的代码中没有捕获异常。有什么想法吗?
这个问题很可能与您 dispose myimage
在 ViewImage()
方法。
这个:
PictureBox1.Image = myimage
...
myimage.Dispose()
使myimage
和PictureBox1.Image
指向同一个Bitmap
对象。位图是引用类型 (类),这意味着当您将其分配给不同的变量时,您只是在传递它们的引用指针。
因此,当您处理 myimage
时,您处理的是 PictureBox
中显示的相同图像,当 GDI+ 尝试重绘它的拉伸版本时,这可能会导致您的错误(事实上,即使是原始图像也不会在您处理后显示出来。
有关详细信息,请参阅:Value Types and Reference Types - Microsoft Docs。
引用类型和值类型如何工作的基本示例
引用类型:
Dim A As New Bitmap("image.bmp")
Dim B As Bitmap = A 'Points to A.
PictureBox1.Image = A 'Points to A.
PictureBox2.Image = B 'Still points to A.
'Calling Dispose() on any of the above will result in none of them having an image since you will ultimately dispose bitmap A.
值类型:
Dim A As Integer = 3
Dim B As Integer = A 'Copy of A.
Dim C As Integer = B 'Copy of B.
C = 4 'Results in A = 3, B = 3, C = 4.
我有一个 TcpListener
得到源源不断的 byte array
。由此,我将 byte() 转换为 MemoryStream 并提供一个 PictureBox 来显示图像。那很好用。
如果我将 PictureBox 上的锚点设置为 top/right/bottom/left
,这意味着图像将在表单展开时展开,然后 然后我实际展开 表单,我会收到以下错误:
An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll
Additional information: Parameter is not valid.
此外,
The application is in break mode
代码
' Enter the listening loop.
While True
Dim client As TcpClient = Await server.AcceptTcpClientAsync()
Dim stream As NetworkStream = client.GetStream
Dim MS As New MemoryStream
Await stream.CopyToAsync(MS)
Await ViewImage(MS)
client.Close()
End While
查看图片功能:
Public Async Function ViewImage(ms As MemoryStream) As Task
Try
Dim myimage As Bitmap
myimage = New Bitmap(ms)
PictureBox1.Image = myimage
PictureBox1.Refresh()
myimage.Dispose()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Function
请注意,我的代码中没有捕获异常。有什么想法吗?
这个问题很可能与您 dispose myimage
在 ViewImage()
方法。
这个:
PictureBox1.Image = myimage
...
myimage.Dispose()
使myimage
和PictureBox1.Image
指向同一个Bitmap
对象。位图是引用类型 (类),这意味着当您将其分配给不同的变量时,您只是在传递它们的引用指针。
因此,当您处理 myimage
时,您处理的是 PictureBox
中显示的相同图像,当 GDI+ 尝试重绘它的拉伸版本时,这可能会导致您的错误(事实上,即使是原始图像也不会在您处理后显示出来。
有关详细信息,请参阅:Value Types and Reference Types - Microsoft Docs。
引用类型和值类型如何工作的基本示例
引用类型:
Dim A As New Bitmap("image.bmp")
Dim B As Bitmap = A 'Points to A.
PictureBox1.Image = A 'Points to A.
PictureBox2.Image = B 'Still points to A.
'Calling Dispose() on any of the above will result in none of them having an image since you will ultimately dispose bitmap A.
值类型:
Dim A As Integer = 3
Dim B As Integer = A 'Copy of A.
Dim C As Integer = B 'Copy of B.
C = 4 'Results in A = 3, B = 3, C = 4.