Add Caption for an Image
<Window x:Class="BitmapProgramming.AddCaption"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="BitmapProgramming" Height="300" Width="300"
>
<Image x:Name="imageElement" />
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace BitmapProgramming
{
public partial class AddCaption : System.Windows.Window
{
public AddCaption()
{
InitializeComponent();
BitmapImage originalBmp = new BitmapImage();
originalBmp.BeginInit();
originalBmp.UriSource = new Uri("http://www.your host.com/a.jpeg");
originalBmp.DownloadCompleted += delegate{
Grid rootGrid = new Grid();
Image img = new Image();
img.Source = originalBmp;
rootGrid.Children.Add(img);
TextBlock caption = new TextBlock();
caption.Text = "Ian's car";
caption.VerticalAlignment = VerticalAlignment.Bottom;
caption.HorizontalAlignment = HorizontalAlignment.Center;
caption.Margin = new Thickness(5);
caption.Padding = new Thickness(5);
caption.TextAlignment = TextAlignment.Center;
caption.TextWrapping = TextWrapping.Wrap;
rootGrid.Children.Add(caption);
RenderTargetBitmap bmp = new RenderTargetBitmap(originalBmp.PixelWidth, originalBmp.PixelHeight,originalBmp.DpiX, originalBmp.DpiY, PixelFormats.Pbgra32);
rootGrid.Measure(new Size(originalBmp.Width, originalBmp.Height));
rootGrid.Arrange(new Rect(0, 0, originalBmp.Width, originalBmp.Height));
bmp.Render(rootGrid);
imageElement.Source = bmp;
};
originalBmp.EndInit();
}
}
}
Related examples in the same category