Share via

FontStream is not added properly to the PrivateFontCollection when more iterations are performed.

Lokesh Baskar 0 Reputation points
2023-06-26T10:21:17.2533333+00:00

We loaded three different font streams into a PrivateFontCollection, printed the names of each font family, and counted the number of fonts once after adding all the font streams.

We repeated this process 10 times. When adding these font streams continuously to the PrivateFontCollection for each iteration, sometimes a font was missing from the collection. For example, the collection sometimes showed only two fonts added.

Please provide me with a solution to have a consistent output using PrivateFontCollection.

Note: Our requirement is to add font stream into the private font collection.

Code snippet:

|string[] fontList = new string[] { @"arial.ttf", @"calibri.ttf", @"verdana.ttf" };
PrivateFontCollection fontCollection = null;
for (int i = 0; i < 10; i++)
{
 
fontCollection = new PrivateFontCollection();
foreach (string fontFile in fontList)
{
 
if (!File.Exists(fontFile)) continue;
Console.WriteLine("Font name: " + fontFile);
Stream fontStream = File.OpenRead(fontFile);
fontStream.Position = 0;
byte[] fontdata = new byte[fontStream.Length];
fontStream.Position = 0;
//read the font data from the ttf stream
fontStream.Read(fontdata,0, (int)fontStream.Length);
IntPtr data = Marshal.AllocCoTaskMem((int)fontStream.Length);
Marshal.Copy(fontdata,0, data, (int)fontStream.Length);
fontCollection.AddMemoryFont(data,(int)fontStream.Length);
}
Console.WriteLine("Total Number of fonts added in the
PrivateFontCollection: " + fontCollection.Families.Length);
for (int k = 0; k < fontCollection.Families.Length; k++)
Console.WriteLine(i

  • " Iteration and
    families Name: " + k +
    " - " + fontCollection.Families[k].Name);
    fontCollection.Dispose();
    fontCollection
    = null;
    }| | -------- | ||

Actual Output:

 

Total Number of fonts added in the PrivateFontCollection: 3

0 Iteration and families Name: 0 - Arial

0 Iteration and families Name: 1 - Calibri

0 Iteration and families Name: 2 - Verdana

 

Total Number of fonts added in the PrivateFontCollection: 3

1 Iteration and families Name: 0 - Arial

1 Iteration and families Name: 1 - Calibri

1 Iteration and families Name: 2 - Verdana

 

Total Number of fonts added in the PrivateFontCollection: 3

2 Iteration and families Name: 0 - Arial

2 Iteration and families Name: 1 - Calibri

2 Iteration and families Name: 2 - Verdana

 

Total Number of fonts added in the PrivateFontCollection: 3

3 Iteration and families Name: 0 - Arial

3 Iteration and families Name: 1 - Calibri

3 Iteration and families Name: 2 - Verdana

 

Total Number of fonts added in the PrivateFontCollection: 3

4 Iteration and families Name: 0 - Arial

4 Iteration and families Name: 1 - Calibri

4 Iteration and families Name: 2 - Verdana

 

Total Number of fonts added in the PrivateFontCollection: 3

5 Iteration and families Name: 0 - Arial

5 Iteration and families Name: 1 - Calibri

5 Iteration and families Name: 2 - Verdana

 

Total Number of fonts added in the PrivateFontCollection: 3

6 Iteration and families Name: 0 - Arial

6 Iteration and families Name: 1 - Calibri

6 Iteration and families Name: 2 - Verdana

 

Total Number of fonts added in the PrivateFontCollection: 3

7 Iteration and families Name: 0 - Arial

7 Iteration and families Name: 1 - Calibri

7 Iteration and families Name: 2 - Verdana

 

Total Number of fonts added in the PrivateFontCollection: 2

8 Iteration and families Name: 0 - Calibri

8 Iteration and families Name: 1 - Verdana

 

Total Number of fonts added in the PrivateFontCollection: 2

9 Iteration and families Name: 0 - Arial

9 Iteration and families Name: 1 - Calibri

 

 

Expected output:

Total Number of fonts added in the PrivateFontCollection: 3

0 Iteration and families Name: 0 - Arial

0 Iteration and families Name: 1 - Calibri

0 Iteration and families Name: 2 - Verdana

 

Total Number of fonts added in the PrivateFontCollection: 3

1 Iteration and families Name: 0 - Arial

1 Iteration and families Name: 1 - Calibri

1 Iteration and families Name: 2 - Verdana

 

Total Number of fonts added in the PrivateFontCollection: 3

2 Iteration and families Name: 0 - Arial

2 Iteration and families Name: 1 - Calibri

2 Iteration and families Name: 2 - Verdana

 

Total Number of fonts added in the PrivateFontCollection: 3

3 Iteration and families Name: 0 - Arial

3 Iteration and families Name: 1 - Calibri

3 Iteration and families Name: 2 - Verdana

 

Total Number of fonts added in the PrivateFontCollection: 3

4 Iteration and families Name: 0 - Arial

4 Iteration and families Name: 1 - Calibri

4 Iteration and families Name: 2 - Verdana

 

Total Number of fonts added in the PrivateFontCollection: 3

5 Iteration and families Name: 0 - Arial

5 Iteration and families Name: 1 - Calibri

5 Iteration and families Name: 2 - Verdana

 

Total Number of fonts added in the PrivateFontCollection: 3

6 Iteration and families Name: 0 - Arial

6 Iteration and families Name: 1 - Calibri

6 Iteration and families Name: 2 - Verdana

 

Total Number of fonts added in the PrivateFontCollection: 3

7 Iteration and families Name: 0 - Arial

7 Iteration and families Name: 1 - Calibri

7 Iteration and families Name: 2 - Verdana

 

Total Number of fonts added in the PrivateFontCollection: 3

8 Iteration and families Name: 0 - Arial

8 Iteration and families Name: 1 - Calibri

8 Iteration and families Name: 2 - Verdana

 

Total Number of fonts added in the PrivateFontCollection: 3

9 Iteration and families Name: 0 - Arial

9 Iteration and families Name: 1 - Calibri

9 Iteration and families Name: 2 - Verdana

 

 

Developer technologies | Windows Forms
Developer technologies | .NET | Other
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 91,741 Reputation points
    2023-06-26T11:38:06.1466667+00:00

    Please provide me with a solution to have a consistent output using PrivateFontCollection.

    This works for me with AddFontFile :

                    string[] fontList = new string[] { @"arial.ttf", @"calibri.ttf", @"verdana.ttf" };
                    PrivateFontCollection fontCollection = null;
                    for (int i = 0; i < 10; i++)
                    {
                        fontCollection = new PrivateFontCollection();
                        foreach (string fontFile in fontList)
                        {
                            if (!File.Exists(fontFile)) continue;
                            fontCollection.AddFontFile(fontFile);
                        }
                        Console.WriteLine("Total Number of fonts added in the PrivateFontCollection: " + fontCollection.Families.Length);
                        for (int k = 0; k < fontCollection.Families.Length; k++)
                            Console.WriteLine(i.ToString() + " Iteration and families Name: " + k + " - " + fontCollection.Families[k].Name);
                        fontCollection.Dispose();
                        fontCollection = null;
                    } 
    
    1 person found this answer helpful.
    0 comments No comments

  2. Lokesh Baskar 0 Reputation points
    2023-07-13T08:22:07.5466667+00:00

    Hi @Anonymous ,
    I apologize for the delay in responding. Thank you for the update.
    We should not dispose the font collection for each iteration. If we do not, there could be a memory spike or the memory usage could increase.
    Our requirement is not to hold the font collection in the list(mylist.Add(fontCollection).
    We need to add fontstream data to the fontCollection instead of using AddFontFile.
    So, it is possible to resolve the problem using the GDI+?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.