How do I use standard Windows warning/error icons in my WPF app?

C#WpfIcons

C# Problem Overview


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# Solutions


Solution 1 - C#

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

Solution 2 - C#

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 2008 Image Library.

Solution 3 - C#

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

Solution 4 - C#

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.

Solution 5 - C#

You can use .NET's SystemIcons class for roughly the first three steps if the default size is ok, see modosansreves answer

So it could be as simple as:

 Imaging.CreateBitmapSourceFromHIcon(SystemIcons.Error.Handle)

Solution 6 - C#

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>

Solution 7 - C#

Can't you simply use Windows API?

Delphi Example:

procedure TForm1.FormClick(Sender: TObject);
var
  errIcon: HICON;
begin
  errIcon := LoadIcon(0, IDI_ERROR);
  DrawIcon(Canvas.Handle, 10, 10, errIcon)
end;

Solution 8 - C#

You can draw it like this:

Graphics g = this.CreateGraphics();
g.DrawIcon(SystemIcons.Question, 40, 40);

Solution 9 - C#

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

Solution 10 - C#

Convert SystemIcons.Error etc to .png (maintains transparency) then add output to resources and use as usual in WPF Image control.

    System.Drawing.SystemIcons.Error.ToBitmap().Save("Error.png", System.Drawing.Imaging.ImageFormat.Png);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRandomEngyView Question on Stackoverflow
Solution 1 - C#bohdan_trotsenkoView Answer on Stackoverflow
Solution 2 - C#Leniel MaccaferriView Answer on Stackoverflow
Solution 3 - C#run2thesunView Answer on Stackoverflow
Solution 4 - C#Hans PassantView Answer on Stackoverflow
Solution 5 - C#Ben VoigtView Answer on Stackoverflow
Solution 6 - C#DileriumLView Answer on Stackoverflow
Solution 7 - C#Andreas RejbrandView Answer on Stackoverflow
Solution 8 - C#Hun1AhpuView Answer on Stackoverflow
Solution 9 - C#User1View Answer on Stackoverflow
Solution 10 - C#David ColemanView Answer on Stackoverflow