Use system icons in WPF

Sometimes you would like to use a standard Windows system icon, such as error, warning, question etc. in your WPF program. The main reason is usually consistent look with the rest of the system. While you could have extracted the desired icon from one of the system resource files, there is a much better technique for doing it. First, add a reference to System.Drawing to your project. Then you can use this sample code to get the desired icon quickly:

  1.             var error = System.Drawing.SystemIcons.Error;
  2.             // You can adjust the parameters of BitmapSizeOptions to suite your specific needs
  3.             var image = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(error.Handle, 
  4.                 Int32Rect.Empty,
  5.                 BitmapSizeOptions.FromEmptyOptions());
  6.  
  7.             // icon has been declared as Image
  8.             icon.Source = image;

Further reading - MSDN library.

Topic: 

Add new comment

Subscribe to Comments for "Use system icons in WPF"