在我制作的应用中,我需要在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);
}
}
答案 0 :(得分:2)
适用于Windows运行时应用程序的WriteableBitmap.Render的类似功能是RenderTargetBitmap。在位图上绘制文本的最简单方法是将位图和带有文本的TextBlock放在网格或画布中,然后在容器Canvas上调用RenderTargetBitmap,将组合图像+文本渲染为新的位图。
其他替代方法是与DirectWrite交互以在DirectX层绘制文本,或使用外部光栅图形包,例如Win2D或WriteableBitmapEx(我不确定WriteableBitmapEx是否可以在Windows上执行文本电话)。