Use the specified font

Source: Internet
Author: User

System. Drawing. Text. privatefontcollection colfont = new system. Drawing. Text. privatefontcollection ();
Colfont. addfontfile ("test. TTF"); // mappath if you have

 

// New system. Drawing. Font (colfont. Families [0], 20, fontstyle. Regular, graphicsunit. Point)

.

 

 

Some applications, for reasons of esthetics or a required visual style, will embed certain uncommon fonts so that they are always there when needed regardless of whether the font is actually installed on the destination system.

The secret to this is twofold. first the font needs to be placed in the resources by adding it to the solution and marking it as an embedded resource. secondly, at runtime, the font is loaded via a stream and stored inPrivatefontcollectionObject for later use.

This example uses a font which is unlikely to be installed on your system. alpha dance is a free True Type font that is available from the Free Fonts collection. this font was embedded into the application by adding it to the solution and selecting the "embedded resource" build action in the properties.

 

Figure 1. The Alpha dance font file embedded in the solution.

Once the file has been successfully encoded in the resources you need to provide a privatefontcollection object in which to store it and a method by which it's loaded into the collection. the best place to do this is probably the form load override or event handler. the following listing shows the process. note how the addmemoryfont method is used. it requires a pointer to the memory in which the font is saved as an array of bytes. in C # We can use the unsafe keyword for convienience but VB must use the capabilities of the specified Al classes unmanaged memory handling. the latter option is of course open to C # programmers who just don't like the unsafe keyword.

Privatefontcollection PFC = new privatefontcollection ();

 

Private void form1_load (Object sender, system. eventargs E)

{

Stream fontstream = This. GetType (). Assembly. getmanifestresourcestream ("embedfont. alphd ___. TTF ");

 

Byte [] fontdata = new byte [fontstream. Length];

Fontstream. Read (fontdata, 0, (INT) fontstream. Length );

Fontstream. Close ();

Unsafe

{

Fixed (byte * pfontdata = fontdata)

{

PFC. addmemoryfont (system. intptr) pfontdata, fontdata. Length );

}

}

}

 

Dim PFC as new privatefontcollection ()

 

Private sub form1_load (byval sender as object, byval e as system. eventargs) handles mybase. Load

'Load the resource

Dim fontstream as stream = me. GetType (). Assembly. getmanifestresourcestream ("embedfont. VB. alphd ___. TTF ")

'Create an unsafe memory block for the data

Dim data as system. intptr = marshal. alloccotaskmem (fontstream. length)

'Create a buffer to read in

Dim fontdata () as byte

Redim fontdata (fontstream. length)

'Fetch the font program from the Resource

Fontstream. Read (fontdata, 0, fontstream. length)

'Copy the bytes to the unsafe memory block

Marshal. Copy (fontdata, 0, Data, fontstream. length)

'Pass the font to the font collection

PFC. addmemoryfont (data, fontstream. length)

'Close the resource stream

Fontstream. Close ()

'Free the unsafe memory

Marshal. freecotaskmem (data)

End sub

 

Fonts may have only certain styles which are available and unfortunately, selecting a font style that doesn't exist will throw an exception. to overcome this the font can be interrogated to see which styles are available and only those provided by the font can be used. the following listing demonstrates how the Alpha dance font is used by checking the available font styles and showing all those that exist. note that the underline and strikethrough styles are pseudo styles constructed by the font rendering engine and are not actually provided in glyph form.

Private void formatepaint (Object sender, system. Windows. Forms. painteventargs E)

{

Bool bold = false;

Bool regular = false;

Bool italic = false;

 

E. Graphics. pageunit = graphicsunit. Point;

Solidbrush B = new solidbrush (color. Black );

 

Float y = 5;

 

System. Drawing. Font FN;

 

Foreach (fontfamily FF in PFC. Families)

{

If (FF. isstyleavailable (fontstyle. Regular ))

{

Regular = true;

Fn = new font (FF, 18, fontstyle. Regular );

E. Graphics. drawstring (fn. Name, FN, B, 5, Y, stringformat. generictypographic );

FN. Dispose ();

Y + = 20;

}

If (FF. isstyleavailable (fontstyle. Bold ))

{

Bold = true;

Fn = new font (FF, 18, fontstyle. Bold );

E. Graphics. drawstring (fn. Name, FN, B, 5, Y, stringformat. generictypographic );

FN. Dispose ();

Y + = 20;

}

If (FF. isstyleavailable (fontstyle. italic ))

{

Italic = true;

Fn = new font (FF, 18, fontstyle. italic );

E. Graphics. drawstring (fn. Name, FN, B, 5, Y, stringformat. generictypographic );

FN. Dispose ();

Y + = 20;

}

If (bold & italic)

{

Fn = new font (FF, 18, fontstyle. Bold | fontstyle. italic );

E. Graphics. drawstring (fn. Name, FN, B, 5, Y, stringformat. generictypographic );

FN. Dispose ();

Y + = 20;

}

Fn = new font (FF, 18, fontstyle. Underline );

E. Graphics. drawstring (fn. Name, FN, B, 5, Y, stringformat. generictypographic );

FN. Dispose ();

Y + = 20;

Fn = new font (FF, 18, fontstyle. Strikeout );

E. Graphics. drawstring (fn. Name, FN, B, 5, Y, stringformat. generictypographic );

FN. Dispose ();

}

 

B. Dispose ();

}

 

Private sub form1_paint (byval sender as object, byval e as system. Windows. Forms. painteventargs) handles mybase. Paint

Dim bold as Boolean = false

Dim regular as Boolean = false

Dim italic as Boolean = false

 

E. Graphics. pageunit = graphicsunit. Point

Dim B as new solidbrush (color. Black)

 

Dim y = 5

 

Dim FN as system. Drawing. Font

Dim FF as fontfamily

For each FF in PFC. Families

If ff. isstyleavailable (fontstyle. Regular) then

Regular = true

Fn = new font (FF, 32, fontstyle. Regular)

E. Graphics. drawstring (fn. Name, FN, B, 5, Y, stringformat. generictypographic)

FN. Dispose ()

Y + = 40

End if

If ff. isstyleavailable (fontstyle. Bold) then

Bold = true

Fn = new font (FF, 32, fontstyle. Bold)

E. Graphics. drawstring (fn. Name, FN, B, 5, Y, stringformat. generictypographic)

FN. Dispose ()

Y + = 40

End if

If ff. isstyleavailable (fontstyle. italic) then

Italic = true

Fn = new font (FF, 32, fontstyle. italic)

E. Graphics. drawstring (fn. Name, FN, B, 5, Y, stringformat. generictypographic)

FN. Dispose ()

Y + = 40

End if

If bold and italic then

Fn = new font (FF, 32, fontstyle. Bold or fontstyle. italic)

E. Graphics. drawstring (fn. Name, FN, B, 5, Y, stringformat. generictypographic)

FN. Dispose ()

Y + = 40

End if

Fn = new font (FF, 32, fontstyle. Underline)

E. Graphics. drawstring (fn. Name, FN, B, 5, Y, stringformat. generictypographic)

FN. Dispose ()

Y + = 40

Fn = new font (FF, 32, fontstyle. Strikeout)

E. Graphics. drawstring (fn. Name, FN, B, 5, Y, stringformat. generictypographic)

FN. Dispose ()

Next

 

B. Dispose ()

End sub

 

Figure 2 shows the application in action.

 

Figure 2. The embedded Alpha dance font.

 

Private void initcustomfont ()
{
// Load the resource
Stream fontstream =
This. GetType (). Assembly. getmanifestresourcestream ("graphyx. Custom. TTF ");

// Create a buffer to read
Byte [] fontdata = new byte [fontstream. Length];

// Fetch the font program from the Resource
Fontstream. Read (fontdata, 0, (INT) fontstream. Length );

Unsafe
{
Fixed (byte * pfontdata = fontdata)
{

// Pass the font to the font collection
PFC. addmemoryfont (system. intptr) pfontdata,
(INT) fontstream. Length );
}
}
// Close the resource stream
Fontstream. Close ();

}

Private void setcustomfont ()
{
System. Drawing. Font FN;
Fontfamily ff;

FF = PFC. Families [0];

Fn = new font (FF, 16, fontstyle. Regular );

// Lblhelp. font = new system. Drawing. Font ("Courier New", 16.0f,
(Fontstyle) 2 );
Lblhelp. font = FN;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.