WPF 文本描边+外发光效果实现
解决思路:
(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 文本描边+外发光效果实现的更多相关文章
- WPF 文本框添加水印效果
有的时候我们需要为我们的WPF文本框TextBox控件添加一个显示水印的效果来增强用户体验,比如登陆的时候提示输入用户名,输入密码等情形.如下图所示: 这个时候我们除了可以修改TextBox控件的控件 ...
- 使用 WPF 做个 PowerPoint 系列 基于 OpenXML 解析实现 PPT 文本描边效果
本文是使用 WPF 做个 PowerPoint 系列的博客,本文来告诉大家如何解析 PPT 里面的文本描边效果,在 WPF 应用中绘制出来,实现像素级相同 背景知识 在开始之前,期望你了解了 PPT ...
- WPF文本框密码框添加水印效果
WPF文本框密码框添加水印效果 来源: 阅读:559 时间:2014-12-31 分享: 0 按照惯例,先看下效果 文本框水印 文本框水印相对简单,不需要重写模板,仅仅需要一个VisualBrush ...
- WPF文字描边的解决方法(二)——支持文字竖排和字符间距调整
原文:WPF文字描边的解决方法(二)--支持文字竖排和字符间距调整 自前天格式化文本效果出来后,今天又添加文本竖排和调整字符间距的功能.另外,由于上次仓促,没来得及做有些功能的设计时支持,这次也调整好 ...
- WPF 实现跑马灯效果的Label控件,数据绑定方式实现
原文:WPF 实现跑马灯效果的Label控件,数据绑定方式实现 项目中需要使用数据绑定的方式实现跑马灯效果的Label,故重构了Label控件:具体代码如下 using System; using S ...
- 纯CSS实现帅气的SVG路径描边动画效果(转载)
本文转载自: 纯CSS实现帅气的SVG路径描边动画效果
- JS 文本输入框放大镜效果
JS 文本输入框放大镜效果 今天下午研究了下 "文本输入框放大镜效果" 当然KISSY官网也有这种组件 请看kissy demo 其实这种效果 对于很多童鞋来说 应该并不陌生!我今 ...
- WPF绘制党徽(立体效果,Cool)
原文:WPF绘制党徽(立体效果,Cool) 前面用WPF方式绘制了党旗(WPF制作的党旗) ,去年3月份利用C# 及GDI+绘制过党徽,这次使用WPF来绘制党徽. ------------------ ...
- WPF 扩大,回弹效果
原文:WPF 扩大,回弹效果 <Window x:Class="Fish.AccountBook.View.Test.PanelWindow" xmlns="htt ...
随机推荐
- 一个label 混搭不同颜色,不同字体的文字.. by 徐
效果如图箭头所示,只需要一个label就可以做到不同颜色或不同字体的效果 1 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, ...
- spring property标签中的 ref属性和ref 标签有什么不同
spring的配置文件可能会有多个<property name="a" ref="b" />就是找当前配置文件里的bean 也就是b <ref ...
- Solution -「NOI 2016」「洛谷 P1587」循环之美
\(\mathcal{Description}\) Link. 给定 \(n,m,k\),求 \(x\in [1,n]\cap\mathbb N,y\in [1,m]\cap \mathbb ...
- [LeetCode]4.寻找两个正序数组的中位数(Java)
原题地址: median-of-two-sorted-arrays 题目描述: 示例 1: 输入:nums1 = [1,3], nums2 = [2] 输出:2.00000 解释:合并数组 = [1, ...
- CentOS7下修改默认网卡名为eth0的方法
1.修改网卡配置文件中的 DEVICE=参数的,关于eth0 [root@ansheng ~ ]# cd /etc/sysconfig/network-scripts/ [root@ansheng n ...
- VS Code开发TypeScript
TypeScript是JaveScript的超集,为JavaScript增加了很多特性,它可以编译成纯JavaScript在浏览器上运行.TypeScript已经成为各种流行框架和前端应用开发的首选. ...
- JVM学习——字节码(学习过程)
JVM--字节码 为什么要学字节码 字节码文件,有什么用? JVM虚拟机的特点:一处编译,多处运行. 多处运行,靠的是.class 字节码文件. JVM本身,并不是跨平台的.Java之所以跨平台,是因 ...
- C# 给Word每一页设置不同图片水印
Word中设置水印时,可加载图片设置为水印效果,但通常添加水印效果时,会对所有页面都设置成统一效果,如果需要对每一页或者某个页面设置不同的水印效果,则可以参考本文中的方法.下面,将以C#代码为例,对W ...
- 容器化 | 在 KubeSphere 中部署 MySQL 集群
程润科 数据库研发工程师,目前从事 RadonDB MySQL Kubernetes 研发,热衷于研究数据库内核.K8s 相关技术. 张莉梅 高级文档工程师,目前负责数据库产品文档开发.维护和管理工作 ...
- [Matlab]二维隐函数绘图
MATLAB提供了一个ezplot函数绘制隐函数图形,有三种调用方式: 对于函数f=f(x),ezplot函数的调用格式为: ezplot(f):在默认区间-2pi<=x<=2pi内绘制f ...