Microsoft Developer Network > 포럼 홈 > Windows Presentation Foundation (WPF) > Displaying icons in Image control - inconsistent behavior
질문하기질문하기
 

답변됨Displaying icons in Image control - inconsistent behavior

  • 2007년 10월 4일 목요일 오후 10:57Steph Grush 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    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

답변

  • 2007년 10월 9일 화요일 오후 8:18Jordan Parker - MSFT 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨

    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.

모든 응답

  • 2007년 10월 5일 금요일 오전 5:00Dr. WPF 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    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?

  • 2007년 10월 8일 월요일 오후 7:32Steph Grush 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    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

  • 2007년 10월 8일 월요일 오후 8:54Dr. WPF 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    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.

  • 2007년 10월 9일 화요일 오후 8:18Jordan Parker - MSFT 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨

    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.

  • 2007년 10월 9일 화요일 오후 10:45Steph Grush 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

     

    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