Proper use of ConstructMarkerFromBitmapSource

06-29-2019 07:42 PM
New Contributor II

Good Evening,

I am trying to use ConstructMarkerFromBitmapSource in v2.3 of the SDK with a CIMUniqueValueRenderer to generate symbology for a series of point features. When I do so, I am getting rather cryptic COM exceptions.  Below is a snippet that results in a COM exception when applying to a feature class. Any ideas?



var r = new CIMUniqueValueRenderer
    Fields = new string[] { "type" }
//r.Groups = new CIMUniqueValueGroup[] { IncidentGroup() };
var uri = new Uri("pack://application:,,,/SamplePlugin;component/Images/blue_circle-16.png");
var img = new BitmapImage(uri);
var marker = SymbolFactory.Instance.ConstructMarkerFromBitmapSource(img) ;
var symbol = SymbolFactory.Instance.ConstructPointSymbol(marker);
r.UseDefaultSymbol = true;
r.DefaultSymbol = symbol.MakeSymbolReference();
0 Kudos
1 Reply
New Contributor II

Did some more investigation this morning. It appears to be due to ConstructMarkerFromBitmapSource assuming the underlying encoding is a bitmap. Unfortunately for me, I was using png's/gif's, and I'm guessing somewhere in the C/C++ backend, the code is trying to parse my png or gif as a bmp. Interestingly enough, the ConstructMarkerFromFile function seems to work fine, so somewhere in there it must be properly testing the extension or parsing the codec before converting it to the URL format that is expected by CIMPictureMarker...

At any rate, I circumvented the issue by using CIMPictureMarker, and then forming the correct URL string expected by testing the codec type. Hopefully the snippet below will save someone else some time...

private static CIMPictureMarker GetMarker(string type)
    Uri uri;
    if (type == "Type I")
        uri = new Uri("pack://application:,,,/ExamplePlugin;component/Images/type1.gif");
    else if (type == "Type II")
        uri = new Uri("pack://application:,,,/ExamplePlugin;component/Images/type2.png");
    else if (type == "Type III")
        uri = new Uri("pack://application:,,,/ExamplePlugin;component/Images/type3.jpg");
        uri = new Uri("pack://application:,,,/ExamplePlugin;component/Images/unknown.png");
    var marker = new CIMPictureMarker()
        Size = 16, // for this use case, I want to use 16. Otherwise the icons look too large in the legend...
        URL = BuildPictureMarkerURL(uri)
    return marker;
private static string BuildPictureMarkerURL(Uri uri)
    var decoder = BitmapDecoder.Create(uri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
    var builder = new StringBuilder();
    if (decoder.CodecInfo.MimeTypes.ToLower().Contains("jpeg"))
    return builder.ToString();
private static string ToBase64(this BitmapDecoder decoder)
    var encoder = BitmapEncoder.Create(decoder.CodecInfo.ContainerFormat);
    foreach (var frame in decoder.Frames)
    using (var stream = new System.IO.MemoryStream())
        return Convert.ToBase64String(stream.ToArray(), Base64FormattingOptions.InsertLineBreaks);
0 Kudos