解析字体的信息,并将其转换为System.Drawing.Font(Parsing font inf

2019-08-17 07:26发布

我有一个包含这样的字体信息的文本文件:

宋体,12.5

......我需要阅读这些信息到label.Font是这样的:

label.Font =新字体(fontNameFields [0],Single.Parse(fontNameFields [1]));

...但我总是得到以下错误:

指数数组的边界之外。

可有人请帮助我?

感谢杰森。

代码我有:

    MatchCollection lines = Regex.Matches(File.ReadAllText(Path), @"(.+?)\r\n""([^""]+)""\r\n(\d+), (\d+)");
    foreach (Match match in lines)
    {
        string control = match.Groups[1].Value;
        string text = match.Groups[2].Value;
        int x = Int32.Parse(match.Groups[3].Value);
        int y = Int32.Parse(match.Groups[4].Value);
        String cfont = match.Groups[5].Value;

            String fontName = cfont;
            String[] fontNameFields = fontName.Split(',');


            label.Font = new Font(fontNameFields[0], Single.Parse(fontNameFields[1]));
}

堆栈跟踪


   at Tabbed.Form1.FillCanvas() in C:\Documents and Settings\jay\My Documents\Visual Studio 2008\Projects\MYPROGGY\ MYPROGGY\Form1.cs:line 574
   at Tabbed.Form1.openbtn_Click(Object sender, EventArgs e) in C:\Documents and Settings\jay\My Documents\Visual Studio 2008\Projects\ MYPROGGY\ MYPROGGY\Form1.cs:line 802
   at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
   at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
   at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
   at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at Tabbed.Program.Main(String[] args) in C:\Documents and Settings\jay\My Documents\Visual Studio 2008\Projects\ MYPROGGY\ MYPROGGY\Program.cs:line 27
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Answer 1:

我怀疑你正在运行到不包含逗号线。 您的样本数据好工作:

using System;

class Test
{
    static void Main(string[] args)
    {
        String fontName = "Arial, 12.5";
        String[] fontNameFields = fontName.Split(',');
        String name = fontNameFields[0];
        float size = float.Parse(fontNameFields[1]);
        Console.WriteLine("{0}: {1}", name, size);
    }
}

登录(或在对话框弹出) fontName只是尝试创建字体之前,我敢肯定,会告诉你这是怎么回事。



Answer 2:

难道是你的格式包含了一个奇怪的逗号,或者根本就没有?

也可能你在标签上提供的代码,就好像它是一个System.Web.UI.WebControls然后字体并没有一个二传手。

飞碟双向的代码上面给定的输入工作正常。 你的问题不在于此。



Answer 3:

乍一看代码看起来OK。

你有没有检查了fontName是你所期望它是(即“宋体,12.5”),什么fontNameFields调用后包含fontName.Split

fontNameFields[0] -> "Arial"
fontNameFields[1] -> "12.5"

在没有看到实际值我不希望暗示任何东西。

编辑

从你更新的代码我建议,这是你的正则表达式多数民众赞成失败。 尝试打印出来的内容match.Groups阵列,以检查是否已正确解析输入。



文章来源: Parsing font info and converting it to System.Drawing.Font
标签: c# fonts parsing