Select to view content in your preferred language

Cannot create a Graphic with PictureMarkerSymbol derived from a BitmapImage

2312
3
09-29-2010 01:41 PM
patblair
Emerging Contributor
I am attempting to create a Graphic that has as its symbol a PictureMarkerSymbol whose image source is derived from an image found at a given URI.  I can successfully retrieve the image, and use it for other purposes, but when I make it the source of my PictureMarkerSymbol, set that symbol on a Graphic object, and add that Graphic object to the graphics layer... I see nothing.

I have included a snippet of code that illustrates what I'm doing.  This seems as though it should be a pretty standard operation, and I am wondering if I am missing something minor, or something critical.

public void DrawAGraphic()
{
    // The URI to the image (a PNG) is valid.
    Uri imgUri = new Uri("http://localhost:8099/Objects.png", UriKind.Absolute);
    BitmapImage bitmap = new BitmapImage();
    // (I've tried each of the CreateOptions, but have been going with this one.)
    bitmap.CreateOptions = BitmapCreateOptions.None;
    // We'll try to place the graphic when the image loads.
    bitmap.ImageOpened += new EventHandler<RoutedEventArgs>(bitmap_ImageOpened);
    bitmap.UriSource = imgUri;
}

static void bitmap_ImageOpened(object sender, RoutedEventArgs e)
{
    // This handler is called, so I believe I can safely say that
    // we have the image in-hand.  It's PixelWidth and PixelHeight
    // properties are appropriate.

    // The sender is the original bitmap we were trying to open.
    // (At this point, the bitmap appears to be valid for any 
    // purpose other than using it with a PictureMarkerSymbol.)
    BitmapImage bitmap = sender as BitmapImage;

    // We create the picture marker symbol.
    PictureMarkerSymbol pict = new PictureMarkerSymbol();
    pict.Source = bitmap;
    pict.OffsetX = 0;
    pict.OffsetY = 0;

    // Now we create the graphic.
    Graphic graphic = new Graphic();
    // The geometry is well within the map extent, and in the same
    // spatial reference.
    graphic.Geometry = new MapPoint(
        -101.5, 47.5, new SpatialReference(4326));
    graphic.Symbol = pict;
    

    // We have access to the map's GraphicsLayer, so we add the graphic.
    graphicsLayer.Graphics.Add(graphic);
}

I am using ArcGIS Server 10, Silverlight 4, C#, and Visual Studio 2010.  I've been hunting around the forums and the Internet for information that might point me towards a solution, but haven't found anything so far.  I would dearly appreciate any advice.
0 Kudos
3 Replies
dotMorten_esri
Esri Notable Contributor
Is you ImageOpened event handler hit? AFAIK in Silverlight the image is not downloaded until the image goes onto the screen (and in this case this doesn't happen until the end of your eventhandler).
0 Kudos
patblair
Emerging Contributor
Thank you for your reply.  The ImageOpened event handler is being hit (I verify that with a breakpoint).  When I created the BitmapImage by passing the URI to the constructor, it was not, but using the code as I've posted it here, it is.  I have performed tests wherein I use the loaded BitmapImage in a regular Image control and that seems to work as you would expect.  (That is to say, you see the image in the control.)

I have witnessed in my experimentation that if I create the BitmapImage (passing the URI to the contructor), then use it as the ImageSource for a standard .Net control (like the Image control), the ImageOpened event doesn't fire if I don't actually put that .Net control somewhere into the layout so it can be drawn.  So I think I can generally verify what you're saying about Silverlight not downloading the image until it's time to show it in some cases.  But, as I say, in this case I think I do have a valid BitmapImage by the time I make it the image source of my PictureMarkerSymbol.  That has led me to think that I am just missing a step somewhere.

Essentially, what I am trying to do is create a graphic whose picture is retrieved from a URL (rather than being defined by a resource in my XAP).  It seems as though the process I'm using should allow me to do that, but I am wondering:  Can I actually do this with the API?  If so, is there perhaps a code sample somewhere that demonstrates how I should be going about it?
0 Kudos
patblair
Emerging Contributor
I have a resolution.  It turns out that the problem wasn't with the code I posted, but rather with the way I had the map configured (d'oh!).  The map in which the graphics didn't appear had the graphics layer defined in XAML, and the base map layer defined in code in such a way that the graphics layer was beneath the base map.  After I rectified that, the graphics started appearing.

I apologize for what was probably a confusing question.  For what it's worth, the code I posted does apparently seem to be a reasonable example for creating the image from a URL.

Once again, many thanks.
0 Kudos