【WPF 实例】实现文本描边+外发光效果的示例代码

这篇文章主要介绍了如何利用WPF实现文本描边以及外发光的效果,文中的示例代码讲解详细,对我们学习有一定帮助,需要的可以参考一下。

解决思路:

(1)描边效果可以将文本字符串用GDI+生成Bitmap,然后转成BitmapImage,再用WPF的Image控件显示。

(2)外发光效果用WPF自带的Effect实现

代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

using System;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Drawing.Text;

using System.IO;

namespace TextHighLighthDemo

{

    public class FancyText

    {

        private static System.Windows.Media.Imaging.BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)

        {

            using (MemoryStream stream = new MemoryStream())

            {

                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png); // 坑点:格式选Bmp时,不带透明度

                stream.Position = 0;

                System.Windows.Media.Imaging.BitmapImage result = new System.Windows.Media.Imaging.BitmapImage();

                result.BeginInit();

                result.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;

                result.StreamSource = stream;

                result.EndInit();

                result.Freeze();

                return result;

            }

        }

        private static Bitmap ImageFromText(string strText, Font fnt, Color clrFore, Color clrBack, int blurAmount = 5)

        {

            Bitmap bmpOut = null;

            int sunNum = 255;  //光晕的值

            using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))

            {

                SizeF sz = g.MeasureString(strText, fnt);

                using (Bitmap bmp = new Bitmap((int)sz.Width, (int)sz.Height))

                using (Graphics gBmp = Graphics.FromImage(bmp))

                using (SolidBrush brBack = new SolidBrush(Color.FromArgb(sunNum, clrBack.R, clrBack.G, clrBack.B)))

                using (SolidBrush brFore = new SolidBrush(clrFore))

                {

                    gBmp.SmoothingMode = SmoothingMode.HighQuality;

                    gBmp.InterpolationMode = InterpolationMode.HighQualityBilinear;

                    gBmp.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

                    gBmp.DrawString(strText, fnt, brBack, 0, 0);

                    bmpOut = new Bitmap(bmp.Width + blurAmount, bmp.Height + blurAmount);

                    using (Graphics gBmpOut = Graphics.FromImage(bmpOut))

                    {

                        gBmpOut.SmoothingMode = SmoothingMode.HighQuality;

                        gBmpOut.InterpolationMode = InterpolationMode.HighQualityBilinear;

                        gBmpOut.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

                        //阴影光晕

                        for (int x = 0; x <= blurAmount; x++)

                        {

                            for (int y = 0; y <= blurAmount; y++)

                            {

                                gBmpOut.DrawImageUnscaled(bmp, x, y);

                            }

                        }

                        gBmpOut.DrawString(strText, fnt, brFore, blurAmount / 2, blurAmount / 2);

                    }

                }

            }

            return bmpOut;

        }

        /// <summary>

        /// 文本转图片

        /// </summary>

        /// <param name="strText"></param>

        /// <param name="fnt"></param>

        /// <param name="clrFore"></param>

        /// <param name="clrBack"></param>

        /// <param name="blurAmount"></param>

        /// <returns></returns>

        public static System.Windows.Media.Imaging.BitmapImage BitmapImageFromText(string strText, Font fnt, Color clrFore, Color clrBack, int blurAmount = 5)

        {

            return BitmapToBitmapImage(ImageFromText(strText, fnt, clrFore, clrBack, blurAmount));

        }

    }

}

应用:

(1)XMAL代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<Grid Background="Gray">

        <StackPanel Height="300">

            <Image x:Name="img" Height="70" VerticalAlignment="Top" >

                <Image.Effect>

                    <DropShadowEffect Color="#00ffff" ShadowDepth="0" BlurRadius="15"/>

                </Image.Effect>

            </Image>

            <Image x:Name="img1" Height="35" VerticalAlignment="Top" Margin="0 10 0 0">

                <Image.Effect>

                    <DropShadowEffect Color="#00ffff" ShadowDepth="0" BlurRadius="5"/>

                </Image.Effect>

            </Image>

        </StackPanel>

    </Grid>

(2)code behind

1

2

3

4

var backColor = System.Drawing.ColorTranslator.FromHtml("#037be2");

var forColor= System.Drawing.ColorTranslator.FromHtml("#ffffff");

img.Source = FancyText.BitmapImageFromText("测试字体,微软雅黑", new System.Drawing.Font("Microsoft YaHei", 60, System.Drawing.FontStyle.Bold), forColor, backColor, 6);

img1.Source = FancyText.BitmapImageFromText("外发光+描边", new System.Drawing.Font("Microsoft YaHei", 30, System.Drawing.FontStyle.Bold), forColor, backColor, 3);

效果:

优化点:可以将FancyText封装成自定义控件,定义描边大小,颜色值等依赖属性,直接为WPF使用。

转载内容,补充学习知识库,如有问题请及时联系,将及时下架。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值