Dela via


How to: Embed an Icon in an Assembly

You can easily integrate resources, such as icons, into your Windows Presentation Foundation (WPF) application by using the Windows Presentation Foundation (WPF) Designer for Visual Studio. Use the Pack URI Scheme provided by Extensible Application Markup Language (XAML) to reference resources in your application's XAML. For more information, see Pack URIs in Windows Presentation Foundation

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To embed an icon in an assembly

  1. In Solution Explorer, right-click your project, point to Add, and then select New Folder. Name the new folder Resources.

  2. Using Windows Explorer, copy a .bmp file into the new Resources folder. You can use one of the bitmap files from the %windir% folder, for example, "Blue Lace 16.bmp".

  3. In Solution Explorer, right-click the Resources folder, point to Add, and then select Existing Item.

    The Add Existing Item dialog box appears.

  4. In the Objects of type drop-down list, select All Files.

  5. Browse to the Resources folder, select the bitmap, and then click the Add button.

    The bitmap is added to the project and appears in the Resources folder.

  6. In Solution Explorer, select the bitmap.

  7. In the Properties window, make sure that Build Action is set to Resource.

To reference an icon resource

  1. Open your application's main window XAML file in the WPF Designer, for example Window1.xaml.

  2. In XAML view, in the opening <Window> tag, set the Icon property to the following pack URI.

    Icon="pack://application:,,,/Resources/Blue Lace 16.bmp"
    

    Your Window declaration should appear similar to the following XAML.

    <Window x:Class="IconDemo.Window1"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300"
        Icon="pack://application:,,,/Resources/Blue Lace 16.bmp">
        <Grid>
    
        </Grid>
    </Window>
    
  3. Build and run the application.

    The bitmap appears in the main window's icon.

See Also

Concepts

Pack URIs in Windows Presentation Foundation

Other Resources

Resources (WPF)

Working with Controls in the WPF Designer