Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XAML how to float text over image

I have some images that I want to display with a watermark.

Currently they are within a stackpanel as follows:

<StackPanel Orientation="Vertical"
                      Margin= "7,0,0,0" 
                      HorizontalAlignment="Center" >
            <Image Width="60"
                   Height="72"
                   VerticalAlignment="Top"
                   Margin="0 0 10 0"
                   Source="{Binding ImageToWatermark}" />

What xaml would I use to float a centered text over an image?

For example, to display London over a picture of the city with this "Segoe Keycaps" font.

London

like image 652
Noah Avatar asked Jul 07 '11 05:07

Noah


People also ask

How do I add text to an image in XAML?

If you want add text to on Image you can crate XAML Image control and XAML TextBlock and add them as some UIContainer child element (For example as UIContainer use Grid). You can save any of XAML VisualTree node to image file using RenderTargetBitmap.


2 Answers

Use <Grid> or <Canvas> instead of <StackPanel> and items will be drawn upon each other.

like image 193
elder_george Avatar answered Oct 17 '22 09:10

elder_george


I've added some sample code in case it helps.

<DataTemplate x:Key="ImageBackgroundBlackBorderedTextTemplate">
            <Grid Height="Auto" Margin="2,5,2,5">
                <Image Stretch="Fill" Source="{Binding ImageUrl}" />
                <Border Background="#80000000" VerticalAlignment="Bottom">
                    <TextBlock  Margin="5,2,5,2" TextWrapping="WrapWholeWords" Text="{Binding Title}"  Style="{StaticResource BaseTextBlockStyle}"/>
                </Border>
            </Grid>
        </DataTemplate>
like image 31
W.K.S Avatar answered Oct 17 '22 10:10

W.K.S