解析字体信息,并将其转换为System.Drawing.Font [英] Parsing font info and converting it to System.Drawing.Font

查看:398
本文介绍了解析字体信息,并将其转换为System.Drawing.Font的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



宋体,12.5



.. 。而我需要阅读这些信息进入label.Font是这样的:



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



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



指数的边界之外该数组。



有人可以帮我这个?



由于
杰森。



代码我有:

  MatchCollection线= Regex.Matches(文件。 ReadAllText(路径),@\r\\\
(+?),([^] +),\r\\\
(\d +),(\d +));
的foreach(赛对垒线)
{
串控制= match.Groups [1] .value的;
字符串文本= match.Groups [2] .value的;
INT X = Int32.Parse(match.Groups [3]。价值);
INT Y = Int32.Parse(match.Groups [4]。价值);
字符串的CFont = match.Groups [5] .value的;

字符串的fontName =的CFont;
字符串[] fontNameFields = fontName.Split(,);


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



堆栈跟踪


 在Tabbed.Form1.FillCanvas()在C:\Documents和Settings\jay\My Documents\Visual工作室2008\Projects\MYPROGGY\ MYPROGGY\Form1.cs:行574 
在Tabbed.Form1.openbtn_Click(对象发件人,EventArgs e)在C:\Documents和Settings\jay\My文件\Visual工作室2008\Projects\ MYPROGGY\ MYPROGGY\Form1.cs:行802
在System.Windows.Forms.ToolStripItem.RaiseEvent(对象键,EventArgs五)
的系统。 Windows.Forms.ToolStripButton.OnClick(EventArgs五)
在System.Windows.Forms.ToolStripItem.HandleClick在System.Windows.Forms.ToolStripItem.HandleMouseUp(EventArgs五)
(MouseEventArgs E)
。在System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs的,ToolStripItemEventType满足)
在System.Windows.Forms.ToolStripItem.FireEvent(EventArgs的,ToolStripItemEventType满足)
在System.Windows.Forms的。 ToolStrip.OnMouseUp(MouseEventArgs MEA)
在System.Windows.Forms.Control.WmMouseUp(消息和;男,MouseButtons按钮,点击的Int32)在System.Windows.Forms.Control.WndProc(消息和
,M)
在System.Windows.Forms.ScrollableControl.WndProc(消息和M)
在System.Windows.Forms.ToolStrip.WndProc(消息和M)
在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息和M)
在System.Windows.Forms.Control.ControlNativeWindow .WndProc(消息和M)
在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr的的HWND,味精的Int32,IntPtr的WPARAM,LPARAM的IntPtr)
在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG和放大器; MSG)
在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(的Int32 dwComponentID,的Int32原因,的Int32 pvLoopData)
在System.Windows.Forms的。 Application.ThreadContext.RunMessageLoopInner(的Int32原因,ApplicationContext的情况下)
在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(的Int32原因,ApplicationContext的情况下)的System.Windows.Forms.Application.Run
( MainForm的形式)的Tabbed.Program.Main
(字串[] args)在C:\Documents和Settings\jay\My Documents\Visual工作室2008\Projects\ MYPROGGY\ MYPROGGY\的Program.cs:行27
在System.AppDomain._nExecuteAssembly(议会会议,字串[] args)在System.AppDomain.ExecuteAssembly(字符串assemblyFile,证据assemblySecurity,字串[] args)$ b $
b。在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态)
在System.Threading.ThreadHelper.ThreadStart()


解决方案

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

 使用系统; 

类测试
{
静态无效的主要(字串[] args)
{
字符串的fontName =宋体,12.5;
字符串[] fontNameFields = fontName.Split(,);
字符串名称= fontNameFields [0];
浮子大小= float.Parse(fontNameFields [1]);
Console.WriteLine({0}:{1},名称,大小);
}
}



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


i have a text file that contains font information like this:

Arial, 12.5

...and I need to read that info into label.Font like this:

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

... but I always get the following error:

Index was outside the bounds of the Array.

Can somebody please help me with this?

Thanks jason.

Code I have:

    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]));
}

Stacktrace


   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()

解决方案

I suspect you're running into a line which doesn't contain a comma. Your sample data work okay:

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);
    }
}

Log (or pop up in a dialog box) fontName just before you try to create the font, and I'm sure that'll show you what's going on.

这篇关于解析字体信息,并将其转换为System.Drawing.Font的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆