问题描述
如题如宋体对应的是simsun.ttc用C#如何获取
解决方案
解决方案二:
用IO查找Fonts文件夹
解决方案三:
解决方案四:
引用1楼DENQH的回复:
用IO查找Fonts文件夹
我想知道的是"宋体"是如何关联上simsun.ttc就是说我用FontDialog得到"宋体"这个名称后,然后我该如何去获得simsun.ttc这个系统文件名有现成的函数或者方法吗?
解决方案五:
注册表HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsNTCurrentVersionFonts
解决方案六:
引用4楼hjywyj的回复:
注册表HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsNTCurrentVersionFonts
查过了,没有找到宋体名和simsun.ttc文件名的对应
解决方案七:
MSDN上2007年已经有人问过这个问题,如果你的英文还可以的话,可以看下地址:http://social.msdn.microsoft.com/Forums/en-US/windowsxps/thread/843d0e98-ac8e-4021-94f9-5e3130d058b9解决方案在:http://www.codeproject.com/Articles/4190/XFont-Get-font-name-and-file-information就是第一篇帖子最后一个人所说:It'snotperfect,butitworks.GoodLuck!
解决方案八:
刚好我也需要这个功能,就一次回复到位吧。以下C#代码可读取指定.ttf文件里的字体名,思路反过来:遍历<windir>Fonts目录下的所有文件,即可找到通过“字体选择器”返回的字体名的字体文件。虽然办法笨了点,但确实也没啥好办法,凑合着能用。It'snotperfect,butitworks.----------------------------------------------usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Windows.Forms;usingSystem.Runtime.InteropServices;usingSystem.IO;namespaceFontNameGetter{[StructLayout(LayoutKind.Sequential,Pack=0x1)]structTT_OFFSET_TABLE{publicushortuMajorVersion;publicushortuMinorVersion;publicushortuNumOfTables;publicushortuSearchRange;publicushortuEntrySelector;publicushortuRangeShift;}[StructLayout(LayoutKind.Sequential,Pack=0x1)]structTT_TABLE_DIRECTORY{publiccharszTag1;publiccharszTag2;publiccharszTag3;publiccharszTag4;publicuintuCheckSum;//ChecksumpublicuintuOffset;//OffsetfrombeginningoffilepublicuintuLength;//lengthofthetableinbytes}[StructLayout(LayoutKind.Sequential,Pack=0x1)]structTT_NAME_TABLE_HEADER{publicushortuFSelector;publicushortuNRCount;publicushortuStorageOffset;}[StructLayout(LayoutKind.Sequential,Pack=0x1)]structTT_NAME_RECORD{publicushortuPlatformID;publicushortuEncodingID;publicushortuLanguageID;publicushortuNameID;publicushortuStringLength;publicushortuStringOffset;}partialclassForm1:Form{publicForm1(){InitializeComponent();}privateTT_OFFSET_TABLEttOffsetTable;privateTT_TABLE_DIRECTORYtblDir;privateTT_NAME_TABLE_HEADERttNTHeader;privateTT_NAME_RECORDttNMRecord;privatevoidbutton1_Click(objectsender,EventArgse){FileStreamfs=newFileStream("f:\simsun.ttc",FileMode.Open,FileAccess.Read);BinaryReaderr=newBinaryReader(fs);byte[]buff=r.ReadBytes(Marshal.SizeOf(ttOffsetTable));buff=BigEndian(buff);IntPtrptr=Marshal.AllocHGlobal(buff.Length);Marshal.Copy(buff,0x0,ptr,buff.Length);TT_OFFSET_TABLEttResult=(TT_OFFSET_TABLE)Marshal.PtrToStructure(ptr,typeof(TT_OFFSET_TABLE));Marshal.FreeHGlobal(ptr);//Mustbemaj=1minor=0if(ttResult.uMajorVersion!=1||ttResult.uMinorVersion!=0)return;boolbFound=false;TT_TABLE_DIRECTORYtbName=newTT_TABLE_DIRECTORY();for(inti=0;i<ttResult.uNumOfTables;i++){byte[]bNameTable=r.ReadBytes(Marshal.SizeOf(tblDir));IntPtrptrName=Marshal.AllocHGlobal(bNameTable.Length);Marshal.Copy(bNameTable,0x0,ptrName,bNameTable.Length);tbName=(TT_TABLE_DIRECTORY)Marshal.PtrToStructure(ptrName,typeof(TT_TABLE_DIRECTORY));Marshal.FreeHGlobal(ptrName);stringszName=tbName.szTag1.ToString()+tbName.szTag2.ToString()+tbName.szTag3.ToString()+tbName.szTag4.ToString();if(szName!=null){if(szName.ToString()=="name"){bFound=true;byte[]btLength=BitConverter.GetBytes(tbName.uLength);byte[]btOffset=BitConverter.GetBytes(tbName.uOffset);Array.Reverse(btLength);Array.Reverse(btOffset);tbName.uLength=BitConverter.ToUInt32(btLength,0);tbName.uOffset=BitConverter.ToUInt32(btOffset,0);break;}}}if(bFound){fs.Position=tbName.uOffset;byte[]btNTHeader=r.ReadBytes(Marshal.SizeOf(ttNTHeader));btNTHeader=BigEndian(btNTHeader);IntPtrptrNTHeader=Marshal.AllocHGlobal(btNTHeader.Length);Marshal.Copy(btNTHeader,0x0,ptrNTHeader,btNTHeader.Length);TT_NAME_TABLE_HEADERttNTResult=(TT_NAME_TABLE_HEADER)Marshal.PtrToStructure(ptrNTHeader,typeof(TT_NAME_TABLE_HEADER));Marshal.FreeHGlobal(ptrNTHeader);bFound=false;for(inti=0;i<ttNTResult.uNRCount;i++){byte[]btNMRecord=r.ReadBytes(Marshal.SizeOf(ttNMRecord));btNMRecord=BigEndian(btNMRecord);IntPtrptrNMRecord=Marshal.AllocHGlobal(btNMRecord.Length);Marshal.Copy(btNMRecord,0x0,ptrNMRecord,btNMRecord.Length);TT_NAME_RECORDttNMResult=(TT_NAME_RECORD)Marshal.PtrToStructure(ptrNMRecord,typeof(TT_NAME_RECORD));Marshal.FreeHGlobal(ptrNMRecord);if(ttNMResult.uNameID==1){longfPos=fs.Position;fs.Position=tbName.uOffset+ttNMResult.uStringOffset+ttNTResult.uStorageOffset;char[]szResult=r.ReadChars(ttNMResult.uStringLength);if(szResult.Length!=0){inty=0;//szResultnowcontainsthefontname.//szResult即是文件中的字体名,遍历Fonts目录下的所有文件,目标就实现了}}}}}privatebyte[]BigEndian(byte[]bLittle){byte[]bBig=newbyte[bLittle.Length];for(inty=0;y<(bLittle.Length-1);y+=2){byteb1,b2;b1=bLittle[y];b2=bLittle[y+1];bBig[y]=b2;bBig[y+1]=b1;}returnbBig;}}}-----------------------------------------------别说不会用这个例子啊,仔细看下代码就知道啦。
解决方案九:
经测试,上面老外的方法不好用,不好意思。自己写了个简单的,经测可用:classFontNameFile{publicstaticstringgetFontFileName(stringfontname){stringfolderFullName=System.Environment.GetEnvironmentVariable("windir")+"\fonts";DirectoryInfoTheFolder=newDirectoryInfo(folderFullName);foreach(FileInfoNextFileinTheFolder.GetFiles()){if(NextFile.Exists){if(fontname==getFontName(NextFile.FullName))returnNextFile.FullName;}}return("");}privatestaticstringgetFontName(stringfontfilename){PrivateFontCollectionpfc=newPrivateFontCollection();try{pfc.AddFontFile(fontfilename);}catch(Exceptionex){return"";}return(pfc.Families[0].Name);}}
解决方案十:
PrivateFontCollection的AddFontFile方法还是不支持很多ttf字体!