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
  • 64,142
  • 17
  • 138
  • 173

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
  • 64,142
  • 17
  • 138
  • 173
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.
  • 143,945
  • 62
  • 324
  • 540
  • 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