WPF實(shí)現(xiàn)文本描邊+外發(fā)光效果的示例代碼
解決思路:
(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時,不帶透明度 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> /// 文本轉(zhuǎn)圖片 /// </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)); } } }
應(yīng)用:
(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("外發(fā)光+描邊", new System.Drawing.Font("Microsoft YaHei", 30, System.Drawing.FontStyle.Bold), forColor, backColor, 3);
效果:
優(yōu)化點(diǎn):可以將FancyText封裝成自定義控件,定義描邊大小,顏色值等依賴屬性,直接為WPF使用。
到此這篇關(guān)于WPF實(shí)現(xiàn)文本描邊+外發(fā)光效果的示例代碼的文章就介紹到這了,更多相關(guān)WPF文本描邊外發(fā)光內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
web.config中配置數(shù)據(jù)庫連接的方式
Web.config文件是一個XML文本文件,它用來儲存 ASP.NET Web 應(yīng)用程序的配置信息(如最常用的設(shè)置ASP.NET Web 應(yīng)用程序的身份驗(yàn)證方式),它可以出現(xiàn)在應(yīng)用程序的每一個目錄中。本文主要介紹web.config中配置數(shù)據(jù)庫連接的兩種方式,一起來看。2015-10-10關(guān)于利用RabbitMQ實(shí)現(xiàn)延遲任務(wù)的方法詳解
最近在使用RabbitMQ來實(shí)現(xiàn)延遲任務(wù)的時候發(fā)現(xiàn),這其中的知識點(diǎn)還是挺多的,所以下面這篇文章主要給大家介紹了關(guān)于利用RabbitMQ實(shí)現(xiàn)延遲任務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-12-12Asp.net core利用dynamic簡化數(shù)據(jù)庫訪問
這篇文章介紹了Asp.net core利用dynamic簡化數(shù)據(jù)庫訪問的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07在ASP.NET2.0中通過Gmail發(fā)送郵件的代碼
我們有時候需要發(fā)送郵件給訪問網(wǎng)頁的用戶,例如,注冊的時候,發(fā)一確認(rèn)信什么的。那么,在ASP.NET2.0中該如果操作呢?2008-06-06ASP.NET Web Api 2實(shí)現(xiàn)多文件打包并下載文件的實(shí)例
這篇文章主要介紹了ASP.NET Web Api 2利用ByteArrayContent和StreamContent實(shí)現(xiàn)多文件打包并下載的方法,提供源碼下載,需要的朋友可以參考下。2016-06-06asp.net core下給網(wǎng)站做安全設(shè)置的方法詳解
這篇文章主要給大家介紹了關(guān)于asp.net core下給網(wǎng)站做安全設(shè)置的相關(guān)資料,文章通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07.NET實(shí)現(xiàn)魔方游戲(一)之任意階魔方的表示
這篇文章主要介紹了.NET實(shí)現(xiàn)魔方游戲(一)之任意階魔方的表示 的相關(guān)資料,需要的朋友可以參考下2016-02-02關(guān)于中g(shù)ridview 字符串截取的方法
在Gridview中,如果你的某一列字符串的長度過長,不做處理的話.那么將顯示的奇丑無比,可以采取設(shè)置樣式,將其顯示為定長,可以在點(diǎn)擊查看的時候,在另一個頁面對其進(jìn)行顯示2013-06-06