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));
Solved! Go to Solution.
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.
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")));
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"));