Hello!! I want to export a control map to png image format, I found this page http://geekswithblogs.net/braulio/archive/2009/07/12/export-canvas-to-png-and-save-it-in-your-local....where describe the way to export the content of a canvas control, like this:[HTML]<Canvas x:Name="canvasToExport" Width="300" Height="180"> <Rectangle Canvas.Left="10" Canvas.Top="10" Fill="DarkBlue" Width="200" Height="100" Stroke="Blue" StrokeThickness="5"> </Rectangle> <Ellipse Canvas.Left="50" Canvas.Top="50" Height="100" Width="200" StrokeThickness="5" Stroke="Black" Fill="Gold"/> </Canvas> <Button Content="Export canvas to PNG" x:Name="btnExport" Width="200" Margin="5" Click="btnExport_Click"/>[/HTML] private void btnExport_Click(object sender, RoutedEventArgs e)
{
CanvasToPNG canvasToPNG = new CanvasToPNG();
// It will export to PNG the canvas content
// parameter canvas ID
canvasToPNG.ShowSaveDialog(canvasToExport);
}
and the class CanvasToPNG is public class CanvasToPNG
{
public void ShowSaveDialog(Canvas canvasToExport)
{
// Instantiate SaveFileDialog
// and set defautl settings (just PNG export)
SaveFileDialog sfd = new SaveFileDialog()
{
DefaultExt = "png",
Filter = "Png files (*.png)|*.png|All files (*.*)|*.*",
FilterIndex = 1
};
if (sfd.ShowDialog() == true)
{
SaveAsPNG(sfd, canvasToExport);
}
}
private void SaveAsPNG(SaveFileDialog sfd, Canvas canvasToExport)
{
WriteableBitmap bitmap = new WriteableBitmap(canvasToExport, new TranslateTransform());
EditableImage imageData = new EditableImage(bitmap.PixelWidth, bitmap.PixelHeight);
try
{
for (int y = 0; y < bitmap.PixelHeight; ++y)
{
for (int x = 0; x < bitmap.PixelWidth; ++x)
{
int pixel = bitmap.Pixels[bitmap.PixelWidth * y + x];
imageData.SetPixel(x, y,
(byte)((pixel >> 16) & 0xFF),
(byte)((pixel >> 8) & 0xFF),
(byte)(pixel & 0xFF), (byte)((pixel >> 24) & 0xFF)
);
}
}
}
catch (System.Security.SecurityException)
{
throw new Exception("Cannot print images from other domains");
}
// Save it to disk
Stream pngStream = imageData.GetStream();
StreamReader sr = new StreamReader(pngStream);
byte[] binaryData = new Byte[pngStream.Length];
long bytesRead = pngStream.Read(binaryData, 0, (int)pngStream.Length); using (Stream stream = sfd.OpenFile())
{
stream.Write(binaryData, 0, binaryData.Length);
stream.Close();
}
}
}
I rename the canvas control by the control map, when the project is running does not send error messages, but the website throws this errorMensaje: Unhandled Error in Silverlight 2 Application Cannot print images from other domains en ESRI.ArcGIS.Samples.SilverMapDemo.ExportarToPNG.SaveAsPNG(SaveFileDialog sfd, Map Map) en ESRI.ArcGIS.Samples.SilverMapDemo.ExportarToPNG.ShowSaveDialog(Map Map) en ESRI.ArcGIS.Samples.SilverMapDemo.Page.btnabrir_Click(Object sender, RoutedEventArgs e) en System.Windows.Controls.Primitives.ButtonBase.OnClick() en System.Windows.Controls.Button.OnClick() en System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) en System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e) en MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)What I should do?