Why does XAML image source property contain "/MyApp;component"? - image

I'm developing a Windows Phone app.
I'm using an image and when I select a picture using Properties panel I get the following XAML:
<Image x:Name="GameImage" Margin="8" Source="/MyApp;component/Assets/Icons/GameImage.png"/>
Why am I getting "/MyApp;component/..."? (Is there any better way?)
If I try to do Image.Source="Assets/Icons/GameImage.png" why does it not work?

This is because your image has it's build action set to Resource (Which is the default). If you switch it to Content you can set the source in your XAML like this:
<Image x:Name="GameImage" Margin="8" Source="/Assets/Icons/GameImage.png"/>
To set it in code you can do this:
BitmapImage tn = new BitmapImage();
tn.SetSource(Application.GetResourceStream(new Uri(#"Assets/Icons/GameImage.png", UriKind.Relative)).Stream);
Image.Source = tn;
You should be using Content for performance reasons. See this article for more detail: http://www.windowsphonegeek.com/tips/wp7-working-with-images-content-vs-resource-build-action

Don't forget to add:
using System.Windows.Media.Imaging;
BitmapImage tn = new BitmapImage();
tn.SetSource(Application.GetResourceStream(new Uri(#"Assets/Icons/GameImage.png", UriKind.Relative)).Stream);
Image.Source = tn;

You can use :
BitmapImage obj = new BitmapImage();
obj.UriSource = new Uri(mera_image.BaseUri,file.Path);

Any content marked as an embedded resource is loaded from an assembly. Thus, usage sites need to know specify the assembly a resource is embedded in. In your case, this is MyApp.

Related

Uploading an image on a button click in XMAL

Why this does not load the image?
private void OnButtonClickedLoadImage(object sender, EventArgs e)
{
ImageSource imgSrc =
ImageSource.FromFile("C:\\MyApp\\MyPicture.png");
ImageViewerc.Source = imgSrc;
}
If you want to load local images, in Android, Place images in the Resources/drawable directory with Build Action: AndroidResource. In ios, The preferred way to manage and support images since iOS 9 is to use Asset Catalog Image Sets. Then use Asset Catalog Image Sets. The picture name can get the picture.
Thank you all (including Jason) for your help. Based on everyone's comment above, I corrected my code to properly load my image like this:
ImageSource imgSrc = ImageSource.FromResource("MyApp.pic2.png", typeof(ImageResourceExtension).GetTypeInfo().Assembly);
ImageViewerc.Source = imgSrc;
The image must be accessed like this: AppName.ImageFileName.ext (I was missing the AppName
Also I should note that the character case between the actual file name in Solution Explorer and the code-behind MUST MATCH or image won't load.

How can I set the Source property of a FFImageLoading.SvgCachedImage from a Dynamic Resource in a Xamarin C# view?

Here is is the XAMLcode I am currently using:
<ffimageloadingsvg:SvgCachedImage Source="{DynamicResource GearIcon}" />
GearIcon is set to "Test.Resources.Theme.gear_light.svg" and everything works perfectly. When I set the Dynamic Resource to "Test.Resources.Theme.gear_dark.svg" then the image changes as expected.
Now I am trying to change this to use fluent C# but nothing is working when I do this:
var gear = new SvgCachedImage;
gear.DynamicResource(SourceProperty, SvgImageSource.FromResource(Application.Current.Resources["GearIcon"].ToString()));
I have also tried this:
gear.DynamicResource(SourceProperty, (Application.Current.Resources["GearIcon"].ToString()));
and
gear.DynamicResource(SourceProperty, (ImageSource)(Application.Current.Resources["GearIcon"].ToString()));
Does anyone have any idea how I can set the source dynamically when using C#. Nothing I have tried works so far except the XAML which I am not using right now.
using FFImageLoading.Svg.Forms;
SvgCachedImage gear = this.GetTemplateChild("YOUR SVGCACHEDIMAGE NAME ON YOUR XAML PAGE") as SvgCachedImage;
gear.source = SvgImageSource.FromResource("YOUR ROOT FOLDER NAME.YOUR IMAGE FOLDER NAME.GearIcon.svg");
The type of Source in SvgCachedImage is a ImageSource . So if you want to set it in code behind you could call the following line .
gear.Source = ImageSource.FromFile(Application.Current.Resources["GearIcon"].ToString());

Xamarin.Forms: ImageSource doesn't contain definition for FromResource

I'm following a tutorial but when I use ImageSource.FromResource to put in the source path for the image I get an error:
ImageSource doesn't contain definition for FromResource.
I guess that you using an old version of xamarin.forms, is declared here and you can find a temporarily solution
Edit :
after talking the problem was naming , you have created a class or property with the same name as ImageSource so using Xamarin.Forms.ImageSource instead solve the problem.
Try this
Image kjdb = new Image();
kjdb.Source = ImageSource.FromResource()

How to retrieve an image from WP7 photo library?

I'm developing an application where the user can add photos from windows phone 7 photo library and assign them to a particular view. To do this I save the OriginalFileName on the database (LINQ to SQL). Later I want to recover the photo and load it into the view. Do you know what I can do? currently I have this code but does not work.
When the user has selected the picture I keep his name in the variable fileName:
private void photoChooserTask_Completed (object sender, PhotoResult e)
{
BitmapImage image = new BitmapImage ();
e.OriginalFileName = fileName;
image.SetSource (e.ChosenPhoto);
this.Thumbnail.Source = image;
this.Thumbnail.Stretch = Stretch.UniformToFill;
}
Later, when the user wants to save this setting I save the fileName in database.
This is the code when I load the view that must contain the photo.
imgSource var = new BitmapImage (new Uri (picture.Url, UriKind.Absolute));
item.LeftImage.Source = imgSource;
Where picture.Url contains the filename.
Any idea? I saw on the internet that you can keep the whole image, but give it the best possible.
What you should do is save the picture returned from the PhotoChooserTask in the IsolatedStorage.
You will then be able to load it when needed.
Here is how to Read and Save Images.
For what you need is to get the picture by browsing the MediaLibrary without using PhotoChooserTask, because as you experienced, the file name might not be the same if you use different methods.
For the custom MediaLibrary browsing interface, you could refer to this codeplex project:
https://multiphotochooser.codeplex.com/

Adding Image into Bing map

I've tried adding a image via the following however it is still not working. The image type is a content.
Image image = new Image();
image.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("myimage.png", UriKind.Relative));
//Define the image display properties
image.Opacity = 1.0;
image.Stretch = Stretch.Fill;
image.Width = 40;
image.Height = 40;
// Center the image around the location specified
//Add the image to the defined map layer
phoneDetailsLayer.AddChild(image, e.Position.Location);
mapViewAll.Children.Remove(phoneDetailsLayer);
mapViewAll.Children.Add(phoneDetailsLayer);
Make sure that your image is the correct resource type and is loaded optimally (ie once if being used multiple times). There are multiple approaches to loading images for WPF (same as WP7) which are described here: WPF image resources
This post here: Visual Studio: How to store an image resource as an Embedded Resource? discusses the different image resource types you should/shouldn't use.
I think you should have a look at both as its a good thing to understand, as it can help you to avoid issues in the future that could pop up.
I can't add a comment to your question, however I'll ask here when you say content, have you added the image directly to the project containing your code or to a separate content project?
Assuming that you have added it directly:
If you had set the "Build Action" to "Resource" then you should use the GetResourceStream method:
Image image = new Image();
StreamResourceInfo resource = Application.GetResourceStream(new Uri("/myimage.png", UriKind.Relative));
var bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(resource.Stream);
image.Source = bmp;
However if you have set the "Build Action" to "Content" you should use the GetContentStream method
Image image = new Image();
StreamResourceInfo resource = Application.GetContentStream(new Uri("/myimage.png", UriKind.Relative));
var bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(resource.Stream);
image.Source = bmp;
Just to clarify the answer to this questions. The problem was not in the resource type, the problem was related to the way relative Uri's work. Just like any well structured project ericlee used different folders within his project like this (relative to the project root):
/pages - Contains the actual pages and therefore also the page containing the above code
/images - Contains the actual PNG images that have to be referenced
In the original code a reference is made to "myimage.png" as a relative uri. The app will now look at "/pages/myimage.png" and therefore won't find the image. The trick here is to use the correct relative URI. It can be constructed as follows:
1. First go up to the project root by using two points -> .. (one for the current dir, one extra to go up one level)
2. Now reference /images -> ../images
3. Now add the actual file reference -> ../images/myimage.png
If you use the correct URI the problem is solved.
The main question seems to be how to get true uri.
For me, the following table helps me in this case (I only have it in German):
http://msdn.microsoft.com/de-de/library/aa970069.aspx
example:
// Absolute URI (default)
Uri absoluteUri = new Uri("pack://application:,,,/File.xaml", UriKind.Absolute);
// Relative URI
Uri relativeUri = new Uri("/File.xaml", UriKind.Relative);
example 2:
Uri uri = new Uri("pack://application:,,,/File.xaml");
or Codebehind:
'Image compiling is set to "content"
MyImage1.Source = New BitmapImage(New Uri("/Images/MyFile.png", Relative))'only example
/projectname;component/images/menu/lost.png
Is the correct way, the rest of your answer is really not working

Resources