/// <summary> /// 获取轮廓多边形列表 /// </summary> /// <param name="hdc">场景</param> /// <param name="uChar">字符</param> /// <returns></returns> private static String GetOutline(IntPtr hdc, uint uChar) { outline.Clear(); // 转置矩阵 MAT2 mat2 = new MAT2(); mat2.eM11.value = 1; mat2.eM22.value = 1; GLYPHMETRICS lpGlyph = new GLYPHMETRICS(); //获取缓存区大小 int bufferSize = GdiNativeMethods.GetGlyphOutline(hdc, uChar, GGO_NATIVE, out lpGlyph, 0, IntPtr.Zero, ref mat2); if (bufferSize > 0) { //获取成功后,分配托管内存 IntPtr buffer = Marshal.AllocHGlobal(bufferSize); try { int ret = GdiNativeMethods.GetGlyphOutline(hdc, uChar, GGO_NATIVE, out lpGlyph, (uint)bufferSize, buffer, ref mat2); if (ret > 0) { //构建轮廓 // width, height outline.AppendFormat("{0},{1},$$POLY_SIZE$$,", lpGlyph.gmBlackBoxX, lpGlyph.gmBlackBoxY); //从缓存区构造字型轮廓 GetPolygons(buffer, bufferSize); return(outline.ToString()); } else { throw new Exception("获取字型数据失败!"); } } finally { //释放托管内存 Marshal.FreeHGlobal(buffer); } } else { throw new Exception("未能获取缓存区!"); } }
/// <summary> /// 获取指定字符在指定字体下的轮廓 /// </summary> /// <param name="uChar">字符</param> /// <param name="font">字体</param> /// <returns></returns> public static String GetOutline(uint uChar, Font font) { //画板 Graphics g = Graphics.FromHwnd(IntPtr.Zero); IntPtr hdc = g.GetHdc(); //将字体选入场景 IntPtr fontPtr = font.ToHfont(); GdiNativeMethods.SelectObject(hdc, fontPtr); try { return(GetOutline(hdc, uChar)); } catch (Exception e) { Console.Write(e.Message); return(""); } }