How can I load a bitmap into an ImageViewAsync on Xamarin Android Native?
You can use LoadStream method, here you will see how to use this method:
ImageService.Instance
.LoadStream (GetStreamFromImageByte)
.Into (imageView);
Here is the GetStreamFromImageByte:
Task<Stream> GetStreamFromImageByte (CancellationToken ct)
{
//Here you set your bytes[] (image)
byte [] imageInBytes = null;
//Since we need to return a Task<Stream> we will use a TaskCompletionSource>
TaskCompletionSource<Stream> tcs = new TaskCompletionSource<Stream> ();
tcs.TrySetResult (new MemoryStream (imageInBytes));
return tcs.Task;
}
About imageInBytes, you can look here, convert the bitmap to byte[]:
MemoryStream stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
byte[] bitmapData = stream.ToArray();
I have posted my demo on github.
Related
I'm using xamarin SignaturePad.
How to use GetImageAsync properly? because the output is not the same with my input.
I use this code:
var imageStream = await signature.GetImageStreamAsync(SignatureImageFormat.Jpeg);
public static byte[] ConvertStreamToByte(Stream stream)
{
if (stream != null)
{
using (MemoryStream memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
return memoryStream.ToArray();
}
}
return null;
}
Convert Byte[] to Image.
this.SignatureImage = ImageSource.FromStream(() => new MemoryStream(signatureData.Data));
My input on top.
Bottom pic is the retrieved data.
Is it misuse of GetImageAsync or Wrong Conversion? Help.
var imageStream = await signature.GetImageStreamAsync(SignatureImageFormat.Jpeg
, Color.Black, Color.White);
Need to add color for stroke and backround else it will default to black.
I need to load a picture from a web api backend into my Xamarin.Forms app.
The picture is stored in an Azure Blob Storage.
This is my Web Api method:
[HttpGet("{id}")]
public HttpResponseMessage Get(int id)
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("ConnectionString");
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("picturecontainer");
// Retrieve reference to a blob named "photo.jpg".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("picture");
var stream = new MemoryStream();
blockBlob.DownloadToStream(stream);
Image image = Image.FromStream(stream);
MemoryStream memoryStream = new MemoryStream();
image.Save(memoryStream, ImageFormat.Jpeg);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new ByteArrayContent(memoryStream.ToArray());
result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
return result;
}
In my app I try to download the image bytes with the following code:
public App ()
{
_client = new HttpClient();
_client.MaxResponseContentBufferSize = 256000;
Button downloadImageBtn = new Button () {
Text = "Download Image",
};
var image = new Image() {
Source = ImageSource.FromUri (new Uri ("http://www.engraversnetwork.com/files/placeholder.jpg")),
Aspect = Aspect.AspectFit
};
downloadImageBtn.Clicked += async (object sender, EventArgs e) => {
var values = await handleClick (sender, e);
uploadPicButton.Text = values;
var imageBytes = await downloadPicture();
image.Source = ImageSource.FromStream(() => new MemoryStream(imageBytes));
};
MainPage = new ContentPage {
Content = new StackLayout {
VerticalOptions = LayoutOptions.Center,
Children = {
image,
downloadImageBtn
}
}
};
}
private async Task<byte[]> downloadPicture()
{
var uri = new Uri (string.Format (RestUrl, "5"));
//return await _client.GetByteArrayAsync (uri);
var response = await _client.GetAsync (uri);
if (response.IsSuccessStatusCode) {
var content = await response.Content.ReadAsByteArrayAsync ();
return content;
}
throw new HttpRequestException ();
}
However when I click on the button, the placeholder image disappears.
Is there a problem when sending the image from the server or when receiving it in the app?
I'd not implement downloading Images manually.
If you want to load more images and/or allow to display a placeholder while loading, I recommend the FFImageLoading library. It offers nice functionality like downloading, caching, showing placeholder and error images and most important: down sampling the image to the target size. This is always a pain on android. Its available for native UI xamarin projects and for Xamarin.Froms.
You can bind strings that contain urls directly to the Source. The code would like like:
<ffimageloading:CachedImage
Source="http://thecatapi.com/?id=MTQ5MzcyNA">
</ffimageloading:CachedImage>
After blockblob.DownloadToStream line, try to set Position of the stream to 0. I'm not familiar with Azure API, but I think it may help.
Also ,try to use ReadAsStreamAsync instead of ReadAsByteArrayAsync in your downloadPicture function. Something like this:
var responseStream = await response.Content.ReadAsStreamAsync();
byte[] buf = new byte[512];
int bufSize;
while((bufSize = (await responseStream.ReadAsync(buf, 0, buf.Length))) > 0)
{
// store the received bytes to some buffer until the file is fully downloaded
}
Do you get all your content this way?
i have searched all over the internet for this but i haven't really got the answer to this.
Part of the app am creating requires the user to take a photo and this photo is saved to the local database. i tried doing this as below but the method requires me to pass a bitmap (System.Windows.Media.Imaging) and the image control is well Image (System.Windows.Controls.Image)
public byte[] convertToByte(BitmapImage img)
{
using (MemoryStream ms = new MemoryStream())
{
WriteableBitmap btmap = new WriteableBitmap(img.PixelWidth, img.PixelHeight);
Extensions.SaveJpeg(btmap, ms, img.PixelWidth, img.PixelHeight, 0, 100);
ms.Seek(0, SeekOrigin.Begin);
return ms.ToArray();
};
}
the other solution i tried assumes my image is located within the app which is not the case as it is captured by the camera
public byte[] convertToByte(Image img)
{
BitmapImage image = new BitmapImage();
image.CreateOptions = BitmapCreateOptions.None;
image.UriSource = new Uri(img.Source.ToString(), UriKind.Relative);
WriteableBitmap wbmp = new WriteableBitmap(image);
MemoryStream ms = new MemoryStream();
wbmp.SaveJpeg(ms, wbmp.PixelWidth, wbmp.PixelHeight, 0, 100);
return ms.ToArray();
}
How can i redefine either of the methods to save the image? or is there a better way to do this?
Thanks in advance.
So i finally figured the answer to this a while back.
The things i've been through in the pursuit of this!
public Byte[] ImageToByteArray(Image img)
{
try
{
MemoryStream ms = new MemoryStream();
WriteableBitmap bmp = new WriteableBitmap(img.Source as BitmapSource);
bmp.SaveJpeg(ms, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
Byte[] bytImage = ms.GetBuffer();
return bytImage;
}
catch (Exception ex)
{
return null;
}
}
Hope anyone who gets a similar problem gets helped!
I have a class that uses the devices camera to capture an image. My aim is to pass the captured image to a canvas on another layout.
This layout will then be saved along with a note entered into a textbox.I have figured out how to save the note and title and allow it to be opened but I'm not sure how I would go about passing the captured image to the layout and saving it along with the note.
Does anyone have any advice or pointers as to how I would go about this?
At the moment this is how I'm attempting to read the image file back to the layout after it is saved,but I'm not sure how to read a file into the canvas so obviously this solution isn't working yet:
if (NavigationContext.QueryString.ContainsKey("note"))
{
string s2 = ".jpg";
string filename = this.NavigationContext.QueryString["note"];
if (!string.IsNullOrEmpty(filename)) {
using (var store = System.IO.IsolatedStorage.IsolatedStorageFile .GetUserStoreForApplication())
using (var stream = new IsolatedStorageFileStream(filename, FileMode.Open, FileAccess.ReadWrite, store))
/*
if(filename.Contains(s2))
{
StreamReader reader = new StreamReader(stream);
this.capturedNoteCanvas = reader.ReadToEnd();
this.noteNameTb.Text = filename; reader.Close();
}
else
*/
{
StreamReader reader = new StreamReader(stream);
this.noteDataTb.Text = reader.ReadToEnd();
this.noteNameTb.Text = filename; reader.Close();
}
}
}
What I'm thinking is something like this:
Working wit CameraCaptureTask and Bitmaps
//Taking a writableBitmap object from cameracapturetask
void cameracapturetask_Completed(object sender, PhotoResult e)
{
try
{
if (e.TaskResult == TaskResult.OK)
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(e.ChosenPhoto);
WritableBitmap wb=new WritableBitmap (bmp.PixelWidth,bmp.PixelHeight);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
saving wb in storage
using (MemoryStream stream = new MemoryStream())
{
wb.SaveJpeg(stream, (int)bmp.PixelWidth, (int)bmp.PixelHeight, 0, 100);
using (IsolatedStorageFileStream local = new IsolatedStorageFileStream(App.PageName, FileMode.Create, mystorage))
{
local.Write(stream.GetBuffer(), 0, stream.GetBuffer().Length);
}
}
//Taking a WritableBitmap from canvas
If your canvas is containing the image, and also the canvas it attributed with some height and width properties then
WritableBitmap wb= new WritableBitmap(canvascontrol,null);
takes the canvas and saves it inside a writablebitmap object which can then be used for further image manipulations.
I'm trying load image from Picture Hub through this...
void photoChooser_Completed(object sender, PhotoResult e)
{
try
{
var imageVar = new BitmapImage();
imageVar.SetSource(e.ChosenPhoto);
var b = new WriteableBitmap(imageVar.PixelWidth, imageVar.PixelHeight);
b.LoadJpeg(toStream(imageVar));//here comes the exception
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Stream toStream(BitmapImage img)
{
WriteableBitmap bmp = new WriteableBitmap((BitmapSource)img);
using (MemoryStream stream = new MemoryStream())
{
bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
return stream;
}
}
Giving an error occurred when accessing the isolotedstorage. please help !
If I understand correctly, you are trying to:
Get image from a chooser (stream)
Create a bitmap object
Write it to another stream
Create a WriteableBitmap from that second stream
This is seriously convoluted. All you have to do is this:
var imageVar = new BitmapImage();
imageVar.SetSource(e.ChosenPhoto);
var b = new WriteableBitmap(imageVar.PixelWidth, imageVar.PixelHeight);
b.SetSource(e.ChosenPhoto);
This will get you the photo, but have in mind that if you first create a BitmapImage using the SetSource method, it will limit the size of your photo to be under 2000x2000. Then the WriteableBitmap will also be of that smaller, reduced size.
If you wish to create a full sized WriteableBitmap using LoadJpeg method, you need to do this:
//DO SOMETHING TO GET THE PIXEL WIDTH AND PIXEL HEIGHT OF PICTURE BASED JUST ON THE STREAM, FOR EXAMPLE USE EXIF READER: http://igrali.com/2011/11/01/reading-and-displaying-exif-photo-data-on-windows-phone/ OR SEE MORE ABOUT LOADING A LARGE PHOTO HERE: http://igrali.com/2012/01/03/how-to-open-and-work-with-large-photos-on-windows-phone/
var b = new WriteableBitmap(PixelWidth, PixelHeight);
b.LoadJpeg(e.ChosenPhoto);
That will load you the full sized JPEG.
The code you've used looks okay !
void photoChooser_Completed(object sender, PhotoResult e)
{
try
{
var imageVar = new BitmapImage();
imageVar.SetSource(e.ChosenPhoto);
var b = new WriteableBitmap(imageVar.PixelWidth, imageVar.PixelHeight);
b.LoadJpeg(toStream(imageVar));//here comes the exception
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Stream toStream(BitmapImage img)
{
WriteableBitmap bmp = new WriteableBitmap((BitmapSource)img);
using (MemoryStream stream = new MemoryStream())
{
bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
return stream;
}
}
Try reconnecting USB !
You didnot specify, what you want to perform after fetching the Image.
If all you want is to display the image in your app, then you follow this code:
In your try block, simply add this
var imageVar = new BitmapImage();
imageVar.SetSource(e.ChosenPhoto);
Image img = new Image();
img.Source = imageVar;
this.ContentPanel.Children.Add(img);