C# GDI+ 文字 陰影,描邊 的實現


好的,各位RT 這次是簡單的 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;
        }
需要陰影的同學 上面的注釋去掉就ok。
 
好吧。沒啥可說的,很多人都會。
歡迎交流。。。
by cnblogs Soar
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM