static BitmapSource GetWindowIcon(IntPtr windowHandle) { var hIcon = default(IntPtr); hIcon = WindowApi.SendMessage(windowHandle, (int)Message.WM_GETICON, WindowApi.ICON_BIG, IntPtr.Zero); if (hIcon == IntPtr.Zero) { hIcon = WindowApi.GetClassLongPtr(windowHandle, WindowApi.GCL_HICON); } if (hIcon == IntPtr.Zero) { //TODO: define this constant in the api. it's messy in here. hIcon = WindowApi.LoadIcon(IntPtr.Zero, (IntPtr)0x7F00 /*IDI_APPLICATION*/); } if (hIcon != IntPtr.Zero) { return(Imaging.CreateBitmapSourceFromHIcon(hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())); } else { throw new InvalidOperationException("Could not load window icon."); } }