C# GDI+ 文字 阴影,描边 的实现

this.Paint += Form1_Paint;//这个写在Form_load事件里面
           
        void Form1_Paint(object sender, PaintEventArgs e)
        {
            //Graphics g = e.Graphics;
            //string s = "Outline";
            //RectangleF rect = this.ClientRectangle;
            //Font font = this.Font;
            //StringFormat format = StringFormat.GenericTypographic;
            //float dpi = g.DpiY;
            //using (GraphicsPath path= GetStringPath(s, dpi, rect, font, format))
            //{
            //    g.DrawPath(Pens.Black, path);
            //}
            Graphics g = e.Graphics;
            string s = "宋体宋体宋体宋体宋体宋体宋体宋体宋体";
            RectangleF rect = new RectangleF(350, 0,400,200);
            Font font = this.Font;
            StringFormat format = StringFormat.GenericTypographic;
            float dpi = g.DpiY;
            using (GraphicsPath path = GetStringPath(s, dpi, rect, font, format))
            {
                //阴影代码
                //RectangleF off = rect;
                //off.Offset(5, 5);//阴影偏移
                //using (GraphicsPath offPath = GetStringPath(s, dpi, off, font, format))
                //{
                //    Brush b = new SolidBrush(Color.FromArgb(100, 0, 0, 0));
                //    g.FillPath(b, offPath);
                //    b.Dispose();
                //}
                g.SmoothingMode = SmoothingMode.AntiAlias;//设置字体质量
                g.DrawPath(Pens.Black, path);//绘制轮廓(描边)
                g.FillPath(Brushes.White, path);//填充轮廓(填充)
            }
        }
        GraphicsPath GetStringPath(string s, float dpi, RectangleF rect, Font font, StringFormat format)
        {
            GraphicsPath path = new GraphicsPath();
            // Convert font size into appropriate coordinates
            float emSize = dpi * font.SizeInPoints / 72;
            path.AddString(s, font.FontFamily, (int)font.Style, emSize, rect, format);
            return path;
        }


知识共享许可协议
《C# GDI+ 文字 阴影,描边 的实现》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     159.74ms