locked
Use C# icon for WPF RRS feed

  • Question

  • How does one attach an existing icon, sample.ico, to WPF?  I tried placing sample.ico into the same subdir as the .xlam files, then adding <Window Icon="sample.ico"> to MainWindow.xaml => "XlamParseException occurred".

    cte677

    Friday, June 20, 2014 2:43 PM

Answers

  • I have a solution.  Right click Project -> Application -> Resources -> Icon and manifest -> Icon: -> select sample.ico.

    The entire procedure (I think I have it right):

    Using Visual Studio, create Resources dir (place sample.ico in dir)
    Right click Project -> Application -> Resources -> Icon and manifest -> Icon: -> select sample.ico
    Right click Resources -> Add -> Existing Item -> select sample.ico
    Right click sample.ico -> Properties -> Advanced -> Resource
    Add <Window x:Class= .... Icon="Resources\sample.ico">


    cte677

    • Proposed as answer by Magnus (MM8)MVP Wednesday, June 25, 2014 9:40 AM
    • Marked as answer by cte677 Wednesday, June 25, 2014 12:25 PM
    Tuesday, June 24, 2014 6:58 PM

All replies

  • The standard is to put into resources folder. Create a Resources folder and put the ico in there

    So you would reference it like

    <Window

    Icon="Resources\main.ico"

    >

    Are you sure your xaml exception is related to icon?

    Friday, June 20, 2014 2:50 PM
  • Did you literally just copy the file?

    You also need to include it in the project.

    Right click project

    Add

    Existing  item

    Change drop down bottom right to all files.

    Select .ico file

    There are two ways to set an icon.

    Setting the icon on the main window and in project properties.

    I recommend the latter.

    Right click the project

    Click properties

    The first (Application) tab will be default.

    Choose the icon in the box under icon and manifest.

    .

    The reason I recommend this is because the icon is associated with the main window.  The first window loaded has significance. 

    It's quite easy to be working on some application, add some menu and set that as the main window only to find your icon suddenly disappears.

    So the "safest" way is the project properties.

    .

    When you distribute the application later, don't forget you need to distribute the icon file as well.  You set it's properties Build action and copy to output directory.

    Or you can instead compile it into your exe and you might like another recent thread which has a link to a (very small) solution:

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/6d5c63f8-42d3-4a02-ab98-6f07338fa155/change-in-appllication-exe-icon-at-runtime-not-getting-reflected-after-installation?forum=wpf#91ad6d60-4518-43ba-aceb-6fa4524d3aac

    Friday, June 20, 2014 3:41 PM
  • Just a note on this to Add onto what Andy has replied, And please note that Andy has given you the correct response to make it so the icon does display, but going back to many versions of WPF, and people keep telling me that it is fixed, it's not!!!

    For the icon to appear, you have to run the application from OUTSIDE Visual Studio. I am running with VS2012, and my icon does not display (I still get the VS Default icon) but once I build the app and then go into the bin folder and run the Application, from windows, the icon does show properly. If you try running from within VS, you get the default, and you think it is not working. Build the project, got through windows explorer to where you are building the project and drill down to the bin folder. You should have a Debug or Release folder. Go into either one and you will see your Application there and double click. Now check out the taskbar and see that the proper icon is displayed.

    Friday, June 20, 2014 4:20 PM
  • I did the following:

    1) Placed sample.ico in a Resources folder.

    2) Project -> Properties -> Application -> Icon and manifest -> Icon: -> browse to sample.ico.  The icon displays to the right of the combo box.

    3) Project -> Properties -> Resources -> Add Resource -> Add Existing File -> browse to sample.ico.  The icon displays to the right of the combo box.

    4) Add to MainWindow.xaml -> <Window x:Class= .... Icon="Resources\sample.ico">.

    5) F5 -> XamlParseException:

    'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '8' and line position '9'.



    cte677

    Monday, June 23, 2014 3:17 PM
  • 1. Right-click on your WPF application project in the Solution Explorer in Visual Studio and select "Add"->"Existing item". Select "All Files (*.*)", browse to the directory on your hard drive where the .ico file is located and add it to the project by clicking on the "Add" button.

    2. Make sure that the Build Action property of the added file is set to Resource (right-click on the .ico file in the Solution Explorer in VS and select "Properties"):

    3. You should then be able to set the Icon property of the window to your .ico file:

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" Icon="path.ico">
    

    Monday, June 23, 2014 3:27 PM
  • 1) Create Resources folder by right clicking project and choosing add folder.

    2) Place ico file in folder

    3) Right click project, Add >existing item >  choose ico *( you have to change the drop down ).

    4) Check properties on the file - you need Build Action: resource, Copy to output Directory: copy if newer

    5) Right click project > Properties > Choose ico file.

    Note:

    You don't need to add the ico to mainwindow - as I explained above.

    You must create the Resource folder in visual studio

    There's a bug in wpf and you will see the default icon in your tray when you f5. Copy the exe and resources folder  somewhere out of bin/debug and run that to confirm it will look OK for the user.

    Monday, June 23, 2014 3:47 PM
  • I had already added sample.ico to the project ... forgot to mention that.

    This is new ... sample.ico -> Properties -> Advanced -> Build Action -> Resource.

    This is also new ... sample.ico -> Properties -> Advanced -> Copy to Output Dir -> Copy if newer.

    The Resources folder was created, and .ico was added within Visual Studio as described in the previous post. Placing the cursor next to the .ico does display the correct image.

    F5 still leads to the same exception message.

    ...

    I decided to remove and re-add the .ico to the project.  It works now.  Hmmmmmmmmmmmm? 

    Do I need to insert <Window x:Class= .... Icon="Resources\sample.ico"> into every dialog box?



    cte677

    Monday, June 23, 2014 6:54 PM
  • No you don't need to set it on every window.

    The main window ( the first window loaded has significance ) and the project are the two ways to set it.

    Unless you want different icons per screen.

    .

    Could you please mark posts which helped and or answered your question.

    Monday, June 23, 2014 7:16 PM
  • I just noticed ... the .exe does not display the icon when on the desktop.

    cte677

    Monday, June 23, 2014 7:17 PM
  • When I look at my exe in bin/debug in file explorer the little picture next to the .exe is the icon.

    When I drag that to some other folder I see the icon.

    When I drag it onto my screen I see the icon.

    example solution

    Tuesday, June 24, 2014 7:59 AM
  • My .exe in bin\Debug does not display the icon.  What needs to be changed?

    cte677

    Tuesday, June 24, 2014 12:39 PM
  • See the link in my post above?

    It's an example solution.

    Tuesday, June 24, 2014 1:54 PM
  • I went to the link ... did not realize it was a zip file.   I downloaded it.

    Compiling / Running does create an .exe that has the icon.  However, it is not clear to me what is missing in my code.

    To summarize the previous posts, the action I have taken is:

    Using Visual Studio, create Resources dir (place sample.ico in dir)
    Right click Resources -> Add -> Existing Item -> select sample.ico
    Right click sample.ico -> Properties -> Advanced -> Resource
    Add <Window x:Class= .... Icon="Resources\sample.ico">

    The icon displays properly on the dialog box.  However, it does not display properly when the .exe is placed on the desktop. 

    What action is missing?  Thanks!


    cte677

    Tuesday, June 24, 2014 2:33 PM
  • I notice you're setting the icon on the window rather than the project but that should work if it's the main window.

    When you compile, do you get an icon file in your bin/debug/Resources folder?

    If not then you need the properties set as you said you did in the post above.

    Build Action: Resource

    Copy to output directory: Copy if newer.

    If that isn't the problem then maybe you broke your solution/project files somehow.

    .

    When you move the exe round - it'll be expecting a resources folder with the ico in it relative to where the exe is.

    Tuesday, June 24, 2014 3:37 PM
  • I have a solution.  Right click Project -> Application -> Resources -> Icon and manifest -> Icon: -> select sample.ico.

    The entire procedure (I think I have it right):

    Using Visual Studio, create Resources dir (place sample.ico in dir)
    Right click Project -> Application -> Resources -> Icon and manifest -> Icon: -> select sample.ico
    Right click Resources -> Add -> Existing Item -> select sample.ico
    Right click sample.ico -> Properties -> Advanced -> Resource
    Add <Window x:Class= .... Icon="Resources\sample.ico">


    cte677

    • Proposed as answer by Magnus (MM8)MVP Wednesday, June 25, 2014 9:40 AM
    • Marked as answer by cte677 Wednesday, June 25, 2014 12:25 PM
    Tuesday, June 24, 2014 6:58 PM