简体   繁体   中英

SplashScreen IOException

SplashScreen s = new SplashScreen(System.Reflection.Assembly.GetExecutingAssembly(),"splash.png");
s.Show(false);

when invoking s.Show() whether the parameter is false or true , it throws an IOException with the message

Cannot locate resource 'splash.png'

even though that splash.png is added to resources and its Build Action is Resource .

I have noticed many problems when using images in WPF, also with Image control.

According to the principle "My Code doesn't work and I don't know why. My code Works and I don't know why", I have solved the problem.

First I have made the Build Action of the splash image as Resource . (Putting the image as Embedded Resource didn't solved the problem).

Second, My code is :

SplashScreen s = new SplashScreen("resources/splash.png");
s.Show(false);
/* do some things */
s.Close(Timespan.FromMilliseconds(300));

even though that splash.png is added to resources and its Build Action is Resource.

Wrong build action, make it Embedded Resource . If you still have trouble then use ildasm.exe to look at the assembly manifest for the .mresource directive to ensure that the resource got properly embedded with the expected name.

Or use a file, following these how-to steps . Generally the more sane approach since it doesn't make that much sense to have a large resource occupy address space for the life of the program when it is only needed for a short amount of time at the start of the program.

try with explicitly specifying the assembly name, even though the caller was in the same assembly as the resource.

Here's how it looks with a Pack URI syntax:

pack://application:,,,/MyAssemblyName;component/MyResourcesFolder/MyImage.png

http://msdn.microsoft.com/en-us/library/aa970069.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM