Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,
when i loading the external font using c# language getting following error
Quote:
error:- System.Runtime.InteropServices.SEHException: 'External component has thrown an exception
Below is my code
#
private float MeasureString(string fontName, float fontSize, string text)
        {
            const float offSet = -20f;

            var fontFamily = GetFontFamily(fontName);
            var font = new Font(fontFamily, fontSize, FontStyle.Regular, GraphicsUnit.Millimeter);
            var bitmap = new Bitmap(1, 1);
            bitmap.SetResolution(96.0f, 96.0f);
            using(var graphic = Graphics.FromImage(bitmap))
            {
                graphic.PageUnit = GraphicsUnit.Millimeter;
                var size = graphic.MeasureString(text, font, new PointF(0, 0), new StringFormat(StringFormatFlags.FitBlackBox | StringFormatFlags.DirectionRightToLeft));
                return size.Width + offSet;
            }
            
        }
Loading the functions
C#
private FontFamily GetPrivateFontFamily(string fontName)
        {
            PrivateFontCollection privateFonts = new PrivateFontCollection();
            var directoryPath = _fontFamilySetting.Value.DirectoryPath;
            if (!string.IsNullOrEmpty(directoryPath) && Directory.Exists(directoryPath))
            {
                foreach (string file in Directory.EnumerateFiles(_fontFamilySetting.Value.DirectoryPath))
                {
                    privateFonts.AddFontFile(file);
                }
                return privateFonts.Families.First(f => f.Name == fontName);
            }
            return null;
        }
Can anyone please help us to solve the error

What I have tried:

Yes, i tried the code using(var graphic = Graphics.FromImage(bitmap)) statement where by using the statement automatically object release but it wont happening in my code
Posted
Updated 19-Oct-18 4:13am
v2
Comments
CHill60 19-Oct-18 10:13am    
What makes you think that the object has not been disposed - the error is referring to an exception from an external component. What does the rest of the exception say and on what line is the exception thrown?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900