I have a Grid in a user control:
<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneAccentBrush}">
<Image
x:Name="imgBack"
Opacity=".25" />
<Grid VerticalAlignment="Top" HorizontalAlignment="Left">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<!-- Some textblocks -->
</Grid>
The size of the grid & source of the image is set dynamically at runtime:
public void ChangeSizeAndImage(int width, int height, string backImagePath)
{
// Set the height and width
LayoutRoot.Width = width;
LayoutRoot.Height = height;
// Set the background image
imgBack.Source = new BitmapImage(new Uri(backImagePath, UriKind.RelativeOrAbsolute));
this.UpdateLayout();
}
I then call a method that generates a jpeg from the ui element & saves it to IsolatedStorage (later adding to a Windows Phone Live Tile)
public static Uri CreateImageFromUIElement(UIElement element, int width, int height)
{
// Instance to store the image in
IsolatedStorageFile userStore = IsolatedStorageFile.GetUserStoreForApplication();
// Random ID to use for the file name
string id = Guid.NewGuid().ToString("n");
// create directory to read/write from
const string shellContentDirectory = "Shared\\ShellContent";
userStore.CreateDirectory(shellContentDirectory);
// Render the UIElement into a writeable bitmap
WriteableBitmap bitmap = new WriteableBitmap(element, new CompositeTransform());
// Save the bitmap to a file. This stream must be closed in order to be read later
string imagePath = shellContentDirectory + "\\" + id + ".jpg";
using (IsolatedStorageFileStream stream = userStore.OpenFile(imagePath, System.IO.FileMode.Create))
{
bitmap.SaveJpeg(stream, width, height, 0, 100);
}
// Uri to get us to the image
Uri imageUri = new Uri("isostore:/" + imagePath.Replace('\\', '/'));
return imageUri;
}
I'm able to successfully use the image on the tile, except I only see the image with a black background, and I don't see any of the text. I'm thinking that this has something to do with jpeg's and transparent PNG's, however, I'm setting the background on the Grid above with a color, so I'm not sure why it would be considered to be transparent.
Edit 1
I am initializing this user control programmatically, setting some properties, and passing it to the create image method. I've found that if I add this user control to a page, then use call the create image method, it works just fine. There must be something about it rendering on a page that causes everything to line up properly. Unfortunately, this approach isn't possible in my situation. Is there a step I can do to get this to work? UpdateLayout();
on the control doesn't work.