Le réseau pour les développeurs > Forums - Accueil > Windows Presentation Foundation (WPF) > Displaying icons in Image control - inconsistent behavior
Poser une questionPoser une question
 

TraitéeDisplaying icons in Image control - inconsistent behavior

  • jeudi 4 octobre 2007 22:57Steph Grush Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    I have some xaml:

     

    <Window
     x:Class="Windowing.MyWindow"

     >
      <StackPanel>
       <TextBlock Text="This is a window!"/>
       <Image Source="image.ico"/>
      </StackPanel>
    </Window>

     

    (I also have a trivial code-behind that calls InitializeComponent in the constructor.)

     

    I open instances of this Window like this:

     

       MyWindow win =
        new MyWindow();
       win.Show();  
      

    When I open a single instance of this Window, this works fine. But as soon as I open a second instance, the image stops appearing. This issue does not occur with a PNG. Both are declared as resources in the project file like this:

     

     <Resource Include="image.ico"/>

     

    I am seeing hints that the Image control won't take an icon as a Source without some preprocessing, yet it seems to work for a single Window. I can create as many instances of the icon as I want in dfferent Image controls within a single Window.

     

    Are resource files in an assembly handled differently when they are icons, or is the difference based on the behavior of the Image control? What causes this behavior?

     

    Thanks,

    Steph

Réponses

  • mardi 9 octobre 2007 20:18Jordan Parker - MSFT Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée

    Dr. WPF is correct -- that's a new Vista icon because it contains a PNG:

     

    >findstr /sni PNG unknown.ico
    unknown.ico:1:  ☺ ♣     ☺   ↕♫  V   @@  ☺   (B  h♫  00  ☺   ¿%  ÉP      ☺   ¿►  8v  ►►  ☺   h♦  αå  ëPNG

     

    Vista's PNG icons are not supported in WPF. Either remove the PNG candidate from the icon or re-save the icon as a bitmap. I did the latter and it worked fine.

Toutes les réponses

  • vendredi 5 octobre 2007 05:00Dr. WPF Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    I can't repro this.  I can open as many instances of the window as I want and they all contain the ico Image.  Do you have a small repro that you can post somewhere?

  • lundi 8 octobre 2007 19:32Steph Grush Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    I have uploaded my test project here:

     

    http://steph.grush.googlepages.com/icondisplayersample

     

    Interesting that it seems to work for you. I wonder if I have done something wrong. Please let me know if the sample reproduces the issue for you, and if not, what version of the framework you have - I have not installed the 3.5 beta yet.

     

    The error occurring in the sample code appears to be an ImageFailed event. The MSDN library says that

    Conditions in which this event can occur include:

    • File not found.
    • Invalid file format - unrecognized or unsupported format.
    • Unknown file format decoding error after upload.

    It doesn't seem like any of these things could be happening, however, since the image is displayed the first time.

     

    Thanks,

    Steph

  • lundi 8 octobre 2007 20:54Dr. WPF Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

    Weird!

     

    It's not anything you're doing wrong.  It's something about that particular icon.

     

    I dropped in about a dozen other icons from my system and *most* of them worked fine.  I found one other icon that failed in the same manner as your "unknown.ico".  I opened all the icon files in Photoshop Elements to look for commonalities.  The ones that worked always had a number of icons in the common formats and sizes.  However, the 2 that failed (mine and yours) both contained a number of normal formats plus one weird format:

     

    e.g., 169478669 x 109051904 @ 21060 bits/pixel

     

    Clearly, this is Elements not understanding the format.  I'm guessing the real format is one of the new 256x256 PNG compressed formats.

     

    I'd say this represents a bug in WPF's icon bitmap decoder.  Very weird that it succeeds the first time and fails thereafter.  You should definitely submit your sample and this report to the WPF Connect site.

     

    This bug repros on 3.5.

  • mardi 9 octobre 2007 20:18Jordan Parker - MSFT Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée

    Dr. WPF is correct -- that's a new Vista icon because it contains a PNG:

     

    >findstr /sni PNG unknown.ico
    unknown.ico:1:  ☺ ♣     ☺   ↕♫  V   @@  ☺   (B  h♫  00  ☺   ¿%  ÉP      ☺   ¿►  8v  ►►  ☺   h♦  αå  ëPNG

     

    Vista's PNG icons are not supported in WPF. Either remove the PNG candidate from the icon or re-save the icon as a bitmap. I did the latter and it worked fine.

  • mardi 9 octobre 2007 22:45Steph Grush Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     

     

    Good heavens! Somehow, it didn't even occur to me that WPF might not support vista icons, even though I knew Windows Forms didn't support them. You're absolutely right - that's the problem.

     

    Thanks for shedding light on a really mysterious issue.

     

    Steph