Write text on image in WP7

泄露秘密 提交于 2019-11-28 01:34:40

You need to get hold of a WriteableBitmap, which can then be manipulated.

This can be done by either adding a UIElement using the Render method or you can manipulate the pixels directly using the Pixels array.

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:

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.

The value is in a format called argb32, ie values for alpha-channel (opacity), red, green and blue. Translation between regular Color and argb32 below:

    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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!