Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to add an image on the Window throws 'BitmapImage must have IsFrozen set to false to modify.'

Tags:

wpf

xaml

App.xaml

<Application ...
         StartupUri="Views\MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary>
                <Local:ViewModelLocator x:Key="ViewModelLocator" />
                <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
                <BitmapImage x:Key="Logo" UriSource="Media/Images/Logo.png" />
            </ResourceDictionary>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

MainWindow.xaml

<Image Source="{StaticResource Logo}" Style="{StaticResource LogoStyle}" />

Throws

Initialization of 'System.Windows.Media.Imaging.BitmapImage' threw an exception.'

Inner:

Specified value of type 'System.Windows.Media.Imaging.BitmapImage' must have IsFrozen set to false to modify.

and the debugger is pointing at UriSource.

Stack

 at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at XXX.Apps.UI.Wpf.App.InitializeComponent() in C:\Projects\XXX\Apps\UI\WPF\src\XXX.Apps.UI.Wpf\App.xaml:line 1
   at XXX.Apps.UI.Wpf.App.Main()
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Inner stack

   at System.Windows.Freezable.WritePreamble()
   at System.Windows.Media.Imaging.BitmapImage.EndInit()
   at MS.Internal.Xaml.Runtime.ClrObjectRuntime.InitializationGuard(XamlType xamlType, Object obj, Boolean begin)

Why? How do I fix it? I am on .NET 4.5

Edit:

Apparently I don't even need to add the Image to the MainWindow to reproduce the issue. It has something to do with my declaration in App.xaml ...

Edit 2/Fix:

As a temporary work-around I have moved the Logo resource from App.xaml into the Window resources and it works fine. Would be great if I could use it from the App.xaml, though.

like image 535
hyankov Avatar asked Jan 04 '17 17:01

hyankov


1 Answers

This question is 3 Years old now, but still has no answer. So the heck with it.

If you still want to use the Image from the App.xaml, but can´t because you have merged Resource Dictionaries in your App.xaml and get the "IsFrozen" error, just put your Image Resource in your own resource dictionary and merge it in your App.xaml.

For this example create a Resource Dictionary (I called this one ImageDictionary.xaml).

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:tradeware.Resources">
    <BitmapImage x:Key="Logo" UriSource="Media/Images/Logo.png"/>
</ResourceDictionary>

Then merge it in your App.xaml.

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ImageDictionary.xaml"/>
            .
            .
            .
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
like image 81
RadiatorTwo Avatar answered Nov 15 '22 20:11

RadiatorTwo