WPF - Import image as resource - wpf

In WinForms it is possible to import an image as a resource, and the image would still work when compiled in the /bin/Debug folder.
I can't figure out how to get this working in WPF, when I run the application the image doesn't load, because the image is saved in /Projects/AppName/images/, and the application is compiled into /Projects/AppName/bin/Debug when I run it in Debug mode.
Do I simply need to make a copy of my Images folder and put it where the application is compiled? Or is there another way. Here is my code which displays my image:
<Image Width="300">
<Image.Source>
<BitmapImage DecodePixelWidth="300" UriSource="/images/jamsnaps-dark.png" />
</Image.Source>
</Image>

Create a folder (e.g. images) in your Visual Studio Project.
Add the image file(s) to that folder.
Set their Build Action to Resource (in the Properties window, see second image in this answer).
Then write the UriSource property like you already did:
UriSource="/images/jamsnaps-dark.png"
That URI is effectively a Resource File Pack URI, where the prefix is automatically added by the XAML Parser.
In code behind, you would write
bitmap.UriSource = new Uri("pack://application:,,,/images/jamsnaps-dark.png");

Two options :
1) Go out from bin/Debug and in to your application folder by ../../ and then to your image.
<Image>
<Image.Source>
<BitmapImage UriSource="../../images/jamsnaps-dark.png" />
</Image.Source>
</Image>
2) Compile your images as Content from properties in the context menu on the image file , and then when compiled they would be placed in been debug.
I don't remember if you also need to tell them to copy local also in properties ( i'm not near a computer at the moment so i can't check .

Add the image to your project directory
In the solutions explorer, right click the image and select Include in Project
Build Action should be set to Resource by default.
You can then start to use the image path relative to the root e.g. images/text.jpg if you put it in a folder called images.

Related

DNN custom skin and css deployment

I am new to this and trying to figure this out. I have created a internal.ascx page and a Internal.css file for my internal page for a website. If I create a new page in DNN and apply this new skin it doesnt seem to apply the CSS. I copied the both ascx and css file to the _default/Skins/ folder.
Have also tried adding in the css via the following code
<dnn:DnnCssInclude runat="server" FilePath="/Internal.css" PathNameAlias="SkinPath" />
Nothing seems to be working. Have cleared my cache and tried different machine to view the page. But the style is still not coming through.
Thanks in advance
Create a new folder in the _default/Skins/. For example, call the folder "Internal" (/Portals/_default/Skins/Internal). Copy your ascx and css skin files into there. Rename the css to "skin.css". You shouldn't need to reference it in the ascx since DNN will pick it up based on the name.
Also, the reason your CSS include statement may not have worked was because filepath "/Internal.css" was probably trying to look for it in the root of the website. I would think it would simply be "Internal.css". But you could verify in Firebug what path it generated.

Swf file cannot load another swf files (with relative paths) in WPF AxShockwaveFlash control (hosted in WindowsFormsHost control)

I am using an AxShockwaveFlash control in WPF (hosted in WindowsFormsHost).
I have a player.swf file which uses external swf files from a folder (the path of the folder is relative to the player.swf file and the player.swf file works with this relative path).
I would like to play the player.swf file in the AxShockwaveFlash control but it cannot find the external swf files in the folder which is given by a relative path.
How can I solve this problem?
axShockwaveFlash.Base sets the directory that the .swf will look for its resources.
Note that a path like #"C:\Resoruces" doesn't seem not work, a trailing backslash is required.
#"C:\Resource\"

How to hide obj folder from WPF project

I don't know why but since yesterday I have the obj folder that appears in white in my wpf project workspace and I don't know how to hide it. This directory contains the g.i.cs file generated from .xaml file.
Does anyone knows how to hide it ?
Look for the Show All Files icon in the Solution Explorer window:
It shows/hides the files which are in the project folder but not included in the project itself.

WPF Custom fonts problem

This is really weird. In Blend 4, the custom font works when I see the application in the designer, but when I run it, the font is gone and it goes back to arial or something. This is my XAML:
<TextBlock Text="Text G" FontFamily="/ProjectName;component/Fonts/#Futura Lt BT" FontSize="48" Background="#FFC44747" />
The font is in a folder called "Fonts" and the control in which I'm trying the font is in a folder called "Controls". I know it must be a problem with the relative position of the "Fonts" folder to the "Controls" folder, but I've already tried a lot of stuff and it doesn't work.
Also, the XAML markup I put up there is what Blend creates when I select the custom font. The font is copied as a resource all right (I already check the csprof file and it's there).
Any ideas? This has been kicking my butt for a couple hours now.
Thanks.
While I understand that this is far too late to help the question author, I am leaving this to help future viewers of this question.
The information in this answer comes from the Packaging Fonts with Applications page on MSDN:
Adding Fonts as Content Items
You can add fonts to your application as project content items that are separate from the application's assembly files. This means that content items are not embedded as resources within an assembly. The following project file example shows how to define content items.
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Other project build settings ... -->
<ItemGroup>
<Content Include="Peric.ttf" />
<Content Include="Pericl.ttf" />
</ItemGroup>
</Project>
In order to ensure that the application can use the fonts at run time, the fonts must be accessible in the application's deployment directory. The element in the application's project file allows you to automatically copy the fonts to the application deployment directory during the build process. The following project file example shows how to copy fonts to the deployment directory.
<ItemGroup>
<Content Include="Peric.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Pericl.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
Adding Fonts as Resource Items
You can add fonts to your application as project resource items that are embedded within the application's assembly files. Using a separate subdirectory for resources helps to organize the application's project files. The following project file example shows how to define fonts as resource items in a separate subdirectory.
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Other project build settings ... -->
<ItemGroup>
<Resource Include="resources\Peric.ttf" />
<Resource Include="resources\Pericl.ttf" />
</ItemGroup>
</Project>
When you add fonts as resources to your application, make sure you are setting the element, and not the element in your application's project file. The element for the build action is not supported.
The following markup example shows how to reference the application's font resources.
<TextBlock FontFamily="./resources/#Pericles Light">
Aegean Sea
</TextBlock>
Please follow the above link for details on referencing Font resources from code and other useful information.
Everywhere over the internet and in books it says that when you add a font you should set the Build Action to "Resource" (example here). And it 'worked for a while. Anyway, to fix my problem, I had to change it from "Resource" to "Content".

System.OutOfMemoryException when using MediaElement in Silverlight 3

I'm trying to build a simple Silverlight 3 MediaPlayer using the MediaElement tag:
<Border Margin="8,8,0,8" Grid.Column="1" Grid.Row="1" BorderBrush="Black" BorderThickness="1" CornerRadius="7">+
<MediaElement x:Name="FighterPilot" Source="http://localhost/repository/FighterPilot.wmv" Stretch="Fill"/>
</Border>
This works very well for low resolution wmv files. But when a WMV File that has a 720p resolution, i get the build error
Xap packaging failed:
System.OutOfMemoryException
Do anyone have any clue why I get this build error?
Thankful for any help on this one!
Update: The PC I'm developing on has loads of free RAM, so memory shouldn't be a problem.
Assuming that you are using Visual Studio and the WMV file is in your Silverlight project, set the build action on the WMV file to "None" and set "Copy to output directory" to "Copy Always". That way it will not get bundled with the XAP file.
Or you could consider putting the WMV file in a "Video" directory under the ClientBin folder in the website which you can then access using the following:
FighterPilot.Source = new Uri("/Video/FighterPilot.wmv", UriKind.Relative);
No, I've never had that problem. The easy solution would be to not include the wmv file in the Silverlight project. You could add it somewhere in the ASP.NET project instead to avoid it being packaged in the xap all together.

Resources