Windows Phone 8.1 - 通过 WriteableBitmap 写入文本 [英] Windows Phone 8.1 - Write text over WriteableBitmap

查看:25
本文介绍了Windows Phone 8.1 - 通过 WriteableBitmap 写入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在制作的应用程序中,我需要在 WriteableBitmap 上写入文本.在 Windows Phone Silverlight 中,我知道该怎么做:我只需要调用 writeableBitmap.Render() 方法,仅此而已!问题是我的应用程序需要使用 Windows 运行时构建,而该方法不存在.我的第一种方法是编写一个在 WriteableBitmap 上执行数字绘制的方法(我只需要绘制数字),但是,使用这种方法,数字看起来有点机器人",它们都是方形的,它们看起来绝对不是非常好.那么,有什么解决方案可以完成我想要的吗?我的意思是:你能帮我完成我画漂亮数字的方法吗?或者...您知道有什么方法可以将字体符号转换为 png,然后调用 writeableBitmap.Blit() 然后相应地调整该字体元素(每个数字)的大小?

In an app that I'm making I need to write text over a WriteableBitmap. In Windows Phone Silverlight I know how to do it: I only need to call writeableBitmap.Render() method, that's all! The problema is that my app needs to be built with Windows Runtime, and that method doesn't exist. My first approach was to write a method that performs drawing of numbers (I only need to draw numbers) over the WriteableBitmap, but, with this method, numbers appear a Little "robotic", they are all square and they definitely don't look really nice. So, is there any solution to accomplish what I want? I mean: could you help me finishing my method to draw nice-looking numbers? or... do you know any way to covert font symbols into png, and then call writeableBitmap.Blit() and then resize that font element (each number) accordingly?

提前致谢.

我的代码如下:

private void DrawStringAsLines(string text, WriteableBitmap writBmp, int startX, int startY)
    {
        int x = startX;
        int y = startY;
        //int radius = 1;
        int size = 2;
        int rectangleHeight = writBmp.PixelHeight / 40;
        int rectangleWidth = writBmp.PixelWidth / 40;

        int whiteSpaceSize = rectangleWidth / 2;

        x -= ((rectangleWidth + whiteSpaceSize) * text.Length);
        y -= (rectangleHeight + whiteSpaceSize + size);

        foreach (char c in text)
        {
            switch (c)
            {
                case '0':
                    {
                        /*writBmp.DrawLine(10, 20, 50, 20, Colors.White);
                        writBmp.DrawLine(10, 20, 10, 70, Colors.White);
                        writBmp.DrawLine(10, 70, 50, 70, Colors.White);
                        writBmp.DrawLine(50, 20, 50, 70, Colors.White);*/

                        /*writBmp.DrawLineAa(x, y, x + 20, y, Colors.Yellow);
                        writBmp.FillEllipseCentered(x, y, 5, 5, Colors.Yellow);
                        writBmp.DrawLineAa(x, y, x, y + 20, Colors.Yellow);
                        writBmp.FillEllipseCentered(x, y, 5, 5, Colors.Yellow);
                        writBmp.DrawLineAa(x, y + 20, x + 20, y + 20, Colors.Yellow);
                        writBmp.FillEllipseCentered(x, y + 20, 5, 5, Colors.Yellow);
                        writBmp.DrawLineAa(x + 20, y, x + 20, y + 20, Colors.Yellow);
                        writBmp.FillEllipseCentered(x + 20, y, 5, 5, Colors.Yellow);*/



                        /*writBmp.DrawLineAa(x, y, x+20, y, Colors.Yellow);


                        //for (int i = 0; i <= 20; i++ )
                         //   writBmp.FillEllipseCentered(x+i, y, radius, radius, Colors.Yellow);

                        writBmp.DrawLineAa(x, y, x, y + 20, Colors.Yellow);

                        //for (int i = 0; i <= 20; i++)
                          //  writBmp.FillEllipseCentered(x, y+i, radius, radius, Colors.Yellow);

                        writBmp.DrawLineAa(x, y + 20, x + 20, y + 20, Colors.Yellow);

                        //for (int i = 0; i <= 20; i++)
                          //  writBmp.FillEllipseCentered(x+i, y + 20, radius, radius, Colors.Yellow);

                        writBmp.DrawLineAa(x + 20, y, x + 20, y + 20, Colors.Yellow);

                        //for (int i = 0; i <= 20; i++)
                          //  writBmp.FillEllipseCentered(x + 20, y + i, radius, radius, Colors.Yellow);*/

                        writBmp.FillRectangle(x, y, x + rectangleWidth + size, y + size, Colors.Yellow);
                        writBmp.FillRectangle(x, y, x + size, y + rectangleHeight, Colors.Yellow);
                        writBmp.FillRectangle(x + rectangleWidth + size, y, x + rectangleWidth + size + size, y + rectangleHeight, Colors.Yellow);
                        writBmp.FillRectangle(x, y + rectangleHeight, x + rectangleWidth + size, y + rectangleHeight + size, Colors.Yellow);
                        break;
                    }

                case '1':
                    {
                        /*writBmp.DrawLineAa(x, y, x, y+20, Colors.Yellow);

                        //for (int i = 0; i <= 20; i++)
                          //  writBmp.FillEllipseCentered(x, y + i, radius, radius, Colors.Yellow);

                        writBmp.DrawLineAa(x, y, x - 10, y + 10, Colors.Yellow);

                        //for (int i = 0; i <= 10; i++)
                          //  writBmp.FillEllipseCentered(x - i, y + i, radius, radius, Colors.Yellow);*/
                        writBmp.FillRectangle(x, y, x + (rectangleWidth / 2) + size, y + size, Colors.Yellow);
                        writBmp.FillRectangle(x + (rectangleWidth / 2), y, x + (rectangleWidth / 2) + size, y + rectangleHeight + size, Colors.Yellow);
                        break;
                    }
                case '2':
                    {
                        /*writBmp.DrawLineAa(x, y, x + 20, y, Colors.Yellow);
                        //for (int i = 0; i <= 20; i++)
                          //  writBmp.FillEllipseCentered(x + i, y, radius, radius, Colors.Yellow);

                        writBmp.DrawLineAa(x + 20, y, x + 20, y + 10, Colors.Yellow);
                        //for (int i = 0; i <= 10; i++)
                          //  writBmp.FillEllipseCentered(x + 20, y + i, radius, radius, Colors.Yellow);
                        writBmp.DrawLineAa(x + 20, y + 10, x, y + 10, Colors.Yellow);
                        //for (int i = 0; i <= 20; i++)
                          //  writBmp.FillEllipseCentered(x + i, y + 10, radius, radius, Colors.Yellow);
                        writBmp.DrawLineAa(x, y + 10, x, y + 20, Colors.Yellow);
                        //for (int i = 0; i <= 10; i++)
                          //  writBmp.FillEllipseCentered(x, y + 10 + i, radius, radius, Colors.Yellow);
                        writBmp.DrawLineAa(x, y + 20, x + 20, y + 20, Colors.Yellow);
                        //for (int i = 0; i <= 20; i++)
                          //  writBmp.FillEllipseCentered(x + i, y + 20, radius, radius, Colors.Yellow);*/
                        writBmp.FillRectangle(x, y, x + rectangleWidth, y + size, Colors.Yellow);
                        writBmp.FillRectangle(x + rectangleWidth, y, x + rectangleWidth + size, y + (rectangleHeight / 2) + size, Colors.Yellow);
                        writBmp.FillRectangle(x, y + (rectangleHeight / 2), x + rectangleWidth, y + (rectangleHeight / 2) + size, Colors.Yellow);
                        writBmp.FillRectangle(x, y + (rectangleHeight / 2), x + size, y + rectangleHeight + size, Colors.Yellow);
                        writBmp.FillRectangle(x, y + rectangleHeight, x + rectangleWidth + size, y + rectangleHeight + size, Colors.Yellow);
                        break;
                    }
                case '3':
                    {
                        /*writBmp.DrawLineAa(x, y, x + 20, y, Colors.Yellow);
                        //for (int i = 0; i <= 20; i++)
                          //  writBmp.FillEllipseCentered(x + i, y, radius, radius, Colors.Yellow);
                        writBmp.DrawLineAa(x + 20, y, x + 20, y + 10, Colors.Yellow);
                        //for (int i = 0; i <= 10; i++)
                          //  writBmp.FillEllipseCentered(x + 20, y + i, radius, radius, Colors.Yellow);
                        writBmp.DrawLineAa(x + 20, y + 10, x, y + 10, Colors.Yellow);
                        //for (int i = 0; i <= 20; i++)
                          //  writBmp.FillEllipseCentered(x + i, y + 10, radius, radius, Colors.Yellow);
                        writBmp.DrawLineAa(x + 20, y + 10, x + 20, y + 20, Colors.Yellow);
                        //for (int i = 0; i <= 10; i++)
                          //  writBmp.FillEllipseCentered(x + 20, y + 10 + i, radius, radius, Colors.Yellow);
                        writBmp.DrawLineAa(x + 20, y + 20, x, y + 20, Colors.Yellow);
                        //for (int i = 0; i <= 20; i++)
                            //writBmp.FillEllipseCentered(x + i, y + 20, radius, radius, Colors.Yellow);*/
                        writBmp.FillRectangle(x, y, x + rectangleWidth, y + size, Colors.Yellow);
                        writBmp.FillRectangle(x + rectangleWidth, y, x + rectangleWidth + size, y + (rectangleHeight / 2), Colors.Yellow);
                        writBmp.FillRectangle(x + (rectangleWidth / 2), y + (rectangleHeight / 2), x + rectangleWidth, y + (rectangleHeight / 2) + size, Colors.Yellow);
                        writBmp.FillRectangle(x + rectangleWidth, y + (rectangleHeight / 2), x + rectangleWidth + size, y + rectangleHeight + size, Colors.Yellow);
                        writBmp.FillRectangle(x, y + rectangleHeight, x + rectangleWidth, y + rectangleHeight + size, Colors.Yellow);
                        break;
                    }
                case '4':
                    {
                        /*writBmp.DrawLineAa(x, y, x, y + 10, Colors.Black);
                        //for (int i = 0; i <= 10; i++)
                           // writBmp.FillEllipseCentered(x, y + i, radius, radius, Colors.Yellow);
                        writBmp.DrawLineAa(x, y + 10, x + 20, y + 10, Colors.Black);
                        //for (int i = 0; i <= 20; i++)
                            //writBmp.FillEllipseCentered(x + i, y + 10, radius, radius, Colors.Yellow);
                        writBmp.DrawLineAa(x + 20, y, x + 20, y + 20, Colors.Black);
                        //for (int i = 0; i <= 20; i++)
                            //writBmp.FillEllipseCentered(x + 20, y + i, radius, radius, Colors.Yellow);*/

                        writBmp.FillRectangle(x, y, x + size, y + (rectangleHeight / 2), Colors.Yellow);
                        writBmp.FillRectangle(x, y + (rectangleHeight / 2), x + rectangleWidth, y + (rectangleHeight / 2) + size, Colors.Yellow);
                        writBmp.FillRectangle(x + rectangleWidth, y, x + rectangleWidth + size, y + rectangleHeight + size, Colors.Yellow);
                        break;
                    }
                case '5':
                    {
                        /*writBmp.DrawLine(x, y, x + 20, y, Colors.Yellow);
                        for (int i = 0; i <= 10; i++)
                            writBmp.FillEllipseCentered(x + i*2, y, radius*2, radius*2, Colors.Yellow);
                        writBmp.DrawLine(x, y, x, y + 10, Colors.Yellow);
                        for (int i = 0; i <= 5; i++)
                            writBmp.FillEllipseCentered(x, y + i*2, radius*2, 2*radius, Colors.Yellow);
                        writBmp.DrawLine(x, y + 10, x + 20, y + 10, Colors.Yellow);
                        for (int i = 0; i <= 10; i++)
                            writBmp.FillEllipseCentered(x + 2*i, y + 10, 2*radius, 2*radius, Colors.Yellow);
                        writBmp.DrawLine(x + 20, y + 10, x + 20, y + 20, Colors.Yellow);
                        for (int i = 0; i <= 5; i++)
                            writBmp.FillEllipseCentered(x + 20, y + 10 + 2*i, 2*radius, radius*2, Colors.Yellow);
                        writBmp.DrawLine(x + 20, y + 20, x, y + 20, Colors.Yellow);
                        for (int i = 0; i <= 10; i++)
                            writBmp.FillEllipseCentered(x + 2*i, y + 20, radius*2, radius*2, Colors.Yellow);*/
                        writBmp.FillRectangle(x, y, x + rectangleWidth + size, y + size, Colors.Yellow);
                        writBmp.FillRectangle(x, y, x + size, y + (rectangleHeight / 2), Colors.Yellow);
                        writBmp.FillRectangle(x, y + (rectangleHeight / 2), x + rectangleWidth, y + (rectangleHeight / 2) + size, Colors.Yellow);
                        writBmp.FillRectangle(x + rectangleWidth, y + (rectangleHeight / 2), x + rectangleWidth + size, y + rectangleHeight, Colors.Yellow);
                        writBmp.FillRectangle(x, y + rectangleHeight, x + rectangleWidth + size, y + rectangleHeight + size, Colors.Yellow);
                        break;
                    }
                case '6':
                    {
                        writBmp.FillRectangle(x, y, x + rectangleWidth, y + size, Colors.Yellow);
                        writBmp.FillRectangle(x, y, x + size, y + rectangleHeight, Colors.Yellow);
                        writBmp.FillRectangle(x, y + (rectangleHeight / 2), x + rectangleWidth, y + (rectangleHeight / 2) + size, Colors.Yellow);
                        writBmp.FillRectangle(x + rectangleWidth, y + (rectangleHeight / 2), x + rectangleWidth + size, y + rectangleHeight, Colors.Yellow);
                        writBmp.FillRectangle(x, y + rectangleHeight, x + rectangleWidth, y + rectangleHeight + size, Colors.Yellow);
                        break;
                    }
                case '7':
                    {
                        writBmp.FillRectangle(x, y, x + rectangleWidth, y + size, Colors.Yellow);
                        writBmp.FillRectangle(x + rectangleWidth, y, x + rectangleWidth + size, y + rectangleHeight + size, Colors.Yellow);
                        writBmp.FillRectangle(x + (rectangleWidth / 2), y + (rectangleHeight / 2), x + rectangleWidth, y + (rectangleHeight / 2) + size, Colors.Yellow);
                        break;
                    }
                case '8':
                    {
                        writBmp.FillRectangle(x, y, x + rectangleWidth, y + size, Colors.Yellow);
                        writBmp.FillRectangle(x, y, x + size, y + (rectangleHeight / 2), Colors.Yellow);
                        writBmp.FillRectangle(x + rectangleWidth, y, x + rectangleWidth + size, y + (rectangleHeight / 2), Colors.Yellow);
                        writBmp.FillRectangle(x, y + (rectangleHeight / 2), x + rectangleWidth, y + (rectangleHeight / 2) + size, Colors.Yellow);
                        writBmp.FillRectangle(x, y + (rectangleHeight / 2), x + size, y + rectangleHeight, Colors.Yellow);
                        writBmp.FillRectangle(x + rectangleWidth, y + (rectangleHeight / 2), x + rectangleWidth + size, y + rectangleHeight, Colors.Yellow);
                        writBmp.FillRectangle(x, y + rectangleHeight, x + rectangleWidth, y + rectangleHeight + size, Colors.Yellow);
                        break;
                    }
                case '9':
                    {
                        writBmp.FillRectangle(x, y, x + rectangleWidth, y + size, Colors.Yellow);
                        writBmp.FillRectangle(x, y, x + size, y + (rectangleHeight / 2), Colors.Yellow);
                        writBmp.FillRectangle(x + rectangleWidth, y, x + rectangleWidth + size, y + rectangleHeight + size, Colors.Yellow);
                        writBmp.FillRectangle(x, y + (rectangleHeight / 2), x + rectangleWidth, y + (rectangleHeight / 2) + size, Colors.Yellow);
                        break;
                    }
                case '/':
                    {
                        writBmp.DrawLine(x + (rectangleWidth / 2), y + rectangleHeight, x + rectangleWidth, y, Colors.Yellow);
                        var increment = (rectangleWidth / 10);
                        writBmp.DrawLine(x + (rectangleWidth / 2) + increment, y + rectangleHeight, x + rectangleWidth + increment, y, Colors.Yellow);
                        increment += increment;
                        writBmp.DrawLine(x + (rectangleWidth / 2) + increment, y + rectangleHeight, x + rectangleWidth + increment, y, Colors.Yellow);

                        break;
                    }
            }

            x += (rectangleWidth + whiteSpaceSize);
        }
    }

推荐答案

与 WriteableBitmap.Render for Windows Runtime 应用程序类似的功能是 RenderTargetBitmap.在位图上绘制文本的最简单方法是将位图和带有文本的 TextBlock 放在 Grid 或 Canvas 中,然后在容器 Canvas 上调用 RenderTargetBitmap 将组合的图像 + 文本渲染为新位图.

The analogous functionality to WriteableBitmap.Render for Windows Runtime apps is RenderTargetBitmap. The easiest way to draw text on a bitmap is to put your bitmap and a TextBlock with the text in a Grid or Canvas then call RenderTargetBitmap on the container Canvas to render the combined image + text into a new bitmap.

其他替代方案是与 DirectWrite 互操作以在 DirectX 层绘制文本,或使用外部光栅图形包,例如 Win2D 或 WriteableBitmapEx(我不确定 WriteableBitmapEx 是否可以在 Windows Phone 上处理文本).

Other alternates are to interop with DirectWrite to draw text at the DirectX layer, or to use an external raster graphics package such as Win2D or WriteableBitmapEx (I'm not sure if WriteableBitmapEx can do text on Windows Phone).

这篇关于Windows Phone 8.1 - 通过 WriteableBitmap 写入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆