最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

WPF實現(xiàn)文本描邊+外發(fā)光效果的示例代碼

 更新時間:2022年03月21日 08:47:43   作者:荀幽  
這篇文章主要介紹了如何利用WPF實現(xiàn)文本描邊以及外發(fā)光的效果,文中的示例代碼講解詳細,對我們學習有一定幫助,需要的可以參考一下

解決思路:

(1)描邊效果可以將文本字符串用GDI+生成Bitmap,然后轉(zhuǎn)成BitmapImage,再用WPF的Image控件顯示。

(2)外發(fā)光效果用WPF自帶的Effect實現(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); // 坑點:格式選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));
        }

    }
}

應用:

(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)化點:可以將FancyText封裝成自定義控件,定義描邊大小,顏色值等依賴屬性,直接為WPF使用。

到此這篇關(guān)于WPF實現(xiàn)文本描邊+外發(fā)光效果的示例代碼的文章就介紹到這了,更多相關(guān)WPF文本描邊外發(fā)光內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

郯城县| 石嘴山市| 绥棱县| 张家港市| 尖扎县| 纳雍县| 永嘉县| 双峰县| 兴海县| 报价| 宿州市| 舒城县| 黎平县| 广宗县| 滨海县| 金溪县| 泌阳县| 汝州市| 奉新县| 桂东县| 永新县| 河池市| 黄石市| 旌德县| 邯郸县| 陈巴尔虎旗| 海淀区| 化德县| 庆阳市| 前郭尔| 敦煌市| 岳池县| 中山市| 肥东县| 东乌珠穆沁旗| 景谷| 永川市| 卫辉市| 阿图什市| 闻喜县| 聂荣县|