Home Register My Account Search Members Admin Control Panel

 


Author Message
dummy1912
Dummy1912

Posts: 17
Registered: Jun 2010
Posted June 18th, 2010 12:15 IP
Reply with quote Edit Post Delete post


Imports:

code:
<br /> Imports System<br /> Imports System.Drawing<br />


First import your .tff file in to the resource
don't forget to change the
Build Action to set on: Embedded Resource

code:
<br /> Private Shared _EmbeddedFonts As Drawing.Text.FontCollection<br /> Private Shared FontArray() As String = {"chp-fire.ttf"}<br /> <br /> Dim FntFC As Drawing.Text.PrivateFontCollection = GetFont(FontArray)<br />


code:
<br /> #Region " Load Font From Recource "<br /> Private Declare Auto Function AddFontMemResourceEx Lib "Gdi32.dll" _<br /> (ByVal pbFont As IntPtr, ByVal cbFont As Integer, _<br /> ByVal pdv As Integer, ByRef pcFonts As Integer) As IntPtr<br /> <br /> Public Function GetFont(ByVal FontResource() As String) As _<br /> Drawing.Text.PrivateFontCollection<br /> 'Get the namespace of the application <br /> Dim NameSpc As String = _<br /> Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString()<br /> Dim FntStrm As IO.Stream<br /> Dim FntFC As New Drawing.Text.PrivateFontCollection<br /> Dim i As Integer<br /> For i = 0 To FontResource.GetUpperBound(0)<br /> 'Get the resource stream area where the font is located<br /> FntStrm = _<br /> Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream( _<br /> NameSpc + "." + FontResource(i))<br /> 'Load the font off the stream into a byte array<br /> Dim ByteStrm(CType(FntStrm.Length, Integer)) As Byte<br /> FntStrm.Read(ByteStrm, 0, Int(CType(FntStrm.Length, Integer)))<br /> 'Allocate some memory on the global heap<br /> Dim FntPtr As IntPtr = _<br /> Runtime.InteropServices.Marshal.AllocHGlobal( _<br /> Runtime.InteropServices.Marshal.SizeOf(GetType(Byte)) * _<br /> ByteStrm.Length)<br /> 'Copy the byte array holding the font into the allocated memory.<br /> Runtime.InteropServices.Marshal.Copy(ByteStrm, 0, _<br /> FntPtr, ByteStrm.Length)<br /> 'Add the font to the PrivateFontCollection<br /> FntFC.AddMemoryFont(FntPtr, ByteStrm.Length)<br /> Dim pcFonts As Int32<br /> pcFonts = 1<br /> AddFontMemResourceEx(FntPtr, ByteStrm.Length, 0, pcFonts)<br /> 'Free the memory<br /> Runtime.InteropServices.Marshal.FreeHGlobal(FntPtr)<br /> Next<br /> Return FntFC<br /> End Function<br /> #End Region<br />


To load the font.
if you wanna use a label font

code:
<br /> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<br /> Label1.Font = New Font(FntFC.Families(0), 45, FontStyle.Bold, GraphicsUnit.Point)<br /> <br /> End Sub<br />


to set the main font

code:
<br /> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<br /> me.Font = New Font(FntFC.Families(0), 45, FontStyle.Bold, GraphicsUnit.Point)<br /> <br /> End Sub<br />
 




Posts:
Registered:
Posted IP
Reply with quote Edit Post Delete post


ian-enterprise-corp :: Main :: Help & Tutorials :: How to load a font from a resource? - Visual Basic.Net
< Previous thread | Next thread > | Subscribe to thread |
Mark all forums read
Logout
All times are GMT
Forum jump:
Thread Options:
Delete thread / Open/Close thread / Rename thread / Stick thread / Move thread / Merge thread