wpf 从 c# 中的图标文件创建 System.Windows.Media.ImageSource?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23817724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a System.Windows.Media.ImageSource From an Icon File in c#?
提问by cindywmiao
I am programming in a WPF application in c#. I need to change the notifier icon sometimes;
我正在 C# 中的 WPF 应用程序中编程。有时我需要更改通知程序图标;
I implemented the icon like this:
我实现了这样的图标:
<tn:NotifyIcon x:Name="MyNotifyIcon"
Text="Title"
Icon="Resources/logo/Error.ico"/>
My solution is changing the Icon, the type of MyNotifyIcon.Iconis ImageSource, and I want to get by an icon file. I could find the way to do that.
我的解决方案是更改图标,MyNotifyIcon.Icon的类型是 ImageSource,我想通过图标文件获取。我能找到办法做到这一点。
Do somebody have some ideas how? Or have the any other solution?
有人有一些想法吗?或者有其他解决方案吗?
Shortly, I have an address like /Resource/logo.icon, and I wanna get a System.Windows.Media.ImageSource
很快,我有一个像/Resource/logo.icon这样的地址,我想要一个System.Windows.Media.ImageSource
回答by George Chondrompilas
You can use the BitmapImageclass:
您可以使用BitmapImage该类:
// Create the source
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri("./Resource/logo/logo.icon");
img.EndInit();
BitmapImageclass inherits from ImageSourceclass which means you can pass the BitmapImageobject to NotifyIcon.Iconas:
BitmapImage类继承自ImageSource类,这意味着您可以将BitmapImage对象传递给NotifyIcon.Icon:
NI.Icon = img;
回答by denver
Are you using the NotifyIcon from the Hardcodet.Wpf.TaskbarNotification namespace created by Philipp Sumi? If so you have the option to either specify the icon as an ImageSource or an Icon.
您是否使用 Philipp Sumi 创建的 Hardcodet.Wpf.TaskbarNotification 命名空间中的 NotifyIcon?如果是这样,您可以选择将图标指定为 ImageSource 或 Icon。
TaskbarIcon notifyIcon = new TaskbarIcon();
// set using Icon
notifyIcon.Icon = some System.Drawing.Icon;
// set using ImageSource
notifyIcon.IconSource = some System.Windows.Media.ImageSource;
Note that internally setting IconSource sets Icon.
请注意,内部设置 IconSource 设置图标。
To set from a resource.
从资源设置。
notifyIcon.Icon = MyNamespace.Properties.Resources.SomeIcon
回答by DrkDeveloper
have you thought in do it with resources?
你有没有想过用资源来做?
To get from resources:
从资源中获取:
MainNameSpace.Properties.Resources.NameOfIconFileInResources;
Put the icons in resources, if you have the image file (not icon) i have a method to change it:
将图标放在资源中,如果您有图像文件(不是图标),我有一种方法可以更改它:
public static Icon toIcon(Bitmap b)
{
Bitmap cb = (Bitmap) b.Clone();
cb.MakeTransparent(Color.White);
System.IntPtr p = cb.GetHicon();
Icon ico = Icon.FromHandle(p);
return ico;
}
And programmatically change it with the known attribute .Icon; Don't worry about ImageSource type.
并使用已知属性 .Icon 以编程方式更改它;不用担心 ImageSource 类型。
Extracting image from icon (.ico) file:
从图标 (.ico) 文件中提取图像:
Stream iconStream = new FileStream (MainNameSpace.Properties.Resources.NameOfIconFileINResources, FileMode.Open );
IconBitmapDecoder decoder = new IconBitmapDecoder (
iconStream,
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.None );
// loop through images inside the file
foreach ( var item in decoder.Frames )
{
//Do whatever you want to do with the single images inside the file
this.panel.Children.Add ( new Image () { Source = item } );
}
// or just get exactly the 4th image:
var frame = decoder.Frames[3];
// save file as PNG
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(frame);
using ( Stream saveStream = new FileStream ( @"C:\target.png", FileMode.Create ))
{
encoder.Save( saveStream );
}
But you must put the .ico file in resources, or take it with relative path...
但是你必须把 .ico 文件放在资源中,或者用相对路径来取......
Taken from How can you access icons from a Multi-Icon (.ico) file using index in C#

