关于c#:在WPF中使用图像和图标

Using Images and Icons in WPF

我试图在WPF应用程序中使用图标,并在某些图像中使用其他图像,但我不断从Designer视图中收到错误消息,并指出诸如" path-x不是有效资源或找不到资源"之类的内容。 -其中" path-x"是我尝试使用的任何图像的路径。

如果它在正确的位置寻找,我敢打赌会找到它的;)

但是,它决定不再给我这个错误。 因此,我继续单击"运行"(F5),以在标题栏中看到我的新图标。 只是要面对这种野兽:

"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: "

Exception at runtime while trying to use an icon for my titlebar!

为什么这件事给我态度? 我只是想插入图片...

更新

这是当我尝试使用"属性"窗格添加图标时为图标生成的XAML:

1
2
3
4
5
<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">

最新更新

enter image description here

我什至无法使用属性窗格添加图标,以下是它给我的错误。


确保目标是资源。然后执行以下操作:

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

MobilWPF是您的名称空间,还是您的项目名称?我忘记了我的项目名称和名称空间是相同的。


我通过为图像设置以下属性来解决了同一问题:

1
2
Build Action = Content
Copy to Output Directory = Copy always


我发现当您添加新资源然后重建项目时,VS2008有时会引起类似的问题。我发现如果您清理构建,然后从头开始重建,问题就会消失。对我来说似乎是VS2008错误。


将资源Properties中的BuildAction设置为Resource可以解决此问题。

招呼


我回想起类似的情况,我的应用程序崩溃了,我最终将其跟踪到一个.ico错误。

如果您使用的是图标和Windows XP,则XP不支持256x256图标。您可能需要在编辑器(例如IcoFx)中打开图标,然后删除256x256图标。


答案似乎取决于图标的存储位置/方式。我正在尝试做与@iterationx答案类似的操作,但是它对我没有用,这就是我的总结。我正在使用VS 2010。

我通过项目的属性页在资源,添加资源(下拉),添加现有文件下将图标添加为资源。然后,我只在Icon属性中使用了文件名:

1
Icon="my_icon.ico"


如果您通读了此异常,则会说出相同的话:

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

这可能是因为Images/favicon.ico图标图像未正确设置。确保将其"构建操作"设置为"资源",并且在项目中的"图像"文件夹下。


使用Icon ="。\ Images \ test.ico"这使我工作


我遇到了同样的问题,发现如果您的项目名称中有一个空格,它将无法正确读取图像文件。希望有帮助。


抱歉,我不能再提供更多帮助。刚开始阅读您的问题时,我认为这只是类型不匹配,就像您在要作为图像的字符串时指定的位置。这个问题似乎是具有多种可能解决方案的问题。除了列出所有内容之外,我仅将您链接到上面有它们的Microsoft Connect页面。