Get image uri from Source - c#

How do I get the image url? My xaml is:
<toolkit:HubTile Name="Demo" Title="Demo" Source="demo\0.png" Margin="15" Tap="Demo_Tap"/>
and in c# I try to get the url from the sender (because I have a lot of entry's in the same tap)
my c# so far
((Microsoft.Phone.Controls.HubTile)(sender)).Source
and I get errors
what I need rests inside
System.Windows.Media.Imaging.BitmapImage m_string;
but how do I access that?

If Source is a BitmapImage you can just cast the sender source as BitmapImage
(((Microsoft.Phone.Controls.HubTile)(sender)).Source as BitmapImage).UriSource.OriginalString

Source
Source is a dependency property of type ImageSource. It gets or sets the image source of the HubTile control.
Are you casting to an ImageSource when getting the value? Also, it would help if you displayed the error message.
Update
Per your update, casting to a BitmapImage and getting the UriSource would probably get you what you want:
(BitmapImage).UriSource

Related

BitmapImage Failing when trying to change image on a trigger c# UWP

I am trying to dynamically change the page I am on biased off of an event in a UWP application. When the even is triggered I check a simple if statement which passes and I try to set the old back ground image to a new one. I dumbed down my code to where I put the exact url (see image of file structure) in to where the image is located, but I still get an error. Bellow is the error:
Error: System.ArgumentException: 'Error 0x2624. Debugging resource strings are unavailable. See http://go.microsoft.com/fwlink/?linkid=106663&Version=4.0.30319.0&File=mscorrc.dll&Key=0x2624"
Here is The Affected Code:
c# -- MainPage.xmal.cs
BackGroundImage.Source = new BitmapImage(new Uri("/Assets/Images/Financial.png", UriKind.Relative));
XMAL --MainPage.xmal
<Image x:Name="BackGroundImage" Source="Assets/Images/Food.png" />
File Structure:
Whenever I trigger the even that changes the background I get the error and the program crashes. Does anyone have any idea why? This is my first time playing around with BitmapImages and Uri.
Well, please try to change the URL schemes.
Like the following:
BackGroundImage.Source= new BitmapImage(new Uri("ms-appx:///Assets/Images/Financial.png"));
For more details about URL schemes in UWP, please refer to this:URI schemes

Using an SVG as a Ribbon SmallImageSource, with SharpVectors?

I've successfully been able to use a static .svg file as an image in WPF by following the guidance in another question.
The basic approach there is to use the SharpVectors library, and do:
<svgc:SvgViewbox Source="path/to/file.svg"/>
In place of an <Image .../> tag.
However I am struggling trying to find a similar method to use an SVG within a System.Windows.Controls.Ribbon - where i'd like to use it as the SmallImageSource of a RibbonMenuButton.
I have tried the following:
<RibbonMenuButton Label="Test">
<RibbonMenuButton.SmallImageSource>
<svgc:SvgViewbox Source="path/to/file.svg"/>
</RibbonMenuButton.SmallImageSource>
</RibbonMenuButton>
Which produces the compiler error message:
The specified value cannot be assigned. The following type was
expected: "ImageSource".
I think the key problem is that an svgc:SvgViewBox is not an "image source", but I don't know how to properly convert or otherwise work around this.
I'm open to alternate approaches which don't use SharpVectors, but it is extremely convenient to have source image files in SVG format and not have to manually convert to any other format.
SharpVectors includes a converter extension which can be used to 'output' an ImageSource.
This is documented in section "1.2 WPF Extensions and Type Converters" of their usage guidelines.
Example:
<RibbonMenuButton Label="Test" LargeImageSource="{svgc:SvgImage path/to/file.svg}"/>
(where svgc is the defined name of the SharpVectors namespace in your XAML.)
The svgc:SvgImage binding extension produces a DrawingImage which is a type of ImageSource. This works perfectly at runtime with the SVG image rendered into the button.
Unfortunately at design-time the button image is blank.
Please try using the newly added property; AppName, which is used to try and resolve the URI of the resource file at design-time.
See the samples for the SvgImage and newly added SvgImageConverter, especially the toolbar demo using the SVG icons.
https://github.com/ElinamLLC/SharpVectors/tree/master/TutorialSamples/ControlSamplesWpf
SvgImageConverter provides binding support, if you need it unlike the SvgImage.

Umbraco GetCropUrl not giving me a valid URL with Image Cropper

I'm using the image cropper on my media type > Image > Upload Image. This seems to be the best solution for creating responsive images in the media library.
However, this presents me with the problem of finding out how to get the URL for my images now.
Usually for Image Croppers I would use this code:
#Model.Content.GetCropUrl("image", "my-crop-name")
However if I try to get the image crop this way I get this instead:
<img src="Umbraco.Web.Models.DynamicPublishedContent?mode=pad&rnd=131108182860000000" />
I was expecting to get an image URL with the crop I specified. This works fine for standard image croppers but not ones on my images in the media library. Why is that? And how should I get the crop URL for these images?
I'm using v7.4.2
In order to get a crop URL for an image in the media section you first need to retrieve the image as IPublishedContent. You can do this as follows:
var imageId = Model.Content.GetPropertyValue<int>("image");
var image = Umbraco.TypedMedia(imageId);
Or if you're using the excellent Core Property Value Converters package you can retrieve it directly without having to perform the conversion:
var image = Model.Content.GetPropertyValue<IPublishedContent>("image");
You then need to call the GetCropUrl() method on your image. If you've replaced the default Upload property with an Image Cropper property, and kept the property alias the same (umbracoFile), you can just pass in the crop alias:
var cropUrl = image.GetCropUrl("my-crop-name");
If the alias of your Image Cropper property is different to the default you will need to pass that as an additional argument:
var cropUrl = image.GetCropUrl("imageCropperAlias", "my-crop-name");
For images directly from the media libary without an Image Cropper data type. I use somethings like this:
<img src="#photo.Url?mode=crop&width=634&height=634" alt="#photo.Name" />
Not very pretty but it works perfectly.

Find original SOURCE attribute value

I have an application where I'm taking a XAML template from a database record, plugging in new text strings and image references, and showing it in a window.
I take the XAML text and create a DependencyObject using this property:
public DependencyObject ParsedXamlTree
{
get
{
String xaml;
DependencyObject theDependencyObj;
xaml = ProcessedXaml;
byte[] xamlData = Encoding.Unicode.GetBytes(xaml);
using (MemoryStream ms = new MemoryStream(xamlData))
{
theDependencyObj = (DependencyObject)XamlReader.Load(ms);
}
return theDependencyObj;
}
}
Within the XAML will be an Image tag. For example:
<Image x:Name="categoryImage"
HorizontalAlignment="Center"
Height="240"
Margin="72,42,408,318"
VerticalAlignment="Center"
Width="320"
Source="SampleAssets\pacman-151558_640.png"/>
The problem I'm having is that when I parse the DependencyObject tree and get to the corresponding Image object, the string "SampleAssets\pacman-151558_640.png" from the Source attribute seems to be missing.
I notice that when it reads and parses the XAML, there is an IO exception being thrown within WPF itself. That's the line where XamlReader.Load is called. This exception is caught within the library so I'm only seeing evidence of it in the debug trace window.
I'm guessing that it's trying to find "SampleAssets\pacman-151558_640.png" for the Image tag and failing. That's fine in itself, as I always figured the image sources would have to be patched up at runtime.
The question is, how do I retrieve the "SampleAssets\pacman-151558_640.png" string that was in the original XAML? Is there somewhere in the Image object I can get this?
Worst case scenario would be parsing the XAML myself to find the tag and extracting the attribute string from it, but I'm hoping that's not necessary.
WPF has a built-in type converter (ImageSourceConverter) that converts URI strings to ImageSource objects (the type of the Image.Source property). If an image could not be loaded from an URI string, the converter returns null, so the Source property of your Image control will also be null.
There is nothing in the object tree that will keep the URI of a non-available image. You will have to parse the XAML Template before loading and replace invalid image URIs by valid ones.

How to access to Base DataContext property in C# code behind?

I have got a class that gaves me a list of images with two URL addresses as property, one for thumbnail and second for full image. In XAML code of different class I successfully bound to those thumbnail data and showed thumbnails of images. Although now I wanna work with second property in C# code after click on thumbnail and I don't know how to access to DataContext. Thank you for a help.
http://s30.postimg.org/ecx7qepnl/prb2.png
As you can see in debugger DataContext is set to instance of MyPlaces.FlickrImage so all you need to do is get the value of img.DataContext and cast it to your type:
var flickrImage = img.DataContext as MyPlaces.FlickrImage;
if (flickrImage != null)
{
// do something with flickrImage.Image1024
}

Categories