Silverlight background image - silverlight

My problem is that the image only ever shows in the designer. It always shows there no matter what method I try, but once I actually start the program it's nothing but white.
My XAML code:
<Grid.Background>
<ImageBrush ImageSource="C:\Users\Martin Clemens Bloch\Desktop\Mappe of
Text\DTU\Bachelor\Silverlight\OrgOS\OrgOS\Data\BackgroundS.jpeg"/>
</Grid.Background>
The Grid in question is at the very front so there should be nothing "blocking the view" to the background image,
The image has been added to the project and I have tried different build actions including "resource", "content" and "embedded resource" combined with "copy always" and "do not copy".
I have also tried a relative path of "/BackgroundS.jpg" while placing the image in the clientbin with the XAP file.. and other places.
I also tried putting the image INSIDE the XAP file by renaming the xap to zip and then back after putting the image.
I have also restarted VS2011.
Some I tried on my own, other stuff after numerous tutorials and stackoverflow questions.
Now we are talking about an IMAGE here, what am I doing so wrong!?

You need to include your image as a Resource in the silverlight project, and reference it like this:
<ImageBrush ImageSource="/The.Assembly.With.Images;component/the/path/to/the/images/myimage.png"/>

Related

Set default xaml window background image in dll

I am currently developing a default WPF control Kit.
But I am stuck with using the correct kind of uri in xaml.
What I have is an image wich should be used as the background for the non-client area of my window.
To make the default controls available very easy I want to put everything in a dll.
Other apps can quickly reference that dll and get access to the style.
The problem is, that my image is not showing up when using the dll style in an app.
My image (/Resources/WindowBackground.jpg) is set to Resource and I am using the following xaml:
<Image Grid.ColumnSpan="99" Grid.RowSpan="99">
<Image.OpacityMask>
<ImageBrush ImageSource="pack://application:,,,/Resources/WindowBackground.jpg"/>
</Image.OpacityMask>
</Image>
I also tried:
<Image Grid.ColumnSpan="99" Grid.RowSpan="99" Source="/Resources/WindowBackground.jpg"/>
Both write the following into the output (Couple times):
..."System.IO.IOException" in PresentationFramework.dll...
I also tried lots of other uris wich sometimes lead to XamlParseExeptions and other not so nice stuff.
Thank you for any hints :D
You can find your answer in the Pack URIs in WPF page on MSDN. For your particular situation, you can use the following syntax to reference your resource image file:
pack://application:,,,/ReferencedAssembly;component/Resources/WindowBackground.‌​jpg
From the linked page:
The following example shows the pack URI for a XAML resource file that is located in a subfolder of the referenced assembly's project folder.
pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml
Note: The type of resource file here is irrelevant.

Embedded fonts works for designer but not for application

I am building a wpf application and I wanted to use Open Sans Regular font in my application.
I referred this link for embedding font. I added OpenSans Regular.ttf file to resources under project properties.
Then I referred them in my application as mentioned below:
<Window x:Class="FontEmbeddingDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" FontFamily="Resources/#Open Sans">
<Window.Resources>
</Window.Resources>
<Grid>
<TextBlock Height="100" Text="This is test text." FontSize="14" FontFamily="Resources/#Open Sans"/>
</Grid>
</Window>
In designer of Visual Studio I can see font changed to open sans but when I run the application it is taking system default font (Arial in my system).
Do let me know if you need any other information.
From MSDN Page:
When you add fonts as resources to your application, make sure you are
setting the <Resource> element, and not the element
in your application's project file. The <EmbeddedResource> element for
the build action is not supported.
So, I guess, you will have to add this font file to your project as you would add any other file and set its BuildAction to Resource instead of adding the font to the Resources under Project Properties, which will make it as an EmbeddedResource.
EDIT
Read this excellent article on how to use custom fonts in a WPF application.
You could also, get this working by way #Sheridan mentioned - which is setting BuildAction to Content. However, the problem with this approach is you will have loose separate file hanging around along with your binary. Users can potentially change this file, which may cause problems with your app. It is best to use Resource as BuildAction as the font gets bundled into the assembly.
In my opinion, I believe that the accepted answer to this question may either be incorrect, or misleading. I am using Font files in exactly the same way and I have absolutely no need to set their BuildAction to Resource. My Font files have a BuldAction set to Content and that works just fine. I'm guessing that the accepted answer would only help users that have set their Font file to EmbeddedResource.
The comment that #sthotakura quoted from the linked MSDN page is merely talking about manually editing the project file, which the question author is not doing. Note that there is no mention of BuildAction property in the linked page apart from mentioning that the EmbeddedResource value is invalid in this case.
Please try this instead or as well:
Set the BuildAction of the Font file(s) to Content.
Reference the Font file like this (with the all important starting slash):
FontFamily="/Resources/#Open Sans"
I just tried removing the starting slash in my project and the Font defaulted to another Font, so I'm pretty sure that this will work... all the same, please let me know. I'm more than happy to remove this answer if I am mistaken.

Silverlight Images won't show in web

This is a nubie question, I have a silverlight 5 (VB) application that runs fine and displays all 3 png images when I run it from my PC. When the app is put onto the web it doesn't show the png Images. The build I have selected for the png files is Resource? I checked the XAP.zip to see if the png files are there and they are.
I've done a bit of research and found that I may need some coding to enable me to get these up and running on the web unfortunately everything I've looked at is in C. I have found these two pieces of code :
<Imagex:Name="myIamge"Source="../computer.JPG"></Image>
myIamge.Source = new BitmapImage(new Uri("../Image/computer.JPG", UriKind.Relative));
Are these two pieces of code what I need?
Do I need them both together?
Does anyone know the conversion Of the second piece of code from c to vb
Is there anyone out there that can tell me how to do this in silverlight VB
Regards
Will
Are these two pieces of code what I need?
Probably, hard to say because you did not explain us where you put physically your 'computer.JPG'
Do I need them both together?
No, if you display this image only once, and never change it after, you do no need the code behind. Just use your xaml declaration.
So, what you have done seems pretty correct: your image should be declared as ressource, and you use the path to specify where the image is, compare to the XAML file using your image.
Thus, for instance if the declaration is in the MainPage.xaml (to the root of your project), and your image in an Image folder, this declaration is correct.
<Image x:Name="myImage" Source="Image/computer.jpg"/>
If despite this, you cannot see your image, I will recommand you to check the website where you deploy your xap file, maybe there is an error explaining why the image can not show up (MIME type, .net version?...)

Why does the Image element give me a design-time error with "add as link"ed images?

I need to display an image, which I've done without problems before, but today I decided to be tricky and use "add as link" instead. Well, now I get:
The file Images/hello.png is not part of the project or its 'Build Action' property is not set to 'Resource'.
Wait... its Build Action is set to Resource. I've seen a Silverlight solution that involves the usage of Merged Dictionaries to share files between Silverlight and WPF projects, but it's not clear to me that this would even apply to my WPF + Image issue.
Has anyone solved this problem before? I could make copies of all of the images, but that seems a little silly if I have a shared repository with clip art and the like.
Dave,
I've just tried to add image as a link to plain WPF application. Build action is "Resource" (don't confuse with "Embedded Resource"). I've added it to the root, and refer to it as <Image Source="/file_name.jpg"/> - all works fine.
The message you have is it compile or runtime? If it's a runtime, how do you refer to the image? Do you see it in Reflector, when you open your assembly (it should be under Resources folder)?
I have images in one assembly which I want to share into another. I've used Add as Link in my second assembly. In the project where the actual image files are located they are in a Resources\Images folder. In the project which links to those files the links are also in a Resources\Images folder. At runtime a XamlParseException claiming "cannot locate resource" is thrown.
My xaml which is referencing the image is in a UserControls folder.
In the project which actually contains the images the path ..\Resources\Images\Blah.png works fine as expected.
Opening the DLLs in Reflector makes it obvious that in the assembly with the linked images holds the images at the root level - the compiler is not respecting the folder location. So in the project with the linked files I have to use ..\Blah.png to find the resource.
Surely a bug in the compiler?

Why can't I set the Image Source in WPF app?

I'm just learning WPF. I'm using VS 2008. I've added an Image control to my XAML, and I added a GIF file to the project. If I use the XAML Designer's property panel to set the image's Source property, the drop-down list for the Source property contains the following:
C:\Sample Project;component/pinHorizontal.gif
There are several problems with this:
My project, named "Sample Project," is not in the root of my drive.
Why is ";component" in there?
If I select this value as given, I get the error "Property value is not valid" (yeah, no kidding).
If I go into the XAML source and set the Source property manually, like so:
<Image Name="PinImage" Source="pinHorizontal.gif"/>
The XAML Designer gives me this error:
"The file pinHorizontal.gif is not part of the project or its 'Build Action' property is not set to 'Resource'."
Why is this task so difficult? How do I assign an image source?
Have a read through this article - though be warned it's not particularly light reading :-)
I suspect what you want for your image path is pack://application:,,,/pinHorizontal.gif. if your image is set to a BuildAction of Resource, this will work fine.
Right click on the image file(pinHorizontal.gif) in the SolutionExplorer, Go to properties, here is the 'Build Action' property, you need to set it to 'Resource'. Hope it helps!!
This post is an "and also"... not an answer to the original question.
I just thought I'd document it in case some other poor booger has this problem in future... and googling that error message finds this SO question... so here's a good place to do so.
I've been creating new icons (actually bitmaps) and adding them "on the fly" to my Visual Studio 2008 project (i.e. Add ~ Existing item). The IDE has been reporting file ${filename} is not part of the project or its 'Build Action' property is not set to 'Resource' errors on my XAML page... but my project builds and runs OK... so what gives???
I tried everything I could think of the get visual studio to refresh it's view of the filesystem, to no avail.
Well, I've just dicovered that restarting Visual Studio "refeshes" its cache of filesystem/build-output contents, which makes those annoying non-errors in the IDE go away. So I add a batch of icons, mark them all as Resources ~ Copy if newer, build, and then restart the IDE, and it's all good.
Cheers all. Keith.
In the Solution Explorer, select your project and right click rebuild the solution, and try to add the image again, it should work fine.
--or--
under the Build tab select rebuild the solution, and try to add the image again.
You can use this piece of code in the XAML to load an image from the absolute path without setting nothing. Use DecodePixelWidth or DecodePixelHeight to save application memory.
<Image>
<Image.Source>
<BitmapImage DecodePixelWidth="200" UriSource="C:\image.png" />
</Image.Source>
</Image>
Hope it helps!
I was having the exact same problem. I just manually typed in the location of the file when the Choose image box pops up and that worked for me.
I opened the Choose image box again after I got it to work and noticed that file:/// was now in front of the file location. I know next to nothing about Visual Basic (which is what I'm using) so there may be a good explanation for this, but I don't have it.

Resources