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);
Related
We have Xamarin Forms solution and in iOS project we are trying to create photo on button click. Problem is image is very dark. It is almost black. Why is this happening? Here is a code:
var _captureSession = new AVCaptureSession();
var _captureDevice = AVCaptureDevice.GetDefaultDevice(AVMediaType.Video);
var _captureDeviceInput = AVCaptureDeviceInput.FromDevice(_captureDevice);
_captureSession.AddInput(_captureDeviceInput);
_captureSession.StartRunning();
private async void OnButtonClick()
{
var output = new AVCaptureStillImageOutput { OutputSettings = new NSDictionary(AVVideo.CodecKey, AVVideo.CodecJPEG) };
_captureSession.AddOutput(output);
var buffer = await output.CaptureStillImageTaskAsync(output.Connections[0]);
NSData data = AVCaptureStillImageOutput.JpegStillToNSData(buffer);
UIImage image = UIImage.LoadFromData(data);
//image = RotateImage(image);
NSData imageData = image.AsPNG();
byte[] byteArray = imageData.ToArray();
IFolder folder = FileSystem.Current.LocalStorage;
IFile file = await folder.CreateFileAsync("image.png", CreationCollisionOption.ReplaceExisting);
using (Stream stream = await file.OpenAsync(PCLStorage.FileAccess.ReadAndWrite))
{
stream.Write(image, 0, image.Length);
}
}
Here is image:
Line:
_captureSession.AddOutput(output);
should go before button click (before StartRunning). With that change image has normal brightness and also rotation is not needed.
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'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.