Exemplo n.º 1
0
        public static Image GetGlyphOutlineImage(Font font, char ch, out GLYPHMETRICS gm)
        {
            byte[] alpha;
            int size;

            using (FontDC dc = new FontDC(font))
            {
                MAT2 mat = new MAT2();
                mat.eM11.value = 1;
                mat.eM22.value = 1;

                size = (int)GetGlyphOutline(dc.handle, ch, (uint)GlyphOutline.GGO_GRAY8_BITMAP, out gm, 0, IntPtr.Zero, ref mat);
                if (size <= 0)
                {
                    return null;
                }

                IntPtr bufptr = Marshal.AllocHGlobal(size);

                try
                {
                    alpha = new byte[size];
                    GetGlyphOutline(dc.handle, ch, (uint)GlyphOutline.GGO_GRAY8_BITMAP, out gm, (uint)size, bufptr, ref mat);
                    Marshal.Copy(bufptr, alpha, 0, size);
                }
                finally
                {
                    Marshal.FreeHGlobal(bufptr);
                }
            }

            int stride = (int)(gm.gmBlackBoxX + 3) & ~3; // dword alinged
            Bitmap bmp = new Bitmap(stride, (int)gm.gmBlackBoxY, PixelFormat.Format32bppArgb);
            BitmapData bd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
            int bmpSize = bd.Stride * bd.Height;
            byte[] bmpValues = new byte[bmpSize];
            Marshal.Copy(bd.Scan0, bmpValues, 0, bmpSize);

            const int bias = 0x40;
            for (int i = 0; i < size; i++)
            {
                byte v = (byte)((int)alpha[i] * 255 / bias);
                bmpValues[i * 4 + 0] = 0xff;
                bmpValues[i * 4 + 1] = 0xff;
                bmpValues[i * 4 + 2] = 0xff;
                bmpValues[i * 4 + 3] = v;
            }

            Marshal.Copy(bmpValues, 0, bd.Scan0, bmpSize);
            bmp.UnlockBits(bd);

            return bmp;
        }
Exemplo n.º 2
0
 public static void GetTextMetrics(Font font, out TEXTMETRIC tm)
 {
     using (FontDC dc = new FontDC(font))
     {
         GetTextMetrics(dc.handle, out tm);
     }
 }
Exemplo n.º 3
0
        public static ABC[] GetCharABCWidths(Font font, char[] chars)
        {
            int count = chars.GetLength(0);
            if (count <= 0)
            {
                throw new ArgumentException("chars count invalid");
            }

            ABC[] abc = new ABC[count];

            using (FontDC dc = new FontDC(font))
            {
                int idx = 0;
                foreach (char ch in chars)
                {
                    ABC _abc;
                    if (!GetCharABCWidths(dc.handle, ch, ch, out _abc))
                    {
                        throw new Exception("GetCharABCWidths failed");
                    }
                    abc[idx++] = _abc;
                }
            }
            return abc;
        }