简体   繁体   English

在WP7中写入图像上的文本

[英]Write text on image in WP7

I am new to WP7 development and I would like to know how can I write text to image? 我是WP7开发的新手,我想知道如何将文字写入图像?

First is it possible to do so? 首先是可以这样做吗?

As in GDI we can write text to image as shown below: 与在GDI中一样,我们可以将文本写入图像,如下所示:

Dim pth As New GraphicsPath()
pth.AddString(txtSample.Text, New FontFamily(DropFont.SelectedValue), 0, Integer.Parse(DropFontSize.SelectedValue), New Point(left, top), StringFormat.GenericTypographic)

But in WP7 as I came to know that GDI is not supported.So how Can I do this? 但是在WP7中,我开始知道GDI不受支持。所以我该怎么做?

Edit: 编辑:

I need to select an image from the pictures hub or take a picture using camera and display it in an image control and write some text and save back with a different name. 我需要从pictures hub选择图像或使用camera并将其显示在图像控件中并写入一些文本并使用其他名称保存。

Any suggestions are most welcome. 任何建议都是最受欢迎的。

You need to get hold of a WriteableBitmap, which can then be manipulated. 您需要获取WriteableBitmap,然后可以对其进行操作。

This can be done by either adding a UIElement using the Render method or you can manipulate the pixels directly using the Pixels array. 这可以通过使用Render方法添加UIElement来完成,也可以使用Pixels数组直接操作Pixels

You probably only need to add TextBlock elements to the bitmap, but if you are curious about pixel manipulation here is how that is done: 您可能只需要将TextBlock元素添加到位图中,但如果您对像素操作感到好奇,请执行以下操作:

I have only experience with pixel manipulation. 我只有像素操作的经验。 This is not entirely straight forward, but you access pixel (x, y) in the one-dimensional array by translating y * width + x . 这不完全是直截了当的,但是通过平移y * width + x可以访问一维数组中的像素(x, y)

The value is in a format called argb32 , ie values for alpha-channel (opacity), red, green and blue. 该值采用名为argb32的格式,即alpha通道(不透明度),红色,绿色和蓝色的值。 Translation between regular Color and argb32 below: 常规Color和argb32之间的翻译如下:

    int ColorToInt(Color c)
    {
        var argb32 = c.A << 24 | c.R << 16 | c.G << 8 | c.B;
        return argb32;
    }

    Color IntToColor(int argb32)
    {
        const int mask = 0x000000FF;
        byte a, r, g, b;
        a = (byte)((argb32 >> 24) & mask);
        r = (byte)((argb32 >> 16) & mask);
        g = (byte)((argb32 >> 8) & mask);
        b = (byte)(argb32 & mask);
        return Color.FromArgb(a, r, g, b);
    }

Why do you need them embedded in the Image? 为什么你需要在图像中嵌入它们?

You could simply place your image and Text in a Grid such as: 您可以简单地将图像和文本放在网格中,例如:

<grid>
   <image source="YourImageSource"/>
   <TextBlock Text="Your Text Here"/>
</grid>

That will overlay your image with Text without having to modify the image so you can use it later. 这将使您的图像与文本重叠,而无需修改图像,以便以后使用它。 It also provides more freedom with bindings etc as you can bind both to different things and switch them in and out independently. 它还提供了更多的绑定等自由,因为您可以将它们绑定到不同的东西并独立地切换它们。

If you are using XNA this can also be done by manipulating the Pixels of the Texture2D the same way as faester said. 如果你正在使用XNA,这也可以通过操纵Texture2D的像素来完成,就像faester所说的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM