0

Different versions of this question are already asked, yet solutions to those questions don't feet my requirements.

Assume We have a resource file named MyResource.resx and an image file Accept.png in a project folder called Icons and a refrence to this image is in MyResource.resx.

I'm looking for a way to set source of an Image UIElement with following requirements:

  1. Refactor name of image and ...
  2. Keep it's transparency
  3. And become aware when I accidentally remove the image file from it's location (compile errors should happen).

Here are what I already tested.

1:In Code behind:

myImage.Source = new BitmapImage(new Uri(@"\Icons\Accept.png", UriKind.Relative));

The problem with this code is: No refactoring is possible, and even worst, errors related to wrong uri are only in run-time.

2: Again in code behind:

myImage.Source = MyResource.Accept.ToBitmapImage()

where .ToBitmapImage() is a method I've found in internet:

public static BitmapImage ToBitmapImage(this System.Drawing.Bitmap bitmap)
        {
            using (MemoryStream memory = new MemoryStream())
            {
                bitmap.Save(memory, ImageFormat.Bmp);
                memory.Position = 0;
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memory;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
                return bitmapImage;
            }
        }

The Only problem with this code is that image Transparency is Lost.

In Xaml:

<Image x:Name="myImage" Source="/Icons/Accept.png" />

The problem with this code is: No refactoring is possible, and errors related to wrong uri are not shown. they are not even noticed in runtime.

I tried to use the resource file right in the xaml, following this tutorial but i've failed.

So how to do this.

eulerleibniz
  • 271
  • 3
  • 11
  • 2
    Transparency is obviously lost due to the image format you're using for encoding and decoding. Replacing `ImageFormat.Bmp` by `ImageFormat.Png` should solve that problem. Or use one of the other conversion methods mentioned in the answers to [this question](http://stackoverflow.com/q/94456/1136211). Besides that, you typically don't use `.resx` files in WPF applications anyway. – Clemens Feb 05 '17 at 09:57
  • @Clemens Thanks for the comment. Replacing ImageFormat.Bmp by ImageFormat.Png worked fine. But I was also hoping for a way to do this in XAML so I could see the resulting UI. And why you typically don't use .resx files in WPF applications? I'm new to WPF anyway. – eulerleibniz Feb 05 '17 at 13:26
  • 1
    [This answer](http://stackoverflow.com/a/15008178/1136211) may be helpful. – Clemens Feb 05 '17 at 14:01
  • @Clemens Thanks again for another helpful comment. And I've decided to use uri and instead of a simple string, I would use: var uri = new Uri("pack://application:,,,/Resources/" + nameof(MyResource.Red)); So I can refactor whenever I want to. – eulerleibniz Feb 05 '17 at 14:10

0 Answers0