1

I need to assign an Icon to a WPF window, but cannot seem to do this from a resource file. I tried this solution, but end up with:

ImageSource for Icon property must be an icon file

But, if I try to leave it as Icon, then I get an error that I need to convert to ImageSource...

Icon = Properties.Resources.myIcon.ToImageSource();
Community
  • 1
  • 1
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180

2 Answers2

1

I ended up doing the following:

using (var iconStream = new MemoryStream())
{
    icon.Save(iconStream);
    iconStream.Seek(0, SeekOrigin.Begin);
    return BitmapFrame.Create(iconStream);
}

which was actually one of the unaccepted answers from the same question I already referenced: just a different response

Community
  • 1
  • 1
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
0

The other SO solution you reference is creating an icon from an IntPtr and so does not apply to your situation. Instead try the following:

var icon = BitmapFrame.Create(Application.GetResourceStream(
              new Uri("MyAppIcon.ico", UriKind.RelativeOrAbsolute)).Stream);

http://welearndotnet.blogspot.com/2011_11_21_archive.html

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • I am reading from a .resx file, though. I would prefer the strong typing that I gain from that. Or, do I have to reference the file location? – Justin Pihony Jul 16 '12 at 16:34