Asp.net .NET PrivateFontCollection-完成后如何释放文件锁
我有一个返回PrivateFontCollection的函数:Asp.net .NET PrivateFontCollection-完成后如何释放文件锁,asp.net,vb.net,dispose,filelock,privatefontcollection,Asp.net,Vb.net,Dispose,Filelock,Privatefontcollection,我有一个返回PrivateFontCollection的函数: Public Shared Function GetCustomFonts() As PrivateFontCollection Dim result = New PrivateFontCollection Dim customFontFiles = {"Garamond.TTF", "Garamond-Bold.TTF", "Garamond-Italic.TTF", "EurostileExtended-Rom
Public Shared Function GetCustomFonts() As PrivateFontCollection
Dim result = New PrivateFontCollection
Dim customFontFiles = {"Garamond.TTF", "Garamond-Bold.TTF", "Garamond-Italic.TTF", "EurostileExtended-Roman-DTC.TTF"}
For Each fontFile In customFontFiles
result.AddFontFile(Hosting.HostingEnvironment.MapPath("/Includes/" & fontFile))
Next
Return result
End Function
然后,我使用以下函数:
Using customFonts = Common.GetCustomFonts()
' Do some stuff here
End Using
我希望这些文件会被释放,但它们仍然被锁定:我得到以下错误:“该操作无法完成,因为文件已在系统中打开。”。请关闭该文件,然后重试。”
关闭IIS中的网站没有帮助;我们必须回收应用程序池才能发布
有人能建议如何使用PrivateFontCollection,使文件在使用之间释放吗?作为一种解决方法,我将字体加载到内存中,并使用了“AddMemoryFont”。请参阅下面的代码。请记住,这是我第一次接触.NET中的非托管资源,所以我不能保证下面的内存管理是正常的
Imports System.Drawing.Text
Imports System.Runtime.InteropServices
Public Class CustomFontService
Implements IDisposable
Dim _fontBuffers As List(Of IntPtr)
Dim _collection As PrivateFontCollection
Public Sub New()
_collection = New PrivateFontCollection
_fontBuffers = New List(Of IntPtr)
Dim customFontFiles = {"Garamond.TTF", "Garamond-Bold.TTF", "Garamond-Italic.TTF", "EurostileExtended-Roman-DTC.TTF"}
For Each fontFile In customFontFiles
Dim fontBytes = System.IO.File.ReadAllBytes(Hosting.HostingEnvironment.MapPath("/Includes/" & fontFile))
Dim fontBuffer As IntPtr = Marshal.AllocHGlobal(fontBytes.Length)
Marshal.Copy(fontBytes, 0, fontBuffer, fontBytes.Length)
_fontBuffers.Add(fontBuffer)
_collection.AddMemoryFont(fontBuffer, fontBytes.Length)
Next
End Sub
Public Function GetCustomFonts() As PrivateFontCollection
Return _collection
End Function
#Region "IDisposable Support"
Private disposedValue As Boolean ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: dispose managed state (managed objects).
End If
For Each buf In _fontBuffers
If (buf <> IntPtr.Zero) Then
Marshal.FreeHGlobal(buf)
End If
Next
' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
' TODO: set large fields to null.
End If
Me.disposedValue = True
End Sub
' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
Protected Overrides Sub Finalize()
' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
Dispose(False)
MyBase.Finalize()
End Sub
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
导入System.Drawing.Text
导入System.Runtime.InteropServices
公共类定制服务
实现IDisposable
Dim _FONTBUFFERSAS列表(IntPtr)
Dim_集合作为PrivateFontCollection
公共分新()
_collection=新PrivateFontCollection
_fontBuffers=新列表(IntPtr)
Dim customFontFiles={“Garamond.TTF”、“Garamond Bold.TTF”、“Garamond Italic.TTF”、“EurostileExtended Roman DTC.TTF”}
对于customFontFiles中的每个fontFile
Dim fontBytes=System.IO.File.ReadAllBytes(Hosting.HostingEnvironment.MapPath(“/Includes/”&fontFile))
Dim fontBuffer As IntPtr=Marshal.AllocHGlobal(fontBytes.Length)
封送处理副本(fontBytes,0,fontBuffer,fontBytes.Length)
_添加(fontBuffer)
_collection.AddMemoryFont(fontBuffer,fontBytes.Length)
下一个
端接头
公共函数GetCustomFonts()作为PrivateFontCollection
返回集合
端函数
#区域“IDisposable支持”
Private disposedValue为布尔值,用于检测冗余呼叫
“我可以
受保护的可重写子处置(作为布尔值处置)
如果不是我的话,那么
如果是这样的话
'TODO:处置托管状态(托管对象)。
如果结束
对于每个buf In
如果(buf IntPtr.Zero),则
弗里赫全球元帅(buf)
如果结束
下一个
'TODO:释放非托管资源(非托管对象)并覆盖下面的Finalize()。
'TODO:将大字段设置为空。
如果结束
Me.disposedValue=True
端接头
'TODO:仅当上面的Dispose(ByVal disposing As Boolean)具有释放非托管资源的代码时,才重写Finalize()。
受保护的覆盖子完成()
'不要更改此代码。将清除代码放在上面的Dispose(ByVal disposing为布尔值)中。
处置(假)
MyBase.Finalize()
端接头
'此代码由Visual Basic添加,以正确实现一次性模式。
Public Sub Dispose()实现IDisposable.Dispose
'不要更改此代码。将清理代码放在上面的Dispose(以布尔值进行disposing)中。
处置(真实)
总干事(Me)
端接头
#末端区域
末级
到目前为止,我唯一的想法是将字体加载到内存中,并改用AddMemoryFont。这样我可以保证PrivateFontCollection永远不会触及文件