How to set images for GalleryItems in Pro SDK

385
5
Jump to solution
04-30-2019 10:06 AM
TimWhiteaker
Occasional Contributor II

I have my own images I created for use as a gallery item icons within my gallery.  In the Initialize() method of my Gallery, I want to do this in C#:

// I already added my image to Properties.Resources
var img = new Bitmap(Properties.Resources.my_png_image);
Add(new GalleryItem("Item Text", img));

However, when I click the Gallery in the ribbon, I see the text but no image.

What's the proper way to include an image in your project and use it as the icon for a gallery item?  I don't have to use Properties.Resources; that's just one thing I tried.

I also tried using images as embedded resources, using an image I added to my Thumbnails folder with Build Action set to Resource, but the resource stream (the stm variable) came back null.

I also tried using images as resources, but I got a "Could not load file or assembly" error on this line:

BitmapImage fromResource = new BitmapImage(new Uri("pack://application:,,,/Module1;component/Thumbnails/my_png_image.png", UriKind.Absolute));
0 Kudos
1 Solution

Accepted Solutions
UmaHarano
Esri Regular Contributor

Hi Tim

Gallery items require the image url in the URI format.  There is a sample in the community samples repo that show this:

Gallery sample

Thanks

Uma

View solution in original post

5 Replies
TimWhiteaker
Occasional Contributor II

I also tried hard-coding an image path on disk, but the image is still missing when I click the gallery drop down in the ribbon.

var img = Image.FromFile(@"C:\my_images\my_png_image.png");
Add(new GalleryItem("Item Text", img));

Maybe I'm missing something about the requirements for images to be used for a gallery item?  That img is definitely not null when I pass it to the GalleryItem constructor.

0 Kudos
UmaHarano
Esri Regular Contributor

Hi Tim

Gallery items require the image url in the URI format.  There is a sample in the community samples repo that show this:

Gallery sample

Thanks

Uma

TimWhiteaker
Occasional Contributor II

Using a Uri helped, and it also appears that the pack URI needed to use my default namespace rather than the module id.  Here's my updated snippet which works, using a correct Pack URI and a Uri object.  Thanks Uma Harano‌!

Add(new GalleryItem("Item Text", new Uri("pack://application:,,,/MyDefaultNamespace;component/Thumbnails/my_png_image.png")));
TimWhiteaker
Occasional Contributor II

Using a string instead of a uri also works for me.

Add(new GalleryItem("Item Text", "pack://application:,,,/MyDefaultNamespace;component/Thumbnails/my_png_image.png"));
0 Kudos
DavidWilton
Occasional Contributor

Also worth noting that the image needs to be set to "Resource" not "addin content". 

0 Kudos