Select to view content in your preferred language

Uploading a map to an image

938
7
06-10-2010 06:48 AM
jonataspovoas
Regular Contributor
Hi, I am making a silverlight application in C#, and I need to get a Map in it's actual extent and rotation and put it on an Image to use in a page of print. How can I do this?
0 Kudos
7 Replies
dotMorten_esri
Esri Notable Contributor
You want to look at WriteableBitmap.Render
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap.render(v=VS.95)...
The returned bitmap can be used as the source of the image.
0 Kudos
hcgis
by
Deactivated User
0 Kudos
MarkCederholm
Frequent Contributor
Is there any way in REST/Silverlight to export a map to an image file?  WriteableBitmap won't allow access to content from a UI element that uses URL-derived properties.
0 Kudos
MarkCederholm
Frequent Contributor
Nope.  REST won't work either, because the resulting image is accessed via a URL.  Double 😞 😞  . The frustrating thing is that my app is intranet and truly isn't cross-domain.  Silverlight just thinks it is because the Map layer requires a URL.
0 Kudos
jonataspovoas
Regular Contributor
Is there any way in REST/Silverlight to export a map to an image file?  WriteableBitmap won't allow access to content from a UI element that uses URL-derived properties.


you CAN actually use writeableBitmap to get the map actual view!

What you must pay attention is that the image MUST have the same proportions of the map, or it'll be distorted.

I made a little project to demonstrate how it works.

Create a standart map application at the MSVisual Studio with the name WriteableBitmapExample and copy this.

mainpage.xaml code:
https://docs.google.com/document/edit?id=1zbnT0H1NNAZXeNiq_mPaykAz6EzMn4knJw26uIfwokI&hl=en
mainpage.xaml.cs code:
https://docs.google.com/document/edit?id=1JEhNd721VAm_4KINcmsM5ar5jEEeEdqdtOsHMDn_3Wc&hl=en
0 Kudos
MarkCederholm
Frequent Contributor
You're missing the point, which is to get access to the bytes of a WriteableBitmap in order to write out an image file, not just display it online.  Having said that, I take back my previous post.  REST is the answer, if you're returning image bytes instead of a URL.  I just tried it, created a WriteableBitmap from the request stream and it worked!  I think I finally have the workaround needed for PDF creation.

        private void BitmapTest()
        {
            string sError = "";
            try
            {
                string sURL = "http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer/export";
                string sData = "?f=image";
                int iWidth = (int)MyMap.Width;
                int iHeight = (int)MyMap.Height;
                sData += "&size=" + iWidth.ToString() + "," + iHeight.ToString();
                sData += "&bbox=-121,32,-113,36";
                Uri uri = new Uri(sURL + sData);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
                req.BeginGetResponse(new AsyncCallback(RespCallback), req);
            }
            catch (WebException e)
            {
                sError = e.Message;
            }
            catch (Exception e)
            {
                sError = e.Message;
            }
        }

        private void RespCallback(IAsyncResult ar)
        {
            string sError = "";
            try
            {
                HttpWebRequest req = (HttpWebRequest)ar.AsyncState;
                HttpWebResponse res = (HttpWebResponse)req.EndGetResponse(ar);
                if (res.StatusCode == HttpStatusCode.OK)
                {
                    string sType = res.ContentType;
                    int iLen = (int)res.ContentLength;
                    Stream stream = res.GetResponseStream();
                    this.Dispatcher.BeginInvoke(() => StreamToBitmap(stream));
                }
            }
            catch (WebException e)
            {
                sError = e.Message;
            }
            catch (Exception e)
            {
                sError = e.Message;
            }
        }

        private void StreamToBitmap(Stream s)
        {
            string sError = "";
            try
            {
                BitmapImage bmi = new BitmapImage();
                bmi.SetSource(s);
                WriteableBitmap wbm = new WriteableBitmap(bmi);
                int iWidth = wbm.PixelWidth;
                int iHeight = wbm.PixelHeight;
                int i = wbm.Pixels[0]; // Bingo!
            }
            catch (Exception e)
            {
                sError = e.Message;
            }
        }
0 Kudos
DominiqueBroux
Esri Frequent Contributor
I am glad you eventually found a workaround because the writeablebitmap limitations seem painful.

I know that it's possible to create a PDF through PrintDocument and a PDF printer (such as PDF creator). But there are UI side effects which are not acceptable in some scenarios.
0 Kudos