Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way for get Silverlight UI Element as an image?

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?

like image 335
Cem Sönmez Avatar asked Mar 05 '13 13:03

Cem Sönmez


1 Answers

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;
}
like image 91
Dreamwalker Avatar answered Nov 07 '22 13:11

Dreamwalker