I'm developing a Window Phone 8.1 app. I'm trying to get an image from a Web Map Service and show it in a MapControl. I already get the stream with a http request and now I want to transform it in an Image.
How can I do this part? This is what I have:
Downloading the Stream:
HttpClient httpClient = new HttpClient();
HttpResponseMessage request = await httpClient.GetAsync("<URL>").ConfigureAwait(false);
Stream stream = await request.Content.ReadAsStreamAsync().ConfigureAwait(false);
Now I have the correct Stream from the request (it's a png image). Now I want to transform it in an Image. I already tried to use the FromStream() method but it seems that it's not supported in WP 8.1.
Can you help me please with this last part?
You could try something like this
HttpClient httpClient = new HttpClient();
HttpResponseMessage request = await httpClient.GetAsync("URL").ConfigureAwait(false);
using (Stream stream = await request.Content.ReadAsStreamAsync().ConfigureAwait(false))
{
MemoryStream memStream = new MemoryStream();
await stream.CopyToAsync(memStream);
memStream.Position = 0;
CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(memStream.AsRandomAccessStream());
img.Source = bitmap;
memStream.Dispose();
}
);
}
where img is an Image object defined in XAML.
BTW, a simple
<Image Source="URL" />
in XAML would also probably work for you if all you need is a bitmap as a source for Image.
Related
After downloading xamarin.signaturePad sample i just want to get image from signature pad to memory stream and show on my imageview. here is my code and its working fine on iOS but on android its showing empty stream
var image = await padView.GetImageStreamAsync(SignatureImageFormat.Png);
var stream = new MemoryStream();
image.CopyToAsync(stream);
var imageByteArray= stream.ToArray();
img_result.Source = ImageSource.FromStream(() => newMemoryStream(imageByteArray));
Just cast your imagestream to memorystream. It should be valid
var imageStream = await padView.GetImageStreamAsync(SignatureImageFormat.Png);
// this is actually memory-stream so convertible to it
var mstream = (MemoryStream)imageStream;
//Unfortunately above mstream is not valid until you take it as byte array
mstream = new MemoryStream(mstream.ToArray());
//Now you can
img_result.Source = ImageSource.FromStream(()=>mstream);
I am working on Xamarin.Forms + CocosSharp Application. Here I want to load an image from an URL in cocoassharp using CCSprite. How can I achieve this? Normal CCSprite image is created like: var sprite = new CCSprite("image.png");
It is better to use async for stream and Read. I just did testing in place where that was not convenient but you should use async versions.
var webClient = new HttpClient();
var imageStream = webClient.GetStreamAsync(new Uri("https://xamarin.com/content/images/pages/forms/example-app.png")).Result;
byte[] imageBytes = new byte[imageStream.Length];
int read=0;
do
{
read += imageStream.Read(imageBytes, read, imageBytes.Length- read);
} while (read< imageBytes.Length);
CCTexture2D texture = new CCTexture2D(imageBytes);
var sprite = new CCSprite(texture);
How do I add Tags to a jpeg picture on UWP (universal Windows Platform) / Windows Phone 10.
WindowsBase and PresentationCore not available.
Here is an example about how to write the Artist exif tag to a jpeg image in Windows Runtime.
You can find all of the EXIF tag ids in this web page:
http://www.exiv2.org/tags.html
var src = await KnownFolders
.PicturesLibrary
.GetFileAsync("210644575939381015.jpg");
using (var stream = await src.OpenAsync(FileAccessMode.ReadWrite))
{
var decoder = await BitmapDecoder.CreateAsync(stream);
var encoder = await BitmapEncoder.CreateForTranscodingAsync(stream, decoder);
// var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
var list = new List<KeyValuePair<string, BitmapTypedValue>>();
var artist = new BitmapTypedValue("Hello World", Windows.Foundation.PropertyType.String);
list.Add(new KeyValuePair<string, BitmapTypedValue>("/app1/ifd/exif/{ushort=315}", artist));
await encoder.BitmapProperties.SetPropertiesAsync(list);
await encoder.FlushAsync();
await stream.FlushAsync();
}
I'm attempting to take a photo with my device camera, but images taken with the device held in "portrait" mode come out sideways. I'd like to rotate them before saving them, but the solution that I keep coming across isn't working for me.
Windows.Storage.Streams.InMemoryRandomAccessStream stream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
imagePreview.Source = null;
await stream.WriteAsync(currentImage.AsBuffer());
stream.Seek(0);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(stream, decoder);
encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise90Degrees;
encoder.IsThumbnailGenerated = false;
await encoder.FlushAsync();
//save the image
StorageFolder folder = KnownFolders.SavedPictures;
StorageFile capturefile = await folder.CreateFileAsync("photo_" + DateTime.Now.Ticks.ToString() + ".bmp", CreationCollisionOption.ReplaceExisting);
string captureFileName = capturefile.Name;
//store stream in file
using (var fileStream = await capturefile.OpenStreamForWriteAsync())
{
try
{
//because of using statement stream will be closed automatically after copying finished
await Windows.Storage.Streams.RandomAccessStream.CopyAsync(stream, fileStream.AsOutputStream());
}
catch
{
}
}
this produces the original image with no rotation applied to it. I've looked at a lot of samples, and can't figure out what I'm doing wrong.
I've got the following code which handles downloading and saving an Image to the phone's media library. It fails with a System.UnauthorizedAccessException as if there was some cross-thread access. To my understading all code below an await statement runs on the UI thread so this should not be an issue. In addition I've tried wrapping the code below var stream = await client.OpenReadTaskAsync(this.Url); with Deployment.Current.Dispatcher.BeginInvoke but it did not help. :(
I am running this on WP8 with the intention to port the code later to WP7.
private async void OnSaveImageCommand()
{
RunProgressIndicator(true, "Downloading image...");
var client = new WebClient();
try
{
var stream = await client.OpenReadTaskAsync(this.Url);
var bitmap = new BitmapImage();
bitmap.SetSource(stream);
using (var memoryStream = new MemoryStream())
{
var writeableBitmap = new WriteableBitmap(bitmap);
writeableBitmap.SaveJpeg(memoryStream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0,
100);
memoryStream.SetLength(memoryStream.Position);
memoryStream.Seek(0, SeekOrigin.Begin);
var mediaLibrary = new MediaLibrary();
mediaLibrary.SavePicture("image.jpg", memoryStream);
MessageBox.Show("Image has been saved to the phone's photo album");
}
}
catch
{
MessageBox.Show("Failed to download image");
}
finally
{
RunProgressIndicator(false);
}
}
Did you add an ID_CAP_MEDIALIB_PHOTO capability to your app's manifest?
UnauthorizedAccessException is 99% of the time a missing capability.