解决思路:

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

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

代码:

 1 using System;
2 using System.Drawing;
3 using System.Drawing.Drawing2D;
4 using System.Drawing.Text;
5 using System.IO;
6
7 namespace TextHighLighthDemo
8 {
9 public class FancyText
10 {
11 private static System.Windows.Media.Imaging.BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
12 {
13 using (MemoryStream stream = new MemoryStream())
14 {
15 bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png); // 坑点:格式选Bmp时,不带透明度
16
17 stream.Position = 0;
18 System.Windows.Media.Imaging.BitmapImage result = new System.Windows.Media.Imaging.BitmapImage();
19 result.BeginInit();
20 result.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
21 result.StreamSource = stream;
22 result.EndInit();
23 result.Freeze();
24 return result;
25 }
26 }
27
28 private static Bitmap ImageFromText(string strText, Font fnt, Color clrFore, Color clrBack, int blurAmount = 5)
29 {
30 Bitmap bmpOut = null;
31 int sunNum = 255; //光晕的值
32 using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
33 {
34 SizeF sz = g.MeasureString(strText, fnt);
35 using (Bitmap bmp = new Bitmap((int)sz.Width, (int)sz.Height))
36 using (Graphics gBmp = Graphics.FromImage(bmp))
37 using (SolidBrush brBack = new SolidBrush(Color.FromArgb(sunNum, clrBack.R, clrBack.G, clrBack.B)))
38 using (SolidBrush brFore = new SolidBrush(clrFore))
39 {
40 gBmp.SmoothingMode = SmoothingMode.HighQuality;
41 gBmp.InterpolationMode = InterpolationMode.HighQualityBilinear;
42 gBmp.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
43 gBmp.DrawString(strText, fnt, brBack, 0, 0);
44 bmpOut = new Bitmap(bmp.Width + blurAmount, bmp.Height + blurAmount);
45 using (Graphics gBmpOut = Graphics.FromImage(bmpOut))
46 {
47 gBmpOut.SmoothingMode = SmoothingMode.HighQuality;
48 gBmpOut.InterpolationMode = InterpolationMode.HighQualityBilinear;
49 gBmpOut.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
50 //阴影光晕
51 for (int x = 0; x <= blurAmount; x++)
52 {
53 for (int y = 0; y <= blurAmount; y++)
54 {
55 gBmpOut.DrawImageUnscaled(bmp, x, y);
56 }
57 }
58 gBmpOut.DrawString(strText, fnt, brFore, blurAmount / 2, blurAmount / 2);
59 }
60 }
61 }
62 return bmpOut;
63 }
64
65 /// <summary>
66 /// 文本转图片
67 /// </summary>
68 /// <param name="strText"></param>
69 /// <param name="fnt"></param>
70 /// <param name="clrFore"></param>
71 /// <param name="clrBack"></param>
72 /// <param name="blurAmount"></param>
73 /// <returns></returns>
74 public static System.Windows.Media.Imaging.BitmapImage BitmapImageFromText(string strText, Font fnt, Color clrFore, Color clrBack, int blurAmount = 5)
75 {
76
77 return BitmapToBitmapImage(ImageFromText(strText, fnt, clrFore, clrBack, blurAmount));
78 }
79
80 }
81 }

应用:

(1)XMAL代码

<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

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使用。

WPF 文本描边+外发光效果实现的更多相关文章

  1. WPF 文本框添加水印效果

    有的时候我们需要为我们的WPF文本框TextBox控件添加一个显示水印的效果来增强用户体验,比如登陆的时候提示输入用户名,输入密码等情形.如下图所示: 这个时候我们除了可以修改TextBox控件的控件 ...

  2. 使用 WPF 做个 PowerPoint 系列 基于 OpenXML 解析实现 PPT 文本描边效果

    本文是使用 WPF 做个 PowerPoint 系列的博客,本文来告诉大家如何解析 PPT 里面的文本描边效果,在 WPF 应用中绘制出来,实现像素级相同 背景知识 在开始之前,期望你了解了 PPT ...

  3. WPF文本框密码框添加水印效果

    WPF文本框密码框添加水印效果 来源: 阅读:559 时间:2014-12-31 分享: 0 按照惯例,先看下效果 文本框水印 文本框水印相对简单,不需要重写模板,仅仅需要一个VisualBrush ...

  4. WPF文字描边的解决方法(二)——支持文字竖排和字符间距调整

    原文:WPF文字描边的解决方法(二)--支持文字竖排和字符间距调整 自前天格式化文本效果出来后,今天又添加文本竖排和调整字符间距的功能.另外,由于上次仓促,没来得及做有些功能的设计时支持,这次也调整好 ...

  5. WPF 实现跑马灯效果的Label控件,数据绑定方式实现

    原文:WPF 实现跑马灯效果的Label控件,数据绑定方式实现 项目中需要使用数据绑定的方式实现跑马灯效果的Label,故重构了Label控件:具体代码如下 using System; using S ...

  6. 纯CSS实现帅气的SVG路径描边动画效果(转载)

    本文转载自: 纯CSS实现帅气的SVG路径描边动画效果

  7. JS 文本输入框放大镜效果

    JS 文本输入框放大镜效果 今天下午研究了下 "文本输入框放大镜效果" 当然KISSY官网也有这种组件 请看kissy demo 其实这种效果 对于很多童鞋来说 应该并不陌生!我今 ...

  8. WPF绘制党徽(立体效果,Cool)

    原文:WPF绘制党徽(立体效果,Cool) 前面用WPF方式绘制了党旗(WPF制作的党旗) ,去年3月份利用C# 及GDI+绘制过党徽,这次使用WPF来绘制党徽. ------------------ ...

  9. WPF 扩大,回弹效果

    原文:WPF 扩大,回弹效果 <Window x:Class="Fish.AccountBook.View.Test.PanelWindow" xmlns="htt ...

随机推荐

  1. 鸟哥的Linux学习笔记-bash

    1. /bin/bash是linux预设的shell,也是Linux发行版的标准shell,它兼容sh,可以看作是sh的功能加强. 2. bash具有命令记录功能,在bash中通过上下键就可以翻找之前 ...

  2. 匿名内部类不能访问外部类方法中的局部变量,除非变量被声明为final类型

    1. 这里所说的"匿名内部类"主要是指在其外部类的成员方法内定义,同时完成实例化的类,若其访问该成员方法中的局部变量,局部变量必须要被final修饰.2. 原因是编译程序实现上的困 ...

  3. 需求: 使用LinkedList存储一副扑克牌,然后实现洗牌功能。

    import java.util.LinkedList; import java.util.Random; /* 需求: 使用LinkedList存储一副扑克牌,然后实现洗牌功能. */ //扑克类 ...

  4. hgame-week3-web-wp

    hgame第三周(web ak) 1.SecurityCenter 先看看hint(**vendor是第三方库和插件放置的文件夹,一般来源于composer的安装) 找到了使用的twig模板,应该是t ...

  5. 《手把手教你》系列技巧篇(六十五)-java+ selenium自动化测试 - cookie -下篇(详细教程)

    1.简介 今天这一篇,宏哥主要讲解:利用WebDriver 提供可以读取.添加和删除cookie 信息的相关操作方法.验证浏览器中是否存在某个cookie.原因是:因为基于真实的cookie 的测试是 ...

  6. Linux运维-常用操作-培训用例

    一.服务器环境 Centos 7.9 二.常用连接工具(免费) 1.Finalshell 2.MobaXterm 3.Putty + WinSCP 三.Linux  系统目录结构 /bin :是 Bi ...

  7. 帆软报表(finereport) 饼图联动

    饼图联动:点击饼图1,饼图2和饼图3显示饼图1的关联数据,接着点击饼图2,饼图3显示饼图2的关联数据,点击上方清除级联,饼图则恢复默认展示状态 下面以上图示例效果为例,说明制作过程. 1.为每个饼图准 ...

  8. Solution -「NOI 2021」「洛谷 P7740」机器人游戏

    \(\mathcal{Description}\)   Link.   自己去读题面叭~ \(\mathcal{Solution}\)   首先,参悟[样例解释 #2].一种暴力的思路即为钦定集合 \ ...

  9. 阿里云服务器ECS挂载数据盘—linux系统

    参考阿里云官网帮助文档:https://help.aliyun.com/document_detail/25426.html 里面有些步骤说的不是很清楚,初学者可能操作时会遇到问题.通过这篇文档进行进 ...

  10. .Net Core AOP之IResultFilter

    一.简介 在.net core 中Filter分为以下六大类: 1.AuthorizeAttribute(权限验证) 2.IResourceFilter(资源缓存) 3.IActionFilter(执 ...