PictureMarkerSymbol : unable to set source from byte array

4207
7
Jump to solution
11-20-2014 08:29 AM
LaurentSIBILLE
New Contributor

Hi,

We are trying to initialize a PictureMarkerSymbol from an image byte array and it doesn't work.

Any help will be very appreciate.

Thanks,

0 Kudos
1 Solution

Accepted Solutions
GregDeStigter
Esri Contributor

You can initialize a `PictureMarkerSymbol` from a byte array of image data like this:

var pms = new PictureMarkerSymbol();
var data = File.ReadAllBytes(@"d:\images\redbeacon.png");
using (var ms = new MemoryStream(data))
{
     await pms.SetSourceAsync(ms);
}

Does this help?  Can you give a little more information about what fails for you - or share some code?

View solution in original post

7 Replies
GregDeStigter
Esri Contributor

You can initialize a `PictureMarkerSymbol` from a byte array of image data like this:

var pms = new PictureMarkerSymbol();
var data = File.ReadAllBytes(@"d:\images\redbeacon.png");
using (var ms = new MemoryStream(data))
{
     await pms.SetSourceAsync(ms);
}

Does this help?  Can you give a little more information about what fails for you - or share some code?

LaurentSIBILLE
New Contributor

Thanks Greg for your help.

It works this way.

0 Kudos
dotMorten_esri
Esri Notable Contributor

FYI you can also do it with the file path:

await pms.SetSourceAsync(new Uri("file://d/images/redbeacon.png"); 

The benefit of that, is that the byte data is not loaded until it actually needs to be rendered (which may be never) and will be done off-thread.

LaurentSIBILLE
New Contributor

Hi Morten,

Thanks for this usefull information.

It will be great if it works for embedded resource images too.

I mean using an uri like following:

pack://application:,,,/[AssemblyName];Component/[EmbeddedResourImagePath]

0 Kudos
MichaelBranscomb
Esri Frequent Contributor

Hi,

You can do something like:

PictureMarkerSymbol pictureMarkerSymbol = new PictureMarkerSymbol();
await pictureMarkerSymbol.SetSourceAsync(new Uri("pack://application:,,,/Images/BlueStickPin.png", UriKind.Absolute));


SimpleRenderer simpleRenderer = new SimpleRenderer() 
{
  Symbol = pictureMarkerSymbol,
};


GraphicsOverlay graphicsOverlay = new GraphicsOverlay()
{
  Renderer = simpleRenderer,
};


Graphic graphic = new Graphic() 
{
  Geometry = new MapPoint(0,0,SpatialReference.Create(3857)),
};
graphicsOverlay.Graphics.Add(graphic);

MyMapView.GraphicsOverlays.Add(graphicsOverlay);

In this case the png file properties are Resource / Do not copy.

Cheers

Mike

0 Kudos
dotMorten_esri
Esri Notable Contributor

pack uris are indeed also supported. Is this not working for you?

0 Kudos
LaurentSIBILLE
New Contributor

I gave it a try and it works.

Maybe additional comment on SetSourceAsync method should help.

0 Kudos