Xamarin Camera Control Photo does not show on device - xamarin

I used the following example to work with Camera Control in Xamarin
Example used: adamped/CameraXF
Following piece of code works fine in emulator. On device, it takes up the space of an image but image doesn't load. Any leads?
private async void CameraButton_Clicked(object sender, EventArgs e)
{
var photo = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions() { });
if (photo != null)
PhotoImage.Source = ImageSource.FromStream(() => { return photo.GetStream(); });
}

I would recommend you to use the CachedImage class from FFImageLoading. I have encountered this problem in lower-end devices and using CachedImage fixes it.
Here is the repo : https://github.com/luberda-molinet/FFImageLoading
Docs : https://github.com/luberda-molinet/FFImageLoading/wiki/Xamarin.Forms-API
Here is the xaml sample.
<ffimageloading:CachedImage HorizontalOptions="Center" VerticalOptions="Center"
WidthRequest="300" HeightRequest="300"
DownsampleToViewSize="true"
Source = "http://loremflickr.com/600/600/nature?filename=simple.jpg">
</ffimageloading:CachedImage>
Dont forget to set the DownsampleToViewSize="true".
This should solve your issue.

Related

Problem with xamarin forms image when removing and adding it from parent layout

Description
I have created simple Xamarin.Forms sample. In it, I have added an Image and loaded the image as a stream through ImageSource.FromSource() method, then removed and add it to the layout.
Xamarin.Forms UWP The image will be disappeared.
Xamarin.Forms Android Cannot access closed stream exception throwing.
Xamarin.Forms iOS Didn't check yet.
Please find the code snippet below
XAML:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ImageException.MainPage">
<ContentPage.Content>
<Grid x:Name="MainGrid">
<Image x:Name="image" />
<Button Text="Click_Me"
Clicked="Button_Clicked"
VerticalOptions="End" />
</Grid>
</ContentPage.Content>
</ContentPage>
C#:
namespace ImageException
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
var assembly = typeof(MainPage).Assembly;
string path = "ImageException";
var stream = assembly.GetManifestResourceStream($"{path}.Image.Image.png");
image.Source = ImageSource.FromStream(() => stream);
}
private void Button_Clicked(object sender, EventArgs e)
{
MainGrid.Children.Remove(image);
MainGrid.Children.Add(image);
}
}
}
Please get the complete sample from below link https://github.com/VigneshRameshh/XamarinFormsImageException
Can anyone suggest how to retain the image after removed and added from the layout?
Thanks in advance,
Vignesh Ramesh.
I checked your code and could reproduce your exception.
If you want to fix this, you could use the code like below.
Stream stream;
Assembly assembly;
string path;
public Page18()
{
InitializeComponent();
assembly = typeof(MainPage).Assembly;
path = "App14";
stream = assembly.GetManifestResourceStream($"{path}.Image.beach.jpg");
image.Source = ImageSource.FromStream(() => stream);
}
private void Button_Clicked(object sender, EventArgs e)
{
stream = assembly.GetManifestResourceStream($"{path}.Image.beach.jpg");
MainGrid.Children.Remove(image);
MainGrid.Children.Add(image);
}

Image From Music Library Not Showing in UWP app

I'm new to UWP development & I'm just showing image in my app from the Music Library.
Infact, I have added Music Library in the app's "Capabilities" & I can confirm that I have access to Music Library as I can read & write files in it.
But when I try to load a image in XAML, it just does not shows...
<Image Height="200" Width="200" Source="C:/Users/Alex Mercer/Music/Album/albumArt.png" />
Please help me understand & solve the problem.
😃 Thanks a lot!
Although we enable the corresponding capabilities, accessing files through paths is still strictly restricted in UWP.
In fact, it is not a good idea to write the full path in XAML, because you cannot guarantee that the path must exist on the device where the application is installed.
To display the pictures in the music library, you can do this:
xaml
<Image Height="200" Width="200" x:Name="AlbumImage" Loaded="AlbumImage_Loaded"/>
xaml.cs
private async void AlbumImage_Loaded(object sender, RoutedEventArgs e)
{
try
{
var albumFolder = await KnownFolders.MusicLibrary.GetFolderAsync("Album");
var albumPic = await albumFolder.GetFileAsync("albumArt.png");
var bitmap = new BitmapImage();
using (var stream = await albumPic.OpenAsync(FileAccessMode.Read))
{
await bitmap.SetSourceAsync(stream);
}
AlbumImage.Source = bitmap;
}
catch (FileNotFoundException)
{
// File or Folder not found
}
catch (Exception)
{
throw;
}
}

After Creating Package in Xamarin UWP, Video is Playing Only With Voice, I am Not able to see Video

I am using MediaManager Plugin with latest version to play a video. everything works fine when i run application in debug mode, but when i create a package for window, video is not showing only voice is heard.
I am using below Package
Plugin.MediaManager.Forms
This is My XAML page
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Digi_Sign.Views.MediaScreen"
xmlns:forms="clr-namespace:MediaManager.Forms;assembly=MediaManager.Forms"
BackgroundColor="Black">
<ContentPage.Content>
<Grid x:Name="stkLayout" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" BackgroundColor="Black">
<forms:VideoView VideoAspect="AspectFill" x:Name="videoPlayer" ShowControls="False" />
</Grid>
</ContentPage.Content>
</ContentPage>
CS Page
CrossMediaManager.Current.Play(fileName);
No Errors Found in package error log, as i mentioned everything works fine when it is debug mode, but not working in release mode
Basically there is no way to initialize renderer for video in native code for xamrin.UWP, so we need to manually load render assembly in initialize it in App.xaml.cs of UWP platform
Below is my code where i have load assembly files inside OnLaunched() and replace existing Xamarin.Forms.Forms.Init()
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
Windows.UI.Xaml.Controls.Frame rootFrame = Window.Current.Content as Windows.UI.Xaml.Controls.Frame;
if (rootFrame == null)
{
rootFrame = new Windows.UI.Xaml.Controls.Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
List<Assembly> assembliesToAdd = new List<Assembly>();
assembliesToAdd.Add(typeof(VideoViewRenderer).GetTypeInfo().Assembly);
Xamarin.Forms.Forms.Init(e, assembliesToAdd);
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
Window.Current.Activate();
}
for more details, here is the link. where you can find more explaination.
https://forums.xamarin.com/discussion/119451/crossmedia-works-in-debug-but-not-in-release-for-uwp-error-msg-about-onecore
Most likely from the behavior you are describing (and looking at your code), it sounds like it's because the Video is not being run on the UI thread, and this causes the app to play the audio but not the video. So change it to the following:
Device.BeginInvokeOnMainThread(() => { CrossMediaManager.Current.Play(fileName); });

Silverlight: ImageTools.ExtendedImage Loading External URL Image NOT Working at all

I'm using the ImageTools library (imagetools.codeplex.com) to load an image from external URL.
<Canvas x:Name="LayoutRoot" Background="Blue"
Width="466" Height="204" >
<Image Name="theImage" />
<Button x:Name="btnTest" Canvas.Top="0" Canvas.Left="-200" Click="btnTest_Click"
Width="100" Height="23"
Content="Test Button">
</Button>
</Canvas>
Init:
public MainPage()
{
InitializeComponent();
Encoders.AddEncoder<PngEncoder>();
Decoders.AddDecoder<PngDecoder>();
Encoders.AddEncoder<JpegEncoder>();
Decoders.AddDecoder<JpegDecoder>();
}
Then:
private void btnTest_Click(object sender, RoutedEventArgs e)
{
ExtendedImage ei = new ExtendedImage();
// ei.UriSource = new Uri(#"https://www.google.com/images/srpr/logo3w.png"); // NOT working
ei.UriSource = new Uri(#"/Images/header.png", UriKind.Relative); // Working
ei.LoadingCompleted += new EventHandler((ss, ee) =>
{
Dispatcher.BeginInvoke(() =>
{
theImage.Source = ei.ToBitmap();
});
});
}
I found that loading a local file /Image/header.png is working, but loading an external URL image (https://www.google.com/images/srpr/logo3w.png) is not working at all.
It behaves crazy: once I click the Test Button, the LayoutRoot canvas disappears.
However, according to this discussion: http://imagetools.codeplex.com/discussions/250624
Loading an external URL image should be working.
Can it be related with UriType ?
Relative,Absolute,RelativeOrAbsolute
ei.UriSource =
new Uri(#"https://www.google.com/images/srpr/logo3w.png"
,UriKind.RelativeOrAbsolute); // works ?
http://msdn.microsoft.com/en-us/library/system.urikind(v=vs.95).aspx
Hope helps!
EDIT: You need a file like that http://twitter.com/crossdomain.xml otherwise SL throws SecurityException normally.
For crossdomain implementation see
https://stackoverflow.com/a/1325011/413032
http://www.silverlighthack.com/post/2008/11/08/Silverlight-clientaccesspolicyxml-files-for-the-Enterprise-(Part-1-of-2).aspx

How to create a small Windows Phone 7 app with click event

I am a newbie in windows phone app and need to create a small Windows Phone 7 app. The app will perform the following task
App Screen has an image "image1' ,when i press on 'image1' it will shows a second image 'image2'
When I press on image2 it will shows image1 and so on
My XAML code
<Button Click="Button_Click">
<Image Source="resourse/image1.jpg"/>
</Button>
C# code
namespace Test
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// here will shows the 'image2' and also give click event to turn 'image1'
}
}
}
Please help
I would tackle this by having a couple of images in your XAML:
<Button Click="Button_Click">
<Grid>
<Image x:Name="imageOne" Source="resourse/image1.jpg"/>
<Image x:Name="imageTwo" Source="resourse/image2.jpg"
Visibility="Collapsed"/>
</Grid>
</Button>
The use of x:Name causes visual studio to generate a field for each image. The second image is 'collapsed', i.e. hidden.
You click handler then does the following:
private void Button_Click(object sender, RoutedEventArgs e)
{
if (imageOne.Visisbility == Visibility.Visible)
{
imageOne.Visisbility = Visibility.Collapsed
imageTwo.Visisbility = Visibility.Visible
}
else
{
imageOne.Visisbility = Visibility.Visible
imageTwo.Visisbility = Visibility.Collapsed
}
}
On each click it toggled the visibility of each image.
This is easier than changing the Source of the image, which involves URIs etc...

Resources