内存不足异常System.Drawing.Image.FromFile

时间:2015-02-09 20:22:42

标签: c# asp.net vb.net

这是我的问题:我尝试使用服务器端代码打开文件。

该文件为16,096KB,它是一个rar文件。

我还试图打开一个7.14KB的文件,它是一个图像文件。

但是当我遇到这行代码时,它会抛出错误 " Out of Memeory"

Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(m_IRAttachmentsPath + AttachmentFileName)

这是我的代码:

 If e.CommandName.ToString() = "OpenFile" Then
                Dim IRAttachmentsPath As String = Nothing
                Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(m_IRAttachmentsPath + AttachmentFileName)
                Dim bytes As Byte()
                Using ms1 As New MemoryStream()
                    img.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg)
                    bytes = ms1.ToArray()
                End Using

                Dim memStream As New MemoryStream()
                Dim Package As ZipPackage = ZipPackage.Create(memStream)

                Dim stream As Stream = New MemoryStream(bytes)
                Package.AddStream(stream, AttachmentFileName)
                SendZipToClient(memStream, Package)
End If



 Private Sub SendZipToClient(memStream As MemoryStream, Package As ZipPackage)
        Package.Close(False)
        memStream.Position = 0
        If memStream IsNot Nothing AndAlso memStream.Length > 0 Then
            Response.Clear()
            Response.AddHeader("content-disposition", "attachment; filename=photos.zip")
            Response.ContentType = "application/zip"
            Response.BinaryWrite(memStream.ToArray())
            Response.[End]()
        End If

    End Sub

0 个答案:

没有答案