首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >获得发光效果的最佳方法windows phone 7

获得发光效果的最佳方法windows phone 7
EN

Stack Overflow用户
提问于 2010-09-16 00:31:20
回答 3查看 4K关注 0票数 8

我正在摆弄Windows phone7SDK,我试图让屏幕看起来像一个老式的数字显示器。现在我正在试图弄清楚如何让文本“发光”,就像那些很酷的数字时钟一样。这是我假设你会考虑使用着色器的类型,但似乎着色器在Windows phone7操作系统上是禁用的。有什么想法吗?更具体地说,我希望文本看起来像是一个光源,并使颜色从实际字体中稍微“渗出”出来。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-03-26 06:20:48

我想说这是一个选择,要么使用图像作为字体,要么使用WriteableBitmap进行模糊处理。

使用预先制作的字体图像可以让字母变得像你想的那样复杂,而且应该表现良好。SpriteFont2非常方便,因为它可以生成具有发光、笔划、阴影等效果的SpriteSheet,并导出包含字母位置的xml文件。将生成的png和xml文件添加到您的解决方案中,并将Build Action更改为content,同时检查您是否引用了System.Xml.Linq。

然后可以使用以下类。

代码语言:javascript
运行
复制
public static class BitmapFont
{
    private class FontInfo
    {
        public FontInfo(WriteableBitmap image, Dictionary<char, Rect> metrics)
        {
            this.Image = image;
            this.Metrics = metrics;
        }
        public WriteableBitmap Image { get; private set; }
        public Dictionary<char, Rect> Metrics { get; private set; }
    }

    private static Dictionary<string, FontInfo> fonts = new Dictionary<string, FontInfo>();
    public static void RegisterFont(string fontFile, string fontMetricsFile)
    {
        string name = System.IO.Path.GetFileNameWithoutExtension(fontFile);
        BitmapImage image = new BitmapImage();

        image.SetSource(App.GetResourceStream(new Uri(fontFile,UriKind.Relative)).Stream);
        var metrics = XDocument.Load(fontMetricsFile);
        var dict = (from c in metrics.Root.Elements()
                    let key = (char)((int)c.Attribute("key"))
                    let rect = new Rect((int)c.Element("x"), (int)c.Element("y"), (int)c.Element("width"), (int)c.Element("height"))
                    select new { Char = key, Metrics = rect }).ToDictionary(x => x.Char, x => x.Metrics);

        fonts.Add(name,new FontInfo(new WriteableBitmap(image),dict));
    }

    public static WriteableBitmap DrawFont(string text, string fontName)
    {
        var font = fonts[fontName];

        var letters = text.Select(x => font.Metrics[x]).ToArray();
        var height = (int)letters.Max(x => x.Height);
        var width = (int)letters.Sum(x => x.Width);

        WriteableBitmap bmp = new WriteableBitmap(width, height);

        int[] source = font.Image.Pixels, dest = bmp.Pixels;
        int sourceWidth = font.Image.PixelWidth;
        int destX = 0;
        foreach (var letter in letters)
        {
            for (int sourceY = (int)letter.Y, destY = 0; destY < letter.Height; sourceY++, destY++)
            {
                Array.Copy(source, (sourceY * sourceWidth) + (int)letter.X, dest, (destY * width) + destX, (int)letter.Width);
            }
            destX += (int)letter.Width;
        }

        return bmp;
    }

    public static Rectangle[] GetElements(string text, string fontName)
    {
        var font = fonts[fontName];

        return (from c in text
                let r = font.Metrics[c]
                select new Rectangle
                {
                    Width = r.Width,
                    Height = r.Height,

                    Fill = new ImageBrush { 
                        ImageSource = font.Image, 
                        AlignmentX=AlignmentX.Left,
                        AlignmentY=AlignmentY.Top,
                        Transform = new TranslateTransform { X = -r.X, Y = -r.Y },
                        Stretch=Stretch.None                        
                    },
                }).ToArray();
    }
}

用法

代码语言:javascript
运行
复制
//Register the font once.
BitmapFont.RegisterFont("Font.png", "Metrics.xml");

//Draws the text to a new bitmap, font name is image name without extension.
image.Source = BitmapFont.DrawFont(DateTime.Now.ToLongTimeString(), "Font");

//Alternatively put these elements in a horizontal StackPanel, or ItemsControl
//This doesn't create any new bitmaps and should be more efficient.
//You could alter the method to transform each letter too.
BitmapFont.GetElements(DateTime.Now.ToLongTimeString(), "Font");

如果您希望模糊处理,请参阅BoxBlur实现here或使用WriteableBitmapEx.Convolute

票数 12
EN

Stack Overflow用户

发布于 2010-11-28 17:32:23

应制作要为其提供光晕的TextBlock的副本。将新项的text属性绑定到原始项的text属性(使用ElementName绑定)。对location / height / width等或您认为会在原始项目上发生更改的任何其他属性执行相同的操作。设置新项目的透明度以及模糊效果。这将给你一个你想要的很酷的光晕效果。

票数 0
EN

Stack Overflow用户

发布于 2011-03-26 08:26:45

几种方法

Using a Border for a Glow Effect in Silverlight 3

Creating a Glowing effect in Silverlight

Glow effect in Silverlight

我相信所有这些都可以在WP7中使用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3719750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档