PictureMarkerSymbol's Source from SymbolDictionary

2653
1
11-06-2012 08:49 AM
Labels (1)
RonVincent
Occasional Contributor
I'm trying to get the image from the SymbolDictionary and use it on a PictureMarkerSymbol. When I run my app it generates an error. Here is the code:

SymbolDictionary symbolDictionary = new SymbolDictionary(SymbolDictionaryType.Mil2525C);

 IEnumerable<string> enumSymbol = new List<string>() { "SHGP-----------" };
IList<SymbolProperties> list = symbolDictionary.FindSymbols(enumSymbol);

 ImageSource imageSource = list[0].GetImage(30, 30);

PictureMarkerSymbol pictureMarkerSymbol2 = new PictureMarkerSymbol()
{
Source = imageSource
 };



Here is the error that I get once my app starts:

{"Specified method is not supported."}

There is not an error if I step right over the Source. In fact, it seems OK. It's only when the app is nearly open that this error occurs.

Thanks,

Ron
0 Kudos
1 Reply
InnesMacKenzie
New Contributor

{"Specified method is not supported."}


Unfortunately this way of creating a PictureMarkerSymbol does not work in accelerated display, as the ImageSource object does not have a Url from which the image's raw image bytes can be obtained.

To work around this problem you could save the ImageSource that you get from SymbolProperties.GetImage to disk as a PNG file, and then create a PictureMarkerSymbol with an ImageSource with a Uri set to the file path of the saved image file.

For example:

//helper method for getting PNG bytes for a BitmapSource

  static byte[] GetPngBytes(BitmapSource image)
  {
   using (MemoryStream ms = new MemoryStream())
   {
    PngBitmapEncoder encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(image));
    encoder.Save(ms);
    byte[] buffer = ms.GetBuffer();
    return buffer;
   }
  }

  ...

  ImageSource imageSource = list[0].GetImage(30, 30);

  string pngPath = "...";

  File.WriteAllBytes(pngPath, GetPngBytes((BitmapSource)imageSource));

  var s = new PictureMarkerSymbol {Source=new BitmapImage(new Uri(pngPath)) };

  ...
0 Kudos