【问题标题】:Windows Phone 8.1 - Write text over WriteableBitmapWindows Phone 8.1 - 在 WriteableBitmap 上写入文本
【发布时间】:2015-02-13 04:17:15
【问题描述】:

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

提前致谢。

我的代码如下:

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);
        }
    }

【问题讨论】:

  • 如何将 UserControl(或只是 TextBlock)渲染到图像而不是手动“绘制”数字?
  • 您的意思是使用 RenderTargetBitmap 将 TextBlock 渲染为图像,然后调用 writeableBitmap.Blit() 将 TextBlock 放在图像上?如果是这样,你能帮我完成这个过程吗?我已经做过了,但是数字看起来很大。
  • 我的意思是@RobCaplan 的建议。制作一个看起来像您想要的方式的 UserControl(使用背景图像、文本,以及您需要的任何内容),对其进行初始化,然后使用 RenderTargetBitmap 从该 UserControl 创建图像。基本上,您想要在结果图像上的所有内容都应该在该 UserControl 中,因此一旦您渲染它 - 您将拥有您想要的图像。希望有帮助。 :)
  • 是的,我已经试过了。在 Windows 8.1 上它按预期工作,但在 Windows Phone 8.1 上它不起作用。见我的另一个问题stackoverflow.com/questions/28501503/…

标签: c# windows-runtime windows-phone windows-phone-8.1


【解决方案1】:

与适用于 Windows 运行时应用的 WriteableBitmap.Render 类似的功能是 RenderTargetBitmap。在位图上绘制文本最简单的方法是将您的位图和带有文本的 TextBlock 放在 Grid 或 Canvas 中,然后在容器 Canvas 上调用 RenderTargetBitmap 以将组合的图像 + 文本渲染到新的位图中。

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

【讨论】:

  • 谢谢!我使用 RenderTargetBitmap 将组合的图像 + 文本渲染为 jpg 文件,然后将其保存到手机的相机胶卷中。在 Windows 8.1 上一切正常,但在 Windows Phone 8.1 上却不行:它只显示 TextBlock 几乎占据了整个屏幕,而它后面只有一半左右的图像。在这里查看我的问题stackoverflow.com/questions/28501503/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多