C# Relative Path to Image Will not Work - c#

I've looked at about a dozen different solutions to get this to work and I just can't seem to execute it properly. I have a file cup.png that I placed in a subfolder called /Images/ located in my project folder under "/Visual Studio 2013\Projects\PointOfSale\PointOfSale\Images\cup.png".
The IDE acts like it can find the file and it shows up in the designer view and everything, and I can compile, but the minute I run it I get a XamlParseException error:
A first chance exception of type
'System.Windows.Markup.XamlParseException' occurred in
PresentationFramework.dll Additional information: 'Provide value on
'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an
exception.' Line number '7' and line position '14'.
What in the world am I missing here? I've tried all different forms of these to try and get this to work:
<ImageBrush ImageSource="pack://application:,,,/PointOfSale;component/cup.png" Opacity="0.1"/>
<ImageBrush ImageSource="/cup.png" Opacity="0.1"/>
<ImageBrush ImageSource="/Images/cup.png" Opacity="0.1"/>
<ImageBrush ImageSource="/PointOfSale;component/Images/cup.png" Opacity="0.1"/>

You have a couple of options (your response to my comment did not answer this directly)
You can include the images as a resource using the BuildAction of Resource.
You can include the images as a piece of content using the BuildAction of Content.
If you choose the first option, then replacing the image would require the entire assembly/executable to be replaced. You can reference the image as "assembly;component/Resources/Images/cup.png" (Note that you need a forward slash at the start of the string).
If you choose the second option, then replacing the image would require you to merely replace the image file. You can reference the image as "/Resources/Images/darkaurora.png" (Note that you need a forward slash at the start of the string).

Related

The XAML Binary Format (XBF) generator reported syntax error '0x09c6' in UWP with Visual Studio

Could someone tell me why this exact error occurs in UWP in Visual Studio? The pattern that I have noticed is when a user control is used inside a data template like this:
<DataTemplate DataType="model:ValueModel">
<local:myControl Value="{x:Bind}"/>
</DataTemplate>
This error comes up in random places and random times during compilation especially if a new blank page is created or if the folder structure is changed.
How do I check the output of the XBF generator and find what is wrong?
Please help!
I had this exact same error, and it turned out to be because I tried to assign an attached property with the wrong base class on my page, like:
<local:NavigablePage
...
xmlns:local="using:MyProject.Views">
<Page.Resources>
...
instead of
<local:NavigablePage
...
xmlns:local="using:MyProject.Views">
<local:NavigablePage.Resources>
...

Set Window background as image from drive on runtime

Images shows in designer but when ran as debug in vs2013 it gives error:
also if exe ran directly with image in same folder.
information: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '12' and line position '10'.
The error is bcoz it cant find the image. also in xaml view when hover the image sorce it says:
project file expected in c:\user\bsienn\docs\vs2013\project\wpf1\wpf1\image1.jpg
though the pic is indeed in that path and is available.
I want to add an image as background for form, I don't want to add the image in resource, bcoz i want the image to be changed when needed. I have placed the image with exe file also tried to place it in bin/debug and main application folder (wpf1/image1.jpg andalso wpf1/wpf1/image1.jpg).
here is the xaml code, please guide
<Window.Background>
<ImageBrush ImageSource="image1.jpg"/>
</Window.Background>
App structure:
app.exe
image1.jpg
Desired outcome, form with background image
This will do as desired
XAML:
<Window.Background>
<ImageBrush x:Name="MainFormBgrImg"/>
</Window.Background>
code behind:
BitmapImage bitimg = new BitmapImage();
bitimg.BeginInit();
bitimg.UriSource = new Uri(#""+AppDomain.CurrentDomain.BaseDirectory+"backgroundImg.jpg", UriKind.RelativeOrAbsolute);
bitimg.EndInit();
MainFormBgrImg.ImageSource = bitimg;
AppDomain.CurrentDomain.BaseDirectory:
Returns current working directory from where where app ran from, i.e c:\users\admin\Desktop\
Putting image in output folder won't make it available to your XAML.
You need to add image in your project and set it's Build Action to Resource.
Right click on added image in project -> Open Properties -> Set Build Action to Resource.

Hyperlink button click in WinPhone 7 Emulator gives error - "Can't Send, Make sure you have setup an account"

I have a C# project for Windows Phone 7 that I created with Expression Blend and Visual Studio 2012. I have an About page with a Hyperlink button that takes the user to the support web page for the app. When I click the button I get a screen in the Emulator saying:
"Can't Send. Make sure you have set up an account."
This ends up triggering a NULL argument exception in System.Windows.ni.dll in the VS2012 debugger. There is no View Exception Details option visible. I have triple-checked the NavigateUri value and I know it is a valid web page. I tried "http://google.com/" just to be sure and I still get the error so it's a more fundamental problem than the URL. Why am I getting this error and how can I fix it?
UPDATE: I am showing the XAML for the button in response to Den's reply. I did not enter the XAML manually, I used the Property Editor window. I believe the problem is that it is being treated as an E-mail command and I have not set up an E-mail service yet.
Here is the XAML for the HyperLink button:
<HyperlinkButton
Grid.Row="3"
Content="Technical Support/Feedback Email"
Margin="0,0,0,23"
HorizontalAlignment="Left"
Foreground="{StaticResource PhoneAccentBrush}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Command="{Binding SendAnEmailCommand}" Width="446" NavigateUri="http://google.com/"/>
However, after removing the Command binding I now get an Exception from the underlying Navigate call the hyperlink button triggers. I get an Argument Exception with the following error message:
System.ArgumentException: Navigation is only supported to relative URIs that are fragments, or begin with '/', or which contain ';component/'. Parameter name: uri
at System.Windows.Navigation.NavigationService.Navigate(Uri source)}
System.Exception
{System.ArgumentException}
It seems like the Hyperlink button isn't meant for external URIs? This seems really strange to me.
Seems like you're doing something wrong when declaring the HyperlinkButton. Here is what it should be like:
<HyperlinkButton TargetName="_blank" NavigateUri="http://www.microsoft.com" Content="Microsoft"/>
Or hey why dont you try making the urikind property to absolute? Test with this and tell me.

Image not loading correctly from BitmapImage in code behind

Okay, I'm attempting to load an image to an Image control on my page. Now as far as I know, I know how to do this (the method I'm using has worked before). But for some reason it's not working this time. The image is not being loaded.
Okay, let's start with the XAML. You can see it here.. Please note my excellent artistic skills. You can see that the top image is the left one on the screen and the bottom image is the right one. As you can also see, the image on the right is loaded in the XAML from a file in the Assets/Images folder. It should also be noted that we can take from this that the image loads okay - there aren't errors with the image. We can also see that the images are not blocked by anything (when the program runs, the right image shows just fine).
So the left image, 'image1' is the one that I'm loading from the code behind. Here is the code behind for that page. As you can see, there's not a lot. Yes I do use a view model, but it doesn't interface with the image at all (its only exposed properties are an Entity (for a selected item) and an ObservableCollection of entities (for the ItemsSource of a control)). As you can also see, I'm attempting to load the same image as is used for the static image (so I can say for sure that the image is okay for Silverlight).
If you're wondering about the BitmapCreateOptions line, that was something that someone suggested to me to add. I have tried removing it, but that doesn't seem to cause any difference.
The next question is - have I attempted to use the event handler for the failure? Yes I have. The error I get is 'AG_E_NETWORK_ERROR'.
I'm not sure what exactly this is about - I'm not loading from a network.
Can anyone offer assistance? Thanks.
I can't help but notice that in your XAML, you have a "../" at the start of the image path, but this is missing from the path you're using in code-behind. Could this be the problem?
Have You tried to call BeginInit and EndInit before and after setting the uri?
BitmapImage bmpImg = new BitmagImage();
bmpImg.BeginInit();
bmpImg.UriSource = new Uri(...);
bmpImg.EndInit();
Or use the constructor that takes an Uri. msdn

How to properly use image resource in WPF application

What is the best way to store static images (like toolbox icons) in a WPF app?
Right now I have Images directory and use them like this:
<dxb:BarButtonItem x:Name="rbSignOut" Content="Sign out" Glyph="Images/Icons/close-16x16.png" LargeGlyph="Images/Icons/close-32x32.png" />
I think it's not the best practice because when I move XAML file in to a subfolder, I need to change all paths. Also, it just does not seems right to store paths in code. So how do I do it properly?
Instead of your path, which is relative to the XAML file:
Glyph="Images/Icons/close-16x16.png"
Use a path which is relevant to the application root (using a leading forward slash)
Glyph="/Images/Icons/close-16x16.png"
No matter where your XAML file is, your image will always be referenced from the root. As long as you don't move your images, you'll always be fine.
Use a ResourceDictionary together with your images. You can add it to the generic.xaml ResourceDictionary so you'll only have to change one path if you move it and you can use the images in every xaml file.
Just Right-Click and change your image's "Build action" to "Content" and use like this
<Image Source="/WPFApplication1;component/Images/Image.png" />
I felt it as the best approach to use Images,Video etc as the Files are not embedded into the assembly.

Categories

Resources