alansiqueira27
alansiqueira27

Reputation: 8506

Can anyone explain me how to add Image using WPF?

I already did this:


var uriSource = new Uri(@"/WpfApplication1;component/Untitled.png", UriKind.Relative);
image1.Source = new BitmapImage(uriSource);

My namespace is WpfApplication1, my image is at main folder of the project. It still doesn't work. What am I doing wrong? It's already set to Resource. When I debug, it says these erros: Height = 'image.Source.Height' threw an exception of type 'System.IO.DirectoryNotFoundException' Metadata = '(image.Source).Metadata' threw an exception of type 'System.NotSupportedException'

Upvotes: 0

Views: 2622

Answers (3)

abramlimpin
abramlimpin

Reputation: 5077

Does

BitmapImage bmi = new BitmapImage(new Uri(r[0].ToString(), UriKind.Relative));

image1.Source = bmi; // location + filename

work? Assuming that your XAML is somehow similar to this one:

<Image x:Name="image1" HorizontalAlignment="Left" Width="175" Stretch="Fill" Margin="373.428,8,0,5.059">
    <Image.BindingGroup>
        <BindingGroup/>
    </Image.BindingGroup>
</Image>

I've added an Image control inside my Canvas/Layout. (Just having a wild guess)

Upvotes: 1

alansiqueira27
alansiqueira27

Reputation: 8506

Damn it, it's simple as Silverlight. But it doesn't support png files to do like this. So this how I did to work. Just changed to a jpg image:


Image image = new Image();
image.Width = 50;
image.Height = 50;
image.Source = new BitmapImage(new Uri("/soccer.jpg", UriKind.Relative));

mainCanvas.Children.Add(image);

Upvotes: 0

Stewbob
Stewbob

Reputation: 16899

Make sure that 'Untitled.png' has its Build Action Property set to 'Resource'. Click on the file in the Solution Explorer, show the Properties dialog, and change the Build Action property.

 Dim bi As New BitmapImage()
 bi.BeginInit()
 bi.UriSource = New Uri("/WpfApplication1;component/Untitled.png", UriKind.Relative)
 bi.EndInit()

Upvotes: 3

Related Questions