I need to generate an image file containing a Grid with a Diagram object inside of it.
I’m using Silverlight 5 and GoSilverlight 1.3.3.5.
Basically I need my output to look like this (the Diagram is outlined in Red):
With the code that I’ve written so far, this is the result that I’m getting:
So it seems like maybe templates are not being applied or rendered before the bitmap is created.
I’ve tried Diagram.Panel.UpdateLayout() and Diagram.Panel.ApplyTemplate() before generating the bitmap object and these don’t seem to help.
My diagram object looks like this:
<ControlTemplate x:Key="WiringDiagramTemplate" TargetType="go:Diagram">
<ScrollViewer HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto"
Padding="0"
BorderThickness="0">
<go:DiagramPanel x:Name="Panel"
Stretch="{TemplateBinding Stretch}"
Padding="{TemplateBinding Padding}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
</ScrollViewer>
</ControlTemplate>
<DataTemplate x:Key="WireTemplate">
<go:LinkShape Stroke="{Binding Data.WireColor}" StrokeThickness="2"
go:Part.SelectionAdorned="True"
go:Part.Reshapable="True" Loaded="Wire_Loaded" >
<go:Link.Route>
<go:Route Routing="Orthogonal" RelinkableFrom="True" RelinkableTo="True" />
</go:Link.Route>
</go:LinkShape>
</DataTemplate>
....
<go:Diagram x:Name="myWiring" Padding="10"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Template="{StaticResource WiringDiagramTemplate}"
LinkTemplate="{StaticResource WireTemplate}"
Margin="0"
Height="170"
/>
and I’m generating the image by:
foreach (Link link in myDiagram.Links)
{
Cable cable = (Cable)link.Data;
myDiagram.SelectedLink = null;
var model = cable.Model;
if (model == null)
{
model = new WireModel();
model.NodesSource = cable.Pins;
model.LinksSource = cable.Wires;
}
myWiring.Model = model;
UIElement element = grdmyWiring;
double width = ((FrameworkElement)element).ActualWidth;
double height = ((FrameworkElement)element).ActualHeight;
WriteableBitmap bitmap = new WriteableBitmap(Convert.ToInt32(Math.Round(width)), Convert.ToInt32(Math.Round(height)));
bitmap.Render(element, null);
bitmap.Invalidate();
PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(bitmap));
using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(cable.From + cable.FromPort + "_" + cable.To + cable.ToPort + ".png", FileMode.Create, isoFile))
{
png.Save(isoStream);
}
}
}
Is there a trick to updating the Diagram to prepare it for being rendered as a bitmap after I apply a model to it?