Resizing form with anchored image (from MemoryStream) causes 'System.ArgumentException' and 'The application is in break mode'

前端 未结 1 869
清酒与你
清酒与你 2021-01-26 05:22

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 i

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-26 06:22

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

    This:

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

    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.

    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).

    For more info see: Value Types and Reference Types - Microsoft Docs.


    Basic example of how Reference Types and Value Types work

    Reference types:

    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.
    

    Value types:

    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.
    

    0 讨论(0)
提交回复
热议问题