Select to view content in your preferred language

Export map to png

5597
12
01-11-2011 09:19 AM
NancyHernandez
Emerging Contributor
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 error

Mensaje: 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?
0 Kudos
12 Replies
ClydeFord
Deactivated User
You may have already found the answer to your questions but in your SL XAML you should put the 'proxy.ashx' file as the URL for ProxlURL such as:

<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer" ProxyURL="proxy.ashx" />
0 Kudos
GalTalmor
Emerging Contributor
You may have already found the answer to your questions but in your SL XAML you should put the 'proxy.ashx' file as the URL for ProxlURL such as:

<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer" Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer" ProxyURL="proxy.ashx" />


OK... but where am I put this proxy.ahsx file?
What is its content? I tried to add an ashx file to the Web project (and change only the name), and then add this definition (ProxyURL="proxy.ashx") to the layer.. and the application loaded to 100% but I see nothing.
Is there another thing that I need to do?
Thank you..!
0 Kudos
BrianGustafson
Occasional Contributor
Can this be done with Bing layers or do I need to swap them out with something else when an image is being saved?
0 Kudos