2014-10-29 6 views
6

मैं एक समारोह जो एक PrivateFontCollection देता है:नेट PrivateFontCollection - कैसे फ़ाइल ताले को रिहा करने का काम पूरा हो जाने

Using customFonts = Common.GetCustomFonts() 
    ' Do some stuff here 
End Using 

मैं उम्मीद करेंगे कि:

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 

इस प्रकार मैं तो फ़ंक्शन का उपयोग करें फाइलें जारी की जाएंगी, लेकिन वे अभी भी लॉक हैं: मुझे निम्न त्रुटि मिलती है: 'कार्रवाई पूरी नहीं की जा सकती क्योंकि फ़ाइल सिस्टम में खुली है। फ़ाइल बंद करें और पुनः प्रयास करें। '

आईआईएस में वेबसाइट को बंद करने से मदद नहीं मिलती है; इसे रिलीज करने के लिए हमें ऐप पूल रीसायकल करना होगा।

क्या कोई भी इस बारे में सलाह दे सकता है कि निजी फ़ॉन्टकोलेक्शन का उपयोग इस तरह से किया जाता है कि फ़ाइलों को उपयोग के बीच में जारी किया जाता है?

+0

मेरा एकमात्र विचार अब तक फ़ॉन्ट को स्मृति में लोड करना है और इसके बजाय AddMemoryFont का उपयोग करना है। इस तरह से मैं गारंटी दे सकता हूं कि PrivateFontCollection फ़ाइलों को कभी छूता नहीं है। – extremeandy

+0

एक संभावित उत्तर के लिए http://stackoverflow.com/questions/26671026/how-to-delete-the-file-of-a-privatefontcollection-addfontfile – Horcrux7

+0

जोड़ा गया कनेक्ट बग https://connect.microsoft.com/ VisualStudio/प्रतिक्रिया/विवरण/1379843 – Peter

उत्तर

1

कामकाज के रूप में, मैंने फ़ॉन्ट को स्मृति में लोड किया और इसके बजाय '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 
संबंधित मुद्दे