網(wǎng)站首頁 編程語言 正文
解決思路:
(1)描邊效果可以將文本字符串用GDI+生成Bitmap,然后轉(zhuǎn)成BitmapImage,再用WPF的Image控件顯示。
(2)外發(fā)光效果用WPF自帶的Effect實(shí)現(xiàn)
代碼:
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); // 坑點(diǎn):格式選Bmp時(shí),不帶透明度 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; } ////// 文本轉(zhuǎn)圖片 /// /// /// /// /// /// ///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)); } } }
應(yīng)用:
(1)XMAL代碼
(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("外發(fā)光+描邊", new System.Drawing.Font("Microsoft YaHei", 30, System.Drawing.FontStyle.Bold), forColor, backColor, 3);
效果:
優(yōu)化點(diǎn):可以將FancyText封裝成自定義控件,定義描邊大小,顏色值等依賴屬性,直接為WPF使用。
原文鏈接:https://www.cnblogs.com/xunyou/p/11596220.html
相關(guān)推薦
- 2023-12-11 使用SSH地址拉取遠(yuǎn)程倉庫代碼報(bào)下面的錯(cuò)誤
- 2022-06-10 C++?STL?中的數(shù)值算法示例講解_C 語言
- 2022-05-21 k8s入門集群組件介紹及概念理解_服務(wù)器其它
- 2022-04-25 C#使用NPOI讀取excel轉(zhuǎn)為DataSet_C#教程
- 2022-11-03 anaconda?部署Jupyter?Notebook服務(wù)器過程詳解_python
- 2022-05-23 一起來學(xué)習(xí)C++的函數(shù)指針和函數(shù)對象_C 語言
- 2022-08-12 python算法加密?pyarmor與docker_python
- 2023-04-18 C生萬物C語言宏將整數(shù)二進(jìn)制位的奇偶數(shù)位交換_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支