Problem is in IconToStream
which creates MemoryStream
, copies icon into it, returns reference and then disposes all resources allocated by MemoryStream
which effectively makes your stream and therefore Frame
empty. If you would change GetSizedSource
to something like below that returns BitmapFrame
before desposing MemoryStream
is should work:
private BitmapFrame GetSizedSource(Icon icon, int size)
{
using (var stream = new MemoryStream())
{
icon.Save(stream);
stream.Position = 0;
return BitmapDecoder.Create(stream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.OnDemand)
.Frames
.SingleOrDefault(_ => Math.Abs(_.Width - size) < double.Epsilon);
}
}