How do I use standard Windows warning/error icons

2020-02-02 13:09发布

I'm making a custom error dialog in my WPF app and I want to use a standard windows error icon. Can I get the OS-specific icon from WPF? If not, does anyone know where to get .pngs with transparency of them? Or know where in Windows to go extract them from?

So far my searches have turned up nothing.

标签: c# wpf icons
10条回答
看我几分像从前
2楼-- · 2020-02-02 13:41

Use this:

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());
        }
    }
}

Don't forget to add System.Drawing reference to your project from global location.

Then use this in your 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>
查看更多
Evening l夕情丶
3楼-- · 2020-02-02 13:42

In Visual Studio, use File + Open + File and select c:\windows\system32\user32.dll. Open the Icon node and double-click 103. On my machine that's the Error icon. Back, right-click it and select Export to save it to a file.

That's the iffy way. These icons are also available in Visual Studio. From your Visual Studio install directory, navigate to Common7\VS2008ImageLibrary\xxxx\VS2008ImageLibrary.zip + VS2008ImageLibrary\Annotations&Buttons\ico_format\WinVista\error.ico. The redist.txt file in the Visual Studio install directly explicitly gives you the right to use this icon in your own application.

查看更多
劫难
4楼-- · 2020-02-02 13:47

About using Standard Microsoft Icons.

The vast majority of developers out there don't know that Visual Studio comes with an Image Library. So here goes two links that highlight it:

About using Microsoft Visual Studio 2010 Image Library.

About using Microsoft Visual Studio 2008 Image Library.

查看更多
Rolldiameter
5楼-- · 2020-02-02 13:49

use SystemIcons and convert them to an ImageSource like so:

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;
}
查看更多
爱情/是我丢掉的垃圾
6楼-- · 2020-02-02 13:56

This is how I used a System icon in XAML:

xmlns:draw="clr-namespace:System.Drawing;assembly=System.Drawing"
...
<Image Source="{Binding Source={x:Static draw:SystemIcons.Warning},
        Converter={StaticResource IconToImageSourceConverter},
        Mode=OneWay}" />

I used this converter to turn an Icon to 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();
    }
}
查看更多
做个烂人
7楼-- · 2020-02-02 13:58

There is a SystemIcons class, but it need adjustment for WPF needs (i.e. converting Icon to ImageSource).

查看更多
登录 后发表回答