Hello. I have a problem while changing font for label, my font is not ttf so i googled for something to change font. I found this thread: http://www.vbforums.com/showthread.php?t=425218 And i took this code:
On form load:Code:Dim EmbedFonts As New PrivateFontCollection() Private Declare Auto Function AddFontMemResourceEx Lib "Gdi32.dll" (ByVal pbFont As IntPtr, ByVal cbFont As Integer, ByVal pdv As Integer, ByRef pcFonts As Integer) As IntPtr Public Function GetFont(ByVal FontResource() As String) As Drawing.Text.PrivateFontCollection Dim NameSpc As String = _ Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString() Dim FntStrm As IO.Stream Dim FntFC As New Drawing.Text.PrivateFontCollection() Dim i As Integer For i = 0 To FontResource.GetUpperBound(0) FntStrm = _ Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(NameSpc + "." + FontResource(i)) Dim ByteStrm(CType(FntStrm.Length, Integer)) As Byte FntStrm.Read(ByteStrm, 0, Int(CType(FntStrm.Length, Integer))) Dim FntPtr As IntPtr = _ Runtime.InteropServices.Marshal.AllocHGlobal( _ Runtime.InteropServices.Marshal.SizeOf(GetType(Byte)) * ByteStrm.Length) Runtime.InteropServices.Marshal.Copy(ByteStrm, 0, FntPtr, ByteStrm.Length) FntFC.AddMemoryFont(FntPtr, ByteStrm.Length) Dim pcFonts As Int32 pcFonts = 1 AddFontMemResourceEx(FntPtr, ByteStrm.Length, 0, pcFonts) Runtime.InteropServices.Marshal.FreeHGlobal(FntPtr) Next Return FntFC End Function
And i added my font to embed. Here:Code:Dim FontResources(0) As String FontResources(0) = "staravenue.ttf" EmbedFonts = GetFont(FontResources) Dim VDFont As New Font(EmbedFonts.Families(0), 31, FontStyle.Regular) Label1.Font = VDFont Label2.Font = VDFont
But when i try to debug my program, i getting this error: Object reference not set to an instance of an object. For this line: Dim ByteStrm(CType(FntStrm.Length, Integer)) As Byte
In that thread where i took the code i seen this post:
But how i can get namespace manually ? If this thing really works, getting namespace manually.I GOT IT!!! there is an error in the coding, here:
Code:
Dim NameSpc As String = _
Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString()
this line does not return the namespace...it returns the assembly name... sometimes the assembly name and namespace are same, thats why, it works...
we have to set the namespace manually...or maybe by other way!





Reply With Quote