我正在为我的WPF应用程序制作自定义错误对话框,我想使用标准的Windows错误图标。我能从WPF中获取特定于操作系统的图标吗?如果不能,有人知道在哪里获取带有透明度的.png文件吗?或者知道从Windows中提取它们的位置吗?
到目前为止,我的搜索还没有发现什么。
我正在为我的WPF应用程序制作自定义错误对话框,我想使用标准的Windows错误图标。我能从WPF中获取特定于操作系统的图标吗?如果不能,有人知道在哪里获取带有透明度的.png文件吗?或者知道从Windows中提取它们的位置吗?
到目前为止,我的搜索还没有发现什么。
有一个SystemIcons类,但它需要进行调整以满足WPF的需求(即将Icon
转换为ImageSource
)。
绝大部分开发者并不知道Visual Studio自带一个图像库。以下是两个链接介绍它:
有关使用Microsoft Visual Studio 2008 图标库的内容。
这是我在XAML中使用系统图标的方法:
xmlns:draw="clr-namespace:System.Drawing;assembly=System.Drawing"
...
<Image Source="{Binding Source={x:Static draw:SystemIcons.Warning},
Converter={StaticResource IconToImageSourceConverter},
Mode=OneWay}" />
我使用了这个转换器将一个图标转换为ImageSource:
public class IconToImageSourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var icon = value as Icon;
if (icon == null)
{
Trace.TraceWarning("Attempted to convert {0} instead of Icon object in IconToImageSourceConverter", value);
return null;
}
ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon(
icon.Handle,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
return imageSource;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
converter
需要在FrameworkElement.Resource
元素(即Application、Window、UserControl)中定义。请参见SO答案:https://dev59.com/cJLea4cB1Zd3GeqPzDPD#34351777。 - Matt M如果默认大小足够,请使用.NET的SystemIcons类进行前三个步骤,参见modosansreves答案
所以它可以很简单:
Imaging.CreateBitmapSourceFromHIcon(SystemIcons.Error.Handle)
using System;
using System.Drawing;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Markup;
using System.Windows.Media.Imaging;
using Extensions
{
[ContentProperty("Icon")]
public class ImageSourceFromIconExtension : MarkupExtension
{
public Icon Icon { get; set; }
public ImageSourceFromIconExtension()
{
}
public ImageSourceFromIconExtension(Icon icon)
{
Icon = icon;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return Imaging.CreateBitmapSourceFromHIcon(Icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
}
}
不要忘记从全局位置添加System.Drawing引用到您的项目中。
然后在您的xaml中使用以下代码:
<WindowOrSomething x:Class="BlaBlaWindow"
xmlns:draw="clr-namespace:System.Drawing;assembly=System.Drawing"
xmlns:ext="clr-namespace:Extensions">
<Image Source="{ext:ImageSourceFromIcon {x:Static draw:SystemIcons.Error}}"/>
</WindowOrSomething>
您是否可以简单地使用Windows API?
Delphi示例:
procedure TForm1.FormClick(Sender: TObject);
var
errIcon: HICON;
begin
errIcon := LoadIcon(0, IDI_ERROR);
DrawIcon(Canvas.Handle, 10, 10, errIcon)
end;
使用SystemIcons
并将它们转换为ImageSource
,如下所示:
try
{
var temp = SystemIcons.WinLogo.ToBitmap(); //Your icon here
BitmapImage bitmapImage = new BitmapImage();
using (MemoryStream memory = new MemoryStream())
{
temp.Save(memory, ImageFormat.Png);
memory.Position = 0;
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
}
this.Icon = bitmapImage;
}
catch (Exception ex)
{
this.Icon = null;
}
将 SystemIcons.Error 等转换为 .png 格式(保持透明度),然后将输出添加到资源中,并像往常一样在 WPF Image 控件中使用。
System.Drawing.SystemIcons.Error.ToBitmap().Save("Error.png", System.Drawing.Imaging.ImageFormat.Png);
Graphics g = this.CreateGraphics();
g.DrawIcon(SystemIcons.Question, 40, 40);
Graphics
。你所写的是针对 GDI 的。 - bohdan_trotsenko