Retrieving Application Icon in WPF

As I started doing more and more work in WPF, I had to port my “About” dialog. In attempt to keep everything clean of Windows Forms that also meant that I need to find another way of loading icon (since Bitmap class is not part of WPF).

Code follows same logic as one for Windows Forms. Only important difference is use of ImageSource as our final destination.

var hLibrary = NativeMethods.LoadLibrary(Assembly.GetEntryAssembly().Location);
if (hLibrary != IntPtr.Zero) {
    var hIcon = NativeMethods.LoadIcon(hLibrary, "#32512");
    if (hIcon != IntPtr.Zero) {
        return Imaging.CreateBitmapSourceFromHIcon(hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
    }
}

Without further ado, here is full sample.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.