Using Images and Icons in WPF

眉间皱痕 提交于 2019-12-01 14:28:14

问题


I am trying to use an Icon in my WPF application, and some images for other things, but I keep getting errors from the Designer View saying stuff like "path-x is not a valid resource or cannot be found." - where "path-x" is the path of whatever image I am trying to use.

If it were looking in the right place, I bet it'd find it ;)

BUT, then, it decided to not give me that error anymore. So, I went ahead and clicked Run (F5), to see my new Icon in the title bar. Only to be confronted with this beast:

"System.Windows.Markup.XamlParseException occurred Message='Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '5' and line position '50'.
Source=PresentationFramework
LineNumber=5 LinePosition=50
StackTrace: at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri) 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 One_Stop_Management.MainWindow.InitializeComponent() in c:\Users\Jason\Documents\Visual Studio 2010\Projects\One Stop Management\One Stop Management\MainWindow.xaml:line 1 at One_Stop_Management.MainWindow..ctor() in C:\Users\Jason\Documents\Visual Studio 2010\Projects\One Stop Management\One Stop Management\MainWindow.xaml.cs:line 25 InnerException: System.IO.IOException Message=Cannot locate resource 'images/favicon.ico'. Source=PresentationFramework StackTrace: at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access) at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access) at System.IO.Packaging.PackWebResponse.CachedResponse.GetResponseStream() at System.IO.Packaging.PackWebResponse.GetResponseStream() at System.IO.Packaging.PackWebResponse.get_ContentType() at System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream stream, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle& safeFilehandle) at System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache) at System.Windows.Media.Imaging.BitmapFrame.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy) at System.Windows.Media.ImageSourceConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) at System.Windows.Baml2006.TypeConverterMarkupExtension.ProvideValue(IServiceProvider serviceProvider) at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(MarkupExtension me, IServiceProvider serviceProvider) InnerException: "

Why is this thing giving me attitude? I'm just trying to insert an image...

Updates

Here is the XAML that was produced for the icon when I tried to add the Icon using the Properties Pane:

<Fluent:RibbonWindow x:Class="One_Stop_Management.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Fluent="clr-namespace:Fluent;assembly=Fluent"
        Title="One Stop Management" Height="727" Width="1208" Icon="Resources\favicon.ico">

Latest Update

I can't even add an icon using the properties pane, below is the error it gives me.


回答1:


I'm sorry I can't be more helpful. On reading your question at first I thought it'd be just a type mismatch, like where you were specifying a string when it was meant to be an image. The issue seems to be one that has multiple possible resolutions. Instead of listing them all I'll just link you to the Microsoft Connect page that has them on it.




回答2:


Make sure the target is a resource. Then do something like this:

Icon="/MobilWPF;component/Resources/Images/MobileIcon.ico"

where MobilWPF is your namespace, or maybe you're project name? I forget my project name and my namespace are the same.




回答3:


I've discovered that VS2008 sometimes causes problems like this when you add new resources and then rebuild the project. I've found if you clean the build and then rebuild from scratch the problem disappears. Looks like a VS2008 bug to me.




回答4:


I fixed this same issue by setting these properties for the image:

Build Action = Content
Copy to Output Directory = Copy always



回答5:


The exception, if you read through it, says the same thing:

InnerException: System.IO.IOException Message=Cannot locate resource 'images/favicon.ico'.

This is probably because the Images/favicon.ico icon image is not setup correctly. Make sure that it's Build Action is set to "Resource", and that it's in the project under the "Images" folder.




回答6:


The answer seems to depend on where/how you store the icon. I was trying to do something similar to @iterationx's answer but it didn't work for me, and this is what I finished up with. I'm using VS 2010.

I added the icon as a resource via the project's properties page, under Resources, Add Resource (dropdown), Add existing file. Then I just used the file name in the Icon property:

Icon="my_icon.ico"



回答7:


I recall a similar situation where my application was crashing, and I eventually tracked it down to an .ico error.

If you're using icons and Windows XP, XP does not support the 256x256 icons. You may need to open the icon in an editor, such as IcoFx, and delete the 256x256 icon.




回答8:


Setting BuildAction to Resource in resource Properties solved this problem.

Greets




回答9:


I had this same problem and found that if your project has a space in the name, it won't read the image files correctly. Hope that helps.




回答10:


use Icon=".\Images\test.ico" this got me working



来源:https://stackoverflow.com/questions/5601806/using-images-and-icons-in-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!