【问题标题】:System.Drawing.Bitmap.FromFile Out of memory exception errorSystem.Drawing.Bitmap.FromFile Out of memory 异常错误
【发布时间】:2018-08-06 21:15:53
【问题描述】:

我对 .Net 了解不多。每当我尝试运行 System.Drawing.Bitmap.FromFile(imagePath) 命令时,我都会遇到内存不足异常,请您帮忙。图像大小小于 1MB,而且它们似乎没有损坏(可以使用任何查看器打开它们),我也在之后处理图像(因为我注意到人们在不同的解决方案中建议这样做)

代码如下:

If My.Computer.FileSystem.FileExists(Server.MapPath(urlToProcess)) Then
                    output.Text = output.Text & "if succeed<br>"
                    imagePath = Server.MapPath(urlToProcess)

                    output.Text = output.Text & imagePath & " "

                    bmpImg = System.Drawing.Bitmap.FromFile(imagePath)
                    imgFormat = bmpImg.RawFormat
                    itemName = Path.GetFileNameWithoutExtension(Server.MapPath(urlToProcess))

                    If imgFormat.Equals(Imaging.ImageFormat.Gif) Then
                        gifIMG = True
                        'get gif image
                        bmpImg = System.Drawing.Bitmap.FromFile(imagePath)
                        'trim away extension from end
                        trimmedImgName = Path.GetFileNameWithoutExtension(Server.MapPath(urlToProcess))

                        'Map a new path
                        urlToProcess = currWorkingDir & "/" & trimmedImgName & ".jpg"
                        imagePath = Server.MapPath(urlToProcess)

                        'save to jpeg format
                        bmpImg.Save(imagePath, System.Drawing.Imaging.ImageFormat.Jpeg)
                        itemName = trimmedImgName
                        'Need to write delete function

                    End If
                    bmpImg.Dispose()

                    If Request.QueryString("s_r") = "checked" Then
                        width = Request.QueryString("s_w")
                        height = Request.QueryString("s_h")
                        AR = Request.QueryString("sAR")
                        measureType = Request.QueryString("spxpct")
                        resizeSourceImage(urlToProcess, itemName & "_s.jpg", height, width, AR, measureType)
                        'output.Text = output.Text & urlToProcess & "<br>"
                    End If

                    If Request.QueryString("n_r") = "checked" Then
                        width = Request.QueryString("n_w")
                        height = Request.QueryString("n_h")
                        AR = Request.QueryString("nAR")
                        measureType = Request.QueryString("npxpct")
                        resizeSourceImage(urlToProcess, itemName & "_n.jpg", height, width, AR, measureType)
                        'output.Text = output.Text & "n resize<br>"
                    End If

                    If Request.QueryString("l_r") = "checked" Then
                        width = Request.QueryString("l_w")
                        height = Request.QueryString("l_h")
                        AR = Request.QueryString("lAR")
                        measureType = Request.QueryString("lpxpct")
                        resizeSourceImage(urlToProcess, itemName & "_l.jpg", height, width, AR, measureType)
                        'output.Text = output.Text & "l resize<br>"
                    End If

                    'remove temp jpg file of a gif source
                    If gifIMG Then
                        If My.Computer.FileSystem.FileExists(Server.MapPath(urlToProcess)) Then
                            My.Computer.FileSystem.DeleteFile(Server.MapPath(urlToProcess))
                        End If
                    End If
                Else
                    output.Text = output.Text & itemName & " cannot be found.<br>"
                End If

【问题讨论】:

    标签: asp.net .net bitmap


    【解决方案1】:

    System.Drawing.Bitmap 实现 IDisposable。

    试着放:

    using bmpImg = System.Drawing.Bitmap.FromFile(imagePath)
    
    
    End Using 
    

    【讨论】:

    • 我得到一个编译错误。似乎在使用 bmpImg 时“使用”是导致它的原因
    • 如果你有严格的选项,你需要写出完整的using bmpImg As Bitmap = 。另外,为避免不必要的强制转换,我建议您使用new Bitmap(imagePath) 而不是FromFile(imagePath)。在内部它做同样的事情,但是这个实际上返回一个Bitmap类型的对象而不是Image
    • @Nyerguds 实际上早先声明为“ Dim bmpImg as Bitmap ”
    • 使用变量之前从不声明。它们应该只存在于“使用”块中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2019-03-20
    • 2017-09-15
    • 2015-12-27
    相关资源
    最近更新 更多