0

我创建了一个 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

提前致谢

4

2 回答 2

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;
    }
  }
}
于 2013-08-01T08:39:28.620 回答
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
于 2016-10-31T08:11:50.683 回答