使用锚定图像(来自 MemoryStream)调整表单大小会导致“System.ArgumentException"和“应用程序处于中断模式" [英] Resizing form with anchored image (from MemoryStream) causes 'System.ArgumentException' and 'The application is in break mode'

查看:33
本文介绍了使用锚定图像(来自 MemoryStream)调整表单大小会导致“System.ArgumentException"和“应用程序处于中断模式"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TcpListener,它可以获取无限的字节数组.然后,我将 byte() 转换为 MemoryStream 并提供一个 PictureBox 来显示图像.这很好用.如果我将 PictureBox 上的锚点设置为 top/right/bottom/left 这意味着当表单扩展时图像将扩展并且 然后我实际扩展表单,我得到以下错误:

I have a TcpListener which gets an endless stream of byte array. From that, I convert the byte() to MemoryStream and feed a PictureBox to display the images. That works fine. If I set the anchor points on the PictureBox to top/right/bottom/left which means the image will expand when the form expands and then I actually expand the form, I get the following error:

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

请注意,我的代码中没有捕获异常.有什么想法吗?

Please note that the exception is not caught within my code. Any ideas?

推荐答案

这个问题很可能与您处置 myimageViewImage() 方法的末尾.

The issue is most likely related to the fact that you dispose myimage at the end of your ViewImage() method.

这个:

PictureBox1.Image = myimage
...
myimage.Dispose()

使 myimagePictureBox1.Image 指向同一个 Bitmap 对象.位图是引用类型(类),这意味着当您将其分配给不同的变量时,您只是在传递它们的引用指针.

makes myimage and PictureBox1.Image point to the same Bitmap object. Bitmaps are reference types (classes) which means that you're just passing their reference pointer around when you assign it to different variables.

因此,当您处理 myimage 时,您正在处理 PictureBox 中显示的相同图像,这可能会导致当 GDI+ 尝试重新绘制拉伸的图像时出现该错误它的版本(事实上,一旦你处理了它,甚至不应该显示原始图像).

Thus when you dispose myimage you are disposing the same image that is being shown in the PictureBox, which could cause that error of yours when GDI+ tries to redraw a stretched version of it (as a matter of fact not even the original image should be displayed once you've disposed it).

有关详细信息,请参阅:值类型和引用类型 - Microsoft Docs.

For more info see: 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.

这篇关于使用锚定图像(来自 MemoryStream)调整表单大小会导致“System.ArgumentException"和“应用程序处于中断模式"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆