观看1200 Dpi Tiff图像时GDI +中发生一般错误

时间:2013-07-31 08:15:52

标签: asp.net vb.net gdi+ tiff

我创建了一个tiff查看器,它已经可以查看tiff图像了。除非它的分辨率高达1200 DPI。我的代码中是否有导致问题的解决方法?

 Public Function GetTiffImage(path As String, page As Integer) As Image


        Dim ms As MemoryStream = Nothing
        Dim SrcImg As Image = Nothing
        Dim returnImage As Image = Nothing

        Try
            SrcImg = Image.FromFile(path)
            ms = New MemoryStream()
            Dim FrDim As New FrameDimension(SrcImg.FrameDimensionsList(0))
            SrcImg.SelectActiveFrame(FrDim, Page)
            SrcImg.Save(ms, ImageFormat.Tiff)
            returnImage = Image.FromStream(ms)
        Catch ex As Exception
            Throw ex
        Finally
            SrcImg.Dispose()
            GC.Collect()
            GC.WaitForPendingFinalizers()
        End Try

        Return returnImage
    End Function

提前致谢

2 个答案:

答案 0 :(得分:1)

根据我的经验,此错误消息具有误导性,通常由OutOfMemoryException引起。我打赌你因为没有清理所有IDisposable对象而失去记忆力。我知道GC.Collect()不一定是可靠的方法。尝试放入一些using块。我使用C#而不是VBScript,因此无法提供确切的语法,但在C#中它是这样的:

using (var SrcImg = Image.FromFile(path))
{
  using (var ms = MemoryStream())
  {
    var FrDim = new FrameDimension(SrcImg.FrameDimensionsList(0));
    SrcImg.SelectActiveFrame(FrDim, Page)
    SrcImg.Save(ms, ImageFormat.Tiff)
    using (var returnImage = Image.FromStream(ms))
    {
      return returnImage;
    }
  }
}

答案 1 :(得分:0)

处理图像后需要释放的内存以防止内存泄漏。 这个基于代码的样本代码已被修改以防止内存泄漏。

Public Function GetTiffImage(path As String, page As Integer) As Image
    Using ms As New MemoryStream
        Using SrcImg As Image = Image.FromFile(path)
            Using returnImage As Image = Image.FromStream(ms)
                Try
                    Dim FrDim As New FrameDimension(SrcImg.FrameDimensionsList(0))
                    SrcImg.SelectActiveFrame(FrDim, page)
                    SrcImg.Save(ms, ImageFormat.Tiff)
                Catch ex As Exception
                    Throw ex
                End Try
                Return returnImage
            End Using
        End Using
    End Using
End Function