ImageSource as a Resource

Hey guys,

I wanted to store an image as a resource. Under the Feb 2006 I had this:

<ImageSource x:Key="Image_RedBall">pack://application:,,,/Images/bullet_ball_glass_red.png</ImageSource>

Which worked fine. I could then use "{StaticResource Image_RedBall}" in any property that needed an image source.

However in the WinFX Beta2, this XAML snippet does not work (there's no public constructor blah blah blah).

How would I go about accomplishing the same thing under the new beta 2, without hardcoding the pack:// URIs everywhere that needs an image source



Answer this question

ImageSource as a Resource

  • Dejjan

    Thanks for the reply Thomas,

    I have this:

    <Application.Resources>
    <ImageSource x:Key="Image_RedBall">pack://application:,,,/Images/bullet_ball_glass_red.png</ImageSource>
    <Application.Resources>

    There are no errors when I try to compile, however when the application runs, it crashes. If I open the Application.xaml file in the Cider designer, I recieve this error:

    Error 1 Type 'System.Windows.Media.ImageSource' does not have a parameterless constructor that is accessible by the assembly that contains the Xaml derived code and thus cannot be constructed via XAML. D:\Projects\Trial Balance\Source Code\PaulStovell.TrialBalance.StudentEdition\Application.xaml 78 6 PaulStovell.TrialBalance.StudentEdition

    This isn't limited to my PC as someone else reported to me that the code wasn't working.



  • rwiegel

    This works fine for me:

    <Grid>

    <Grid.Resources>

    <ImageSource x:Key="MyImage">pack://application:,,,/Images/WinFX.jpg</ImageSource>

    </Grid.Resources>

    <Button HorizontalAlignment="Center" VerticalAlignment="Center" >

    <Image Source="{StaticResource MyImage}" />

    </Button>

    </Grid>



  • Lesley-Ann Vaughan

    Hi Thomas,

    It turns out my problem wasn't so much the image, but trying to use it as an Icon for a Window. When I removed that, it all ran smoothly. This is curious because it worked on Beta 3.

    I still get errors from the Cyder designer about the public constructor, but I'll ignore that for now.

    Thanks!



  • ImageSource as a Resource