4

I have the following directory structure

Project
\Images
 +view.png
control.xaml

and in the control I have a button defined by the following XAML:

<Button Click="Search"
        Grid.Column="1"
        Margin="0,5,5, 0"
        HorizontalAlignment="Right">
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Image Source="pack://application:,,,/images/view.png"
                   Width="16"
                   Height="16"
                   ToolTip="Search"
                   Cursor="Hand"
                   Opacity="0.8" />
        </ControlTemplate>
    </Button.Template>
</Button>

However, neither this pack URI method nor the "/images/view.png" is working. As I understand it, this is the same issue this question raises. However, I get the same error. The confusing thing is that in designer in Visual Studio 2008, the image renders correctly, but on the call to the InitializeComponent() call, I get:

Cannot convert string 'pack://application:,,,/images/view.png' in attribute 'Source' to object of type 'System.Windows.Media.ImageSource'. Cannot locate resource 'images/view.png'. Error at object 'System.Windows.Controls.ControlTemplate' in markup file 'RecapSpecEditControl;component/modaltreadgroupdatadialog.xaml' Line 61 Position 40.

I thought that maybe there was a namespace that I had to declare but according to the msdn site I believe I don't have to do anything like that.

Community
  • 1
  • 1
Anthony Potts
  • 8,842
  • 8
  • 41
  • 56

3 Answers3

9

I actually got this to work, but had to set my source to "/ProjectName;component/images/view.png" Because I have the ProjectName as a referenced assembly this is then the same as the Path: portion at the msdn page that I referenced in the question.

Anthony Potts
  • 8,842
  • 8
  • 41
  • 56
6

Set the Build Action for 'view.png' to Resource instead of Content and this problem should go away. I was able to reproduce your problem this way and it works correctly when set as a Resource.

Ben Collier
  • 2,632
  • 1
  • 23
  • 18
  • 1
    I thought you might be on to something, but my build action on the image is already set to resource. What about the Copy to Output directory, what do you have that set as? – Anthony Potts Dec 09 '09 at 15:52
  • 'Do not copy'. Interesting. I thought for sure that was it, as it displayed in the designer and gave me the exact same error at runtime when I had it building as Content. – Ben Collier Dec 09 '09 at 15:56
  • 1
    Your answer made total sense, and I was certain that I would see that my build action was wrong. – Anthony Potts Dec 09 '09 at 16:10
  • Found the solution, but yours is definitely worth noting as a possible answer under the right circumstances as you demonstrated. Thanks for the help. – Anthony Potts Dec 09 '09 at 16:31
  • +1 for pointing me in the right direction. Similar issue as OP but I had my Build Action to 'Embedded Resource' switching to 'Resource' corrected it. – OttPrime Jul 08 '16 at 14:57
-3

Xaml.VB

Call the Image from Application folder and Design Page

Private Sub LoadImages()
        Dim strUri As String
        strUri = AppDomain.CurrentDomain.BaseDirectory() & "\NavigationImages\settingsicon.png"
        Image2.Source = New BitmapImage(New Uri(strUri))
    End Sub

Page load in Xaml.VB

Call LoadImages()

Xaml Design Page

Image Name="Image2"Height="32" HorizontalAlignment="Left" 
CoolBeans
  • 20,654
  • 10
  • 86
  • 101
Ayyappan
  • 5
  • 1