Asked by:
PrivateFontCollection freezes the application and return Error in w3wp.exe

Question
-
User-553915629 posted
Hi,
We are creating multiple charts as custom web control dynamically, using the below code. If use system font like 'Arial' (installed font), it creates charts well. But we need to load the font from file, for this we are using PrivateFontCollection.
The Code is:
protected override void CreateChildControls() { Font font; string strFontFile = @"c:\temp\Source sans pro.ttf"; using(PrivateFontCollection fonts = new PrivateFontCollection()) { fonts.AddFontFile(strFontFile); FontFamily fontFamily = new FontFamily(fonts.Families.FirstOrDefault()?.Name, fonts); font = new Font(fontFamily, 8); } _chart = new System.Web.UI.DataVisualization.Charting.Chart(); // Chart legent creation Legend legend = new Legend { BackColor = Color.Transparent, BorderColor = Color.Transparent, LegendStyle = this.LegendStyle, Docking = this.Docking, Font = font, TextWrapThreshold = this.LegendTextWrapThreshold }; _chart.Legends.Add(legend); this.Controls.Add(_chart); }
We tried to dispose the 'PrivateFontCollection' still we have these errors. But this error doesn't happenfrequently.
Please suggest, how do we use 'PrivateFontCollection' in ASP custom webcontrol?
Thanks
Monday, September 24, 2018 8:52 AM
All replies
-
User475983607 posted
You have a few conceptual design bugs....
First, ttf fonts are installed on the client machine. You can't load the font in code on the server. It worked on your development machine because it was both the client and server.
https://www.w3schools.com/CSSref/css3_pr_font-face_rule.asp
Second, the pdb error is due to debugging a release build where the debug symbol files (pdb) do not exist.
Monday, September 24, 2018 11:36 AM -
User-553915629 posted
Hi @mgebhard,
First, ttf are not installed in our machine so we use 'PrivateFontCollection' to load the font dynamically and by this we achieve the font. Additionally the control is created in server side only. That works well. But if we create the controls multiple time -(dynamically) , it shows the error.
We suspicious on the 'PrivateFontCollection' dispose.
In which order we have to dispose / in which phase of 'ASP page life cycle' we have to dispose/ Or we never dispose that.We confused. Could u suggest how to solve this ?
Monday, September 24, 2018 12:48 PM -
User475983607 posted
The PrivateFontCollection is a Windows application API not ASP.NET. As far as I know, you must create a linked file reference as recommended in my previous linked docs.
Monday, September 24, 2018 1:20 PM -
User-553915629 posted
This is the error shown during the chart rendering(for the chart creation we use above code).
How the Privatefontcollection works here?
Tuesday, September 25, 2018 5:06 AM -
User283571144 posted
Hi VJ529992,
As far as I know, we could use PrivateFontCollection to add custom font in the chat.
But I couldn't directly find out why the your server control show this error.
Could you please share how you define the server control?
How you define the _chart?
Besides, I also created a test demo with using the PrivateFontCollection in pageload event, it works well.
More details about my test demo ,you could refer to below codes:
<div> <asp:Chart ID="Chart1" runat="server" Width="1061px"> <Series> <asp:Series Name="Series1"> </asp:Series> <asp:Series Name="Series2"> </asp:Series> </Series> <ChartAreas> <asp:ChartArea Name="ChartArea1"> </asp:ChartArea> </ChartAreas> </asp:Chart> </div>
Code-behind:
protected void Page_Load(object sender, EventArgs e) { Font font; string strFontFile = @"D:\Generation September.ttf"; using (PrivateFontCollection fonts = new PrivateFontCollection()) { fonts.AddFontFile(strFontFile); FontFamily fontFamily = new FontFamily(fonts.Families.FirstOrDefault()?.Name, fonts); font = new Font(fontFamily, 8); } DataTable dt = new DataTable(); dt.Columns.Add("Date", typeof(DateTime)); dt.Columns.Add("Num"); dt.Rows.Add("1/12/2007", 140); dt.Rows.Add("1/15/2008", 150); dt.Rows.Add("7/1/2008", 120); dt.Rows.Add("11/15/2015", 200); dt.Rows.Add("12/31/2018", 130); Chart1.DataSource = dt; Chart1.Titles.Add("Total No.: aaaa"); Chart1.Titles[0].Alignment = ContentAlignment.MiddleCenter; Chart1.Titles[0].Font = font; Chart1.Series["Series1"].XValueMember = "Date"; Chart1.Series["Series1"].YValueMembers = "Num"; Chart1.Series["Series1"].ChartType = SeriesChartType.Bar; Chart1.Series["Series1"].Label = "#VALX"; Chart1.Series["Series1"].ChartArea = "ChartArea1"; Chart1.Series["Series2"].XValueMember = "Date"; Chart1.Series["Series2"].YValueMembers = "Num"; Chart1.Series["Series2"].ChartType = SeriesChartType.Bar; Chart1.Series["Series2"].Label = "#VALX"; Chart1.Series["Series2"].ChartArea = "ChartArea1"; Chart1.ChartAreas[0].Position.X = 10; Chart1.ChartAreas[0].Position.Y = 10; Chart1.ChartAreas[0].Position.Width = 40; Chart1.ChartAreas[0].Position.Height = 80; Chart1.ChartAreas[0].BackColor = Color.Transparent; // Chart legent creation Legend legend = new Legend { BackColor = Color.Transparent, BorderColor = Color.Transparent, Font = font, }; Chart1.Legends.Add(legend); Chart1.DataBind(); }
Result:
Best Regards,
Brando
Tuesday, September 25, 2018 6:23 AM