解析字体信息并将其转换为 System.Drawing.Font
我有一个包含这样的字体信息的文本文件:
Arial,12.5
...我需要将该信息读入 label.Font,如下所示:
label.Font = new 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]));
}
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 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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑您遇到了不包含逗号的行。您的示例数据工作正常:
在尝试创建字体之前记录(或在对话框中弹出)
fontName
,我相信这会告诉您发生了什么。I suspect you're running into a line which doesn't contain a comma. Your sample data work okay:
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.Web.UI.WebControls 一样,然后 Font 没有设置器。
上面的 Skeet 代码对于给定的输入工作得很好。你的问题出在其他地方。
Could it be your format contains a strange comma, or none at all?
Also could you provide code on the label as if it were a
System.Web.UI.WebControls
then Font has not got a setter.Skeet's code above works fine for the given input. You're problem lies elsewhere.
乍一看,代码看起来没问题。
您是否检查过
fontName
是否符合您的预期(即“Arial,12.5”)以及调用fontName.Split
之后fontNameFields
包含的内容代码>?在没有看到实际值的情况下,我不想提出任何其他建议。
编辑
从您更新的代码中我建议您的正则表达式失败了。尝试打印
match.Groups
数组的内容,以检查您是否正确解析输入。At first glance the code looks OK.
Have you checked that
fontName
is what you expect it to be (i.e. "Arial, 12.5") and whatfontNameFields
contains after the call tofontName.Split
?Without seeing the actual values I wouldn't like to suggest anything else.
EDIT
From your updated code I'd suggest that it's your regex that's failing. Try printing out the contents of the
match.Groups
array to check that you're parsing the input correctly.