How to get image location to Label or Entry in Xamarin - c#

in .xaml
<Image x:Name="image">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="GetImageLocationFromExternalCard"/>
</Image.GestureRecognizers>
</Image>
<Label x:Name="fileLocation"/>
in .xaml.cs
private void GetImageLocationFromExternalCard(object sender, EventArgs e)
{
// what can i write is here
image.Source = fileLocation.Text;
}
I want to make is like pick an image file from memory card and send as string location to label text.

use MediaPicker
var photo = await MediaPicker.PickPhotoAsync();
image.Source = photo.FullPath;

Related

Downloading and Displaying pdf files from internet

I have a UWP that allows me to display a pdf from a website url. It's also able to display a pdf from the project folder.
However, I am trying to display a picture placed in the local app folder with a button.
I have tried searching through google and found no workable solution. Does anyone have any suggestions to help?
You can refer to the sample in the official documentation Image Class.
Setting Image.Source.
Setting Image.Source using code.
Or use FileOpenPicker to select a picture in a local folder.
Page.xaml
<Image x:Name="image"></Image>
<Button Content="Button" Click="Button_Click"/>
Page.xaml.cs
private async void Button_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
picker.FileTypeFilter.Add(".bmp");
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
var file = await picker.PickSingleFileAsync();
if (file != null)
{
IRandomAccessStream ir = await file.OpenAsync(FileAccessMode.Read);
BitmapImage bi = new BitmapImage();
await bi.SetSourceAsync(ir);
image.Source = bi;
}
}
UPDATE
Put your image path in Source.
Page.xaml
<Image x:Name="image" Width="200" Source="Assets/StoreLogo.png" Visibility="Collapsed"></Image>
<Button Content="Button" Click="Button_Click"/>
Page.xaml.cs
private void Button_Click(object sender, RoutedEventArgs e)
{
if (image.Visibility == Visibility.Collapsed)
{
image.Visibility = Visibility.Visible;
}
else
{
image.Visibility = Visibility.Collapsed;
}
}

Tap on a image inside the ListView, we should get a property value of specific Row

I have a listView, it contains an image with a list of items. When I tap on the image, I should get ProfileID of that row.
<Image Source="{Binding ImageUrl}" x:Name="{Binding ProfileID}" Aspect="AspectFill">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="imageUserGesture_Tapped"/>
</Image.GestureRecognizers>
</Image>
private async void imageUserGesture_Tapped(object sender, EventArgs e)
{
CloseAnimation();
var img = ((Image)sender);
var name = img.Name;//How can I read name property ??????
//var name = e.LoadFromXaml(MatchProfile).Name;
}
Name is not an actual property you can reference, it is just a XAML artifact (hence the "x" prefix). Instead, you can use the BindingContext to get the data you need
var img = ((Image)sender);
var context = (MyClassGoesHere)img.BindingContext;
var name = context.ProfileID;
<Image Source="{Binding ImageUrl}"
x:Name="imageUser"
Aspect="AspectFill">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="imageUserGesture_Tapped"
Command="{Binding TapCommand}"
CommandParameter="{Binding ProfileID}"
NumberOfTapsRequired="1"/>
</Image.GestureRecognizers>
</Image>

How to select an image from gallery in xamarin forms?

I'm trying to pick an image from gallery and i've done everything that is in this doc, but i'm getting an error with the "image" object in my .cs implementation.
My .cs code:
public partial class MyProfilePage : ContentPage
{
public MyProfilePage()
{
InitializeComponent();
BindingContext = new MyProfileViewModel();
}
async void ImageButton_Clicked(object sender, EventArgs e)
{
//(sender as Button).IsEnabled = true;
Stream stream = await DependencyService.Get<IPhotoPickerService>().GetImageStreamAsync();
if (stream != null)
{
image = ImageSource.FromStream(() => stream);
}
(sender as Button).IsEnabled = true;
}
}
Welcome to SO!
but i'm getting an error with the "image" object in my .cs implementation.
You can check the Xaml file that whether contain the x:Name="image".
Such as follows:
<ContentPage Title="Photo Picker"
Icon="monkeyicon.png">
<StackLayout Margin="20,35,20,20" >
<Label Text="Photo Picker"
FontAttributes="Bold"
HorizontalOptions="Center" />
<Button Text="Pick Photo"
Clicked="OnPickPhotoButtonClicked" />
<Image x:Name="image" />
</StackLayout>
</ContentPage>
If not works , you can refer to following steps:
First, you need to share the detail error logs here, and explain this occurs in iOS or
Android.
Second, you also can refer to this official sample Xamarin.Forms - Dependency Service to check where problem is.

Get url from image source and launch it (UWP)

I have UWP app where I have Image with url source.
Here is xaml code:
<Image x:Name="Image" HorizontalAlignment="Left" Height="200" Width="200" Tapped="Image_Tapped">
<Image.Source>
<BitmapImage UriSource="{Binding data.thumbnail}" />
</Image.Source>
</Image>
I created Tapped event handler
Here is code
private void Image_Tapped(object sender, TappedRoutedEventArgs e)
{
var source = Image.SourceProperty.ToString();
Debug.WriteLine(source);
}
But it seems not right.
How I can get ImageSource and launch this url(Image source is url) in browser?
You need the Launcher class.
private async void Image_Tapped(object sender, TappedRoutedEventArgs e)
{
if (((Image)sender).Source is BitmapImage bitmapImage)
{
var uri = bitmapImage.UriSource;
// Launch the URI
var success = await Windows.System.Launcher.LaunchUriAsync(uri);
if (success)
{
// URI launched
}
else
{
// URI launch failed
}
}
}
Also note since you might want to decode the image to render size since you have already specified the image size (i.e. 200x200) to save a bit of memory. You don't have to do this if you are using Image.Source directly.
<BitmapImage DecodePixelWidth="200" DecodePixelHeight="200" ... />

get the source of image when user clicks it in c#

i have included four photos in xaml code as follows
<Image Grid.Column="0"
Source="Assets/1.png"
Name="m1"
MouseLeftButtonDown="selected"/>
<Image Grid.Column="1"
Source="Assets/2.png"
Name="m2"
MouseLeftButtonDown="selected"/>
<Image Grid.Column="2"
Source="Assets/3.png"
Name="m3"
MouseLeftButtonDown="selected"/>
<Image Grid.Column="3"
Source="Assets/4.png"
Name="m4"
MouseLeftButtonDown="selected"/>
i want to get the source of the image in "selected" function.
my selected function is as follows
private void selected(object sender, MouseButtonEventArgs e)
{
//do somethings....
}
How can i assign the source of the selected image(sender) to a new Image object?.
something similar to follows
Image newimage = new Image();
newimage.Source = //something..
Is there a way to dynamically get the source?
Cast your sender as an image and you will be able to use the Source property:
private void selected(object sender, MouseButtonEventArgs e)
{
Image newimage = new Image();
newimage.Source = ((Image)sender).Source;
}
Use OriginalSource property of event and cast it to Image:
var clickedImage = (Image)e.OriginalSource;
Image newimage = new Image();
newimage.Source = clickedImage.Source;

Categories

Resources