System.Drawing Out of Memory Exception

前端 未结 5 1276
深忆病人
深忆病人 2020-11-28 15:43

My application graphics engine throws these exceptions. They are all consumed by an empty catch block. In the early days I found one that was not trapped (associated with

5条回答
  •  清歌不尽
    2020-11-28 15:58

    I tried the solution 'Joe White' suggested and that was it. It threw a OutOfMemoryException because the rectangle's width and height were 0. In my case the window was minimized when the exception occurred.

    Here is an example;

      Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    
        Using e
            ##Add conditions to avoid OutOfMemoryException##
            If (Not ClientRectangle.Width = 0) And (Not ClientRectangle.Height = 0) Then
                Using rect As GraphicsPath = New GraphicsPath()
                    rect.AddRectangle(ClientRectangle)
    
                    Using gb As New PathGradientBrush(rect)
                        gb.WrapMode = WrapMode.Tile
                        gb.SurroundColors = New Color() {GradientColors(1), GradientColors(0), GradientColors(2)}
                        gb.CenterColor = GradientColors(0)
                        gb.SetSigmaBellShape(0.5F)
                        e.Graphics.FillPath(gb, rect)
                    End Using
                End Using
            End If
        End Using
    
    End Sub
    

提交回复
热议问题