I added a bunch of images as project resources.
Now i want to use them in my WPF application using the image control.
How can I assign the resource to the source of the image control?
First, mark you image file as a "Resource" in the properties window of Visual Studio. Then you can quite easily reference the resource using the file name:
<Image x:Name="image1" Source="theimage.png" />
If you have put your image in a folder, you can use
<Image x:Name="image1" Source="/folder/theimage.png" />
You wanted to reference your image in XAML right?
like this
<Image Source="Resources\MyImage.png" />
and you dont need to add the image to the project resource. just add the images to your project via solution explorer
Related
My Windows Store app/Metro/Win RT app downloads image from server on to some local folder. I need to bind Image control to the downloaded image at runtime.
The problem is that image doesn't show up unless I add it as a binary resource to the project.
My downloaded images are stored in ProjectFolder/Data/Media. Now, here is how I bind image source to image control.
<Image x:Name="WriterImage" Stretch="None" Source="{Binding Path=PersonData.Photo.MediaImageSource"></Image>
public ImageSource MediaImageSource
{
// Here _MediaUrl gets a value: ms-appx:///Data/Media/Writer1.jpg
BitmapImage source = new BitmapImage(new Uri(_MediaUrl));
}
This works only if I add Writer1.jpg as a resource to the project. If I remove it from the project, it doesn't show up.
Note that there are different URL schemes that your application can access:
ms-appx:/// is a read-only location, and refers to files that are included (compiled) with your application, such as resources.
ms-appdata:///local/ refers to local read-write storage for your application. If you are downloading files, my guess is that you should use this URL scheme.
For your example above, I would try using the following URL:
ms-appdata:///local/Data/Media/Writer1.jpg
See the following for more info about URL (URI) schemes: http://msdn.microsoft.com/en-us/library/windows/apps/jj655406.aspx
I have some icons in my WPF project to make the application more user friendly. WPF shows all icons on my computer but when I run the app on other computers it will show some of icons but not all of them. How can I fix it?
Update:
<Image Source="Office/Add-Female-User.ico" Stretch="None" />
I'm sure that all images are accessible because some of them are visible.
You are referencing your images (in your image controls) as Content not Resource. I suspect the problem is your images aren't copied to the output dir. View properties on missing image files and set build action to Content and Copy to output dir to Copy if newer.
For resource referencing see http://msdn.microsoft.com/en-us/library/aa970069.aspx.
How to determine that UserControl has been loaded completely?
I have some UserControls with images and media files from another server bound like that:
<Image Source="{Binding Image}" />
I would like to know when all my images are downloaded and ready to be shown.
I've noticed an event ImageLoaded, and probably I can listen to it on all of my Image controls. Isn't there a better way to do that?
The class FrameworkElement contains a public event called "Loaded", so naturally all classes derviving from it (UserControl class) expose it as well.
MSDN
The question would be : Are you downloading your images ? (This can be after loading of the control Check here) Or are you embedding your images in the project? They'll be a part of the .xap file that's downloaded when you launch your application.
You can add an event handler to that event to execute code when the said event occurrs. Hope that helps.
Here's a screenshot:
How can I display the 'no.png' image? It seems changing it to Resource worked.
How can I change the image in my code-behind c# file?
imgStatus.Source = new ur???
You can use the format like this Source="/YourProjectName;component/images/yes.png"
The images folder should be resided in your project root directory.
The Visual Studio can automatically generate the path in the image control's property setting panel.
Because I want to do things like load images from the web (see this post). To see this in action try creating a Silverlight Project without a website and with a website and past the following into the main page. On the web project things work as expected, in the file based project you get a white screen.
<Grid x:Name="LayoutRoot" Background="White">
<ListBox>
<ListBoxItem>
<Image Source="http://sstatic.net/so/img/logo.png"/>
</ListBoxItem>
<ListBoxItem>
<Image Source="http://sstatic.net/so/img/logo.png"/>
</ListBoxItem>
<ListBoxItem>
<Image Source="http://sstatic.net/so/img/logo.png"/>
</ListBoxItem>
<ListBoxItem>
<Image Source="http://sstatic.net/so/img/logo.png"/>
</ListBoxItem>
</ListBox>
</Grid>
So I guess I need to move my Silverlight project from using a default file to an http accessible location. How do I move my development files to a website and still be able to compile, debug, etc from Visual Studio. Is the only way to do this create a new Solution as web project and go and Add everything to it?
When you add a Silverlight project to your ASP.NET project it will generate two test pages: one in aspx and one in html.
For a non-ASP.NET website use the html file and everything that it references such as Silverlight.js and the ClientBin directory.
ASP.NET is not required for hosting Silverlight.
I created a new Silverlight project with an ASP net application and used that as my Web application base (copying it to the old Solutions directory and adding it to the Solution). Setting the Web Applicaiton to the starting project and a quick edit to the Silverlight Applications tab of the properties and everything worked. I just take the .html page and the xap from the ClientBin to move it to a live website, and images are working perfectly.