I am having the stream and I trying to set the Image Source as a stream. My code is
var contentPage = new ContentPage();
var image = new Image();
Stream stream = args.Stream;
stream.Position = 0;
image.Source = ImageSource.FromStream(() => stream);
contentPage.Content = image;
Navigation.PushModalAsync(contentPage);
Image rendered in android and iOS but it not worked in UWP. Please suggest me, how to render the image via stream.
Thanks,
Santhiya A
I experienced this also in UWP and have found that you should do this work in the OnAppearing() method of the contentPage.
Related
I save images relative to the workingdirectory and then want to display them in a uno Skia.WPF app.
However they never appear.
This is how I create the ImageSource:
public static async Task<ImageSource> GenerateSource()
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.FileTypeFilter.Add(".png");
var pickerResult = await picker.PickSingleFileAsync("tileset");
if (pickerResult is null)
return null;
using var stream = await pickerResult.OpenReadAsync();
string imagePath = Path.GetFullPath(Path.Combine("cache", "image.png"));
Directory.CreateDirectory(Path.GetDirectoryName(imagePath));
using (var bitmapImageStream = File.Open(imagePath,
FileMode.Create,
FileAccess.Write,
FileShare.None))
{
await stream.AsStreamForRead().CopyToAsync(bitmapImageStream);
bitmapImageStream.Flush(true);
}
var imgSrc = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
imgSrc.UriSource = new Uri($"file://{imagePath}");
var width = imgSrc.PixelHeight;
return imgSrc;
}
But when I use the path directly in the Xaml it also not working.
<Image Source="file://cache/image.png" Width="32" Height="32" />
Using an image from the Internet using an http url works.
Sample Repo
file: URIs are currently not supported in Uno, however, you can still show local images by copying to the appropriate folder using ms-appdata scheme. If you copy the file into ApplicationData.Current.TemporaryFolder, you will the be able to reference that path by ms-appdata:///temp/{imageName} URI.
I used the Xamarin(Photo Picker) example and was able to output the image using this code
image.Source = ImageSource.FromStream(() => stream);
How do I convert a stream of images to a file.Path?
I want to save the path to the image to a local database.
Yes,it is recommended that you use Xamarin.Essentials: Media Picker to achieve this.
You can get the FullPath of your image except the stream.
Please refer the following code:
async void Button_Clicked(System.Object sender, System.EventArgs e)
{
var result = await MediaPicker.PickPhotoAsync(new MediaPickerOptions
{
Title = "Please pick a photo"
});
if (result != null)
{
var stream = await result.OpenReadAsync();
resultImage.Source = ImageSource.FromStream(() => stream);
// you can get the FullPath of current photo
string path = result.FullPath;
}
}
If you have an Image in your UI, it can have different types of Sources. It can be from a File, a URL or a Stream.
A stream is in memory only, so if you want a path to a file, you will have to convert it to a file first.
using (System.IO.FileStream fileStream = System.IO.File.Create(filePath))
{
stream.CopyTo(fileStream);
}
And then you can get the path to your file.
You can save the file wherever you want, I recommend using Xamarin.Essentials FileSystem Helper
I am using this sharing function:
public static async void ShareImageAndText(string text, string image)
{
var fn = "pic.png";
var file = Path.Combine(FileSystem.CacheDirectory, fn);
File.WriteAllBytes(file, Convert.FromBase64String(image));
await Share.RequestAsync(new ShareFileRequest()
{
Title = text,
File = new ShareFile(file)
});
}
This shares an image to whereever I please, but the text "title" only appears if I share to email. If I share to whatsapp for instance, it will only give the image. But since I also want to share a text with an uri in it, this option doesnt work.
Who knows how to share a file AND a text in the same request?
Thanks
Having the same problem, I was unable of resolving it using the Xamaring.Essentials.Share. So, I created in my Xamarin Forms App a Page with the Image and Text that I need to send, and take a screenshot of it using Screenshot https://learn.microsoft.com/en-us/xamarin/essentials/screenshot and saving it to File. Then just send it.
//Take a screenshot from this page to a file
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var File_Path = System.IO.Path.Combine(path, "Screenshot.png");
var screenshot = await Screenshot.CaptureAsync();
var stream = await screenshot.OpenReadAsync(ScreenshotFormat.Png);
using (var fileStream = System.IO.File.Create(File_Path))
{
stream.Seek(0, System.IO.SeekOrigin.Begin);
stream.CopyTo(fileStream);
}
await Share.RequestAsync(new ShareFileRequest
{
Title = "Some Title",
File = new ShareFile(File_Path)
});
I am sending image Source to other page like
string bbb = dtnhd.imgsource.ToString();
Other page I got this:
"Xamarin.Forms.StreamImageSource"
How I can change to "Xamarin.Forms.StreamImageSource" to image source.
I am using
byte[] byteArray = Encoding.UTF8.GetBytes(imagepath1);
Stream stream = new MemoryStream(byteArray);
var imageSource = ImageSource.FromStream(() => stream);
I am working on accessing RSS feed using Flex mobile project. But i dont get image in that RSS feed,how to get image from RSS feed in Flex mobile project and how to convert an image into byte stream in Flex mobile project
But i dont get image in that RSS feed
RSS is xml. You need to looking for image elements, get url and load images separately
http://www.w3schools.com/rss/rss_tag_image.asp
how to convert an image into byte stream
In order to load image as ByteArray you need to use URLLoader for image loading:
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, loader_completeHandler);
function loader_completeHandler(event:Event):void
{
//here you can get loaded image as ByteArray
var imageData:ByteArray = loader.data;
}
If after this you need to show image in display list then:
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, loader_completeHandler);
var imageBytesLoader:Loader = new Loader();
imageBytesLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageBytesLoader_completeHandler);
function loader_completeHandler(event:Event):void
{
//here you can get loaded image as ByteArray
var imageData:ByteArray = loader.data;
imageBytesLoader.loadBytes(imageData);
}
function imageBytesLoader_completeHandler(event:Event):void
{
//here you can get loaded image as Bitmap
var bitmap:Bitmap = Bitmap(imageBytesLoader.content);
}