开发者

BitmapImage filenotfound exception (PresentationCore.resources) on construction

开发者 https://www.devze.com 2023-04-04 14:15 出处:网络
I have a problem with a BitmapImage in wpf. When i create it it gives a filenotfound exception, which says it\'s missing the PresentationCore.resources assembly. But i\'ve added it to my references li

I have a problem with a BitmapImage in wpf. When i create it it gives a filenotfound exception, which says it's missing the PresentationCore.resources assembly. But i've added it to my references list and it still throws the same exception

Uri filename = new Uri(@"D:\barcode_zwart_wit.jpg", UriKind.Absolute);
BitmapImage image =开发者_运维问答 new BitmapImage(filename); //<-- FileNotFound Exception
  • The image uri is correct.

  • The image isn't opened somewhere else
  • I've checked the version of the PresentationCore.resources, no problem there.

  • The lines are in the setter of a property, which is first launched in the constructor of a usercontrol.

  • Visual Studio 2010, .net v4

Does anyone have any idea what's the problem? Does PresentationCore.resources have any dependencies i don't know about?


The problem might be with the image's color profile being unavailable. Use BitmapImage.CreateOptions property to ignore color profile information as below:

var bmp = new BitmapImage();
bmp.BeginInit();
bmp.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
bmp.UriSource = uri;
bmp.EndInit();
// Use the bmp variable for further processing and dispose it

I had the same issue with a few of .jpg images and this resolved it.


I've solved it by using AppDomain.CurrentDomain.AssemblyResolve. it's an event which is called if a certain assembly can't be found.

in constructor:

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

CurrentDomain_AssemblyResolve:

Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    string missingAssemblyName = args.Name.Remove(args.Name.IndexOf(',');
    if ( missingAssemblyName == "PresentationCore.resources" )
    {
         string s = "C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\PresentationCore.resources.dll";
         return Assembly.LoadFile(s);
    }
    return;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号