首页 > 解决方案 > 在 Richtextbox 中保存图像时 OutOfMemory

问题描述

我正在使用 Richtextbox 作为注释字段,它使用户能够添加图像

现在我在保存包含分辨率为 4800x2800 的图像的字段时出现内存不足错误,

调试时 rtfText = Me.txtNotes.Rtf 发生错误

“在 mscorlib.dll 中发生了“System.OutOfMemoryException”类型的未处理异常”

尺寸有限制吗?我打算将图像限制为 1920x1080 但不确定如何,因为它会在我做任何事情之前返回 OOM 错误(我认为它来自richtextbox.rtf)

如果我能得到帮助来限制可以添加的图像分辨率,将不胜感激

Private Function ValidateRichTextImage() As Boolean
    Dim rtfText As String
    Dim width As Integer
    Dim height As Integer


    rtfText = Me.txtNotes.Rtf

    Dim widthRegex As New Regex("picwgoal[\d]+")
    Dim heightRegex As New Regex("pichgoal[\d]+")
    Dim widthmatch = widthRegex.Match(rtfText)
    If widthmatch.success Then
        Dim swidth As String = widthmatch.ToString.Replace("picwgoal", "")
        width = Integer.Parse(swidth) / 15
    End If

    Dim heightmatch = heightRegex.Match(rtfText)
    If heightmatch.success Then
        Dim sheight As String = heightmatch.ToString.Replace("pichgoal", "")
        height = Integer.Parse(sheight) / 15
    End If

    If rtfText.Contains("picwgoal") AndAlso width = 0 AndAlso height = 0 Then
        Me.Errors.Add("P1MMO0001", , Tb.Errors.ErrorTypeType.ErrorTypeError, , )
        Return False
    End If

    If width >= height Then 'landscape
        If width > mcImageWidth OrElse height > mcImageHeight Then
            Me.Errors.Add("P1MMO0001", , Tb.Errors.ErrorTypeType.ErrorTypeError, , )
            Return False
        End If
    Else 'portrait
        If width > mcImageHeight OrElse height > mcImageWidth Then
            Me.Errors.Add("P1MMO0001", , Tb.Errors.ErrorTypeType.ErrorTypeError, , )
            Return False
        End If
    End If

    Return True
End Function

标签: vb.netimageout-of-memoryrichtextbox

解决方案


推荐阅读