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;
}
精彩评论