5

I want to export an image of my ArcGIS map object with the graphics layer on it. I've tried esri's own web services for export but they're not so efficient and clear, not supporting complex geometric shapes also they're not support local layers such as Google map provider. Service supports only ArcGISTiledLayer i want it in all layers. So, i searched in their forums but they say they won't support local layers until next versions.

I've tried ImageTool libraries and WritableBitmapEx libraries in codeplex. But when i try to get byte[] from a WritableBitmap i can not access its Pixels property for some security reasons all the time. Application throws a SecurityException and says that 'you can't access this pixels property'.

So, is there any way for get a UIElement control's image and save it to the disk? Or is there a workaround for this security exception?

Cem Sönmez
  • 506
  • 1
  • 3
  • 15

1 Answers1

5

Yes the image tools library has a method to do this into png/jpg etc.

http://imagetools.codeplex.com/

Also you can use RenderTargetBitmap - http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx

Here is an example of how to save a file to disk. you can only do it from a dialog

http://www.silverlightshow.net/items/Using-the-SaveFileDialog-in-Silverlight-3.aspx

EDIT - Sample Code

Calling

var objImage = new WritableBitmap(MyElement, MyElement.RenderTransform);

var bytData = objImage.ToPng();

Extension Method

using ImageTools.IO.Png;
using ImageTools;

public static byte[] ToPng(this WriteableBitmap Image)
{
    byte[] bytResult;

    using (MemoryStream objPngStream = new MemoryStream())
    {
        PngEncoder objPngEncoder = new PngEncoder();
        objPngEncoder.Encode(Image.ToImage(), objPngStream);
        objPngStream.Seek(0, SeekOrigin.Begin);
        bytResult = objPngStream.ToArray();
        objPngStream.Close();
    }
    return bytResult;
}
Dreamwalker
  • 3,032
  • 4
  • 30
  • 60
  • I do have some code to use imagetools to save a png trying to find it :) – Dreamwalker Mar 05 '13 at 13:29
  • i've tried image tools library with WriteToStream method, it doesn't work. I will try RenderTargetBitmap class. – Cem Sönmez Mar 05 '13 at 13:30
  • Saving to disk must be done with the SaveFileDialog otherwise you will get an exception bear that in mind. Still looking for the imagetools code – Dreamwalker Mar 05 '13 at 13:32
  • I'm not having any problems to save file to disk. The problem is i can't get image's byte array. – Cem Sönmez Mar 05 '13 at 13:35
  • added a sample for you. you could of course adapt this to write to a stream instead this was used in a service call in my app – Dreamwalker Mar 05 '13 at 13:46
  • It is the same issue on `objPngEncoder.Encode(Image.ToImage(), objPngStream)` line. SecurityException is thrown and message is "WriteableBitmap has protected content. Pixel access is not allowed". I coded a ToByteArray() extension method for WritableBitmap and when i try to access its Pixels property it throws the same exception. I guess we can't access this property maybe i should find another way :( – Cem Sönmez Mar 05 '13 at 14:11
  • 1
    You are getting a cross domain security issue so are been denied access to the pixel data. I am not sure there is a way around this in silverlight. – Dreamwalker Mar 05 '13 at 14:19