I am setting the ImageUrl of a StyledStringElement, but do not know if the url exists. I'd like to put in an placeholder image that is used until the image is successfully downloaded:
var item = new StyledStringElement(n.Title);
item.ImageUri = new Uri(n.ImageThumbUrl);
I get this now:
Helpful: http://yusinto.blogspot.ca/2012/05/background-image-downloading-with.html
I copied the StyledStringElement to a new StyledStringElementLoader and edited this code:
if (extraInfo.Uri != null)
{ img = ImageLoader.DefaultRequestImage (extraInfo.Uri, this);
if(img==null)
img=myLoaderImagePassedToConstructor;
}
Related
This is the part of a controller which uploads files in ASP.net Core.
My question is why do we return a value for this case?
string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", imageData.File.FileName);
using (var stream = System.IO.File.Create(path))
{
await imageData.File.CopyToAsync(stream);
}
Image file = await _context.Images.FirstOrDefaultAsync(f => f.FileName == imageData.File.FileName);
if (file == null)
{
Image image = new Image()
{
ContentType = imageData.File.ContentType,
DateModified = DateTime.Now,
FileName = imageData.File.FileName,
Path = path,
Description = imageData.Description
};
_context.Images.Add(image);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetImage), new { id = image.ImageId }, image);
}
That's common for any type of record, simultaneously confirming the creation and allowing you to make changes if required.
I have a small png image I like to show in an imageview using Xamarin.Android.
I am downloading the file using the following code:
private void Download()
{
var url = "https://hns.d7u.de/v4/images/hvvstoerungen_facebook.png";
var directory = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "/myapp/";
var fileName = url.Substring(url.LastIndexOf("/") +1);
var path = directory + fileName;
System.Net.WebClient wC = new System.Net.WebClient();
wC.Headers.Add(System.Net.HttpRequestHeader.AcceptEncoding, "gzip");
wC.DownloadDataCompleted += WC_DownloadDataCompleted;
wC.DownloadDataAsync(new Uri(url), path);
}
private void WC_DownloadDataCompleted(object sender, System.Net.DownloadDataCompletedEventArgs e)
{
var path = e.UserState.ToString();
var bytes = e.Result;
if (File.Exists(path))
File.Delete(path);
if (!File.Exists(path))
File.WriteAllBytes(path, bytes);
}
It is stored at /data/user/0/myapp/files/hns/hvvstoerungen_facebook.png and a File.Exists(...) returns a true for that path. So I am sure, that the file is downloaded and it exists.
When I want to show it in the ImageView, I do it like this:
if (System.IO.File.Exists(imageFilePath))
{
Android.Net.Uri andrUri = Android.Net.Uri.Parse(imageFilePath);
ImageIcon.SetImageURI(andrUri);
//Also not working:
//Bitmap bitmap = BitmapFactory.DecodeFile(imageFilePath);
//ImageIcon.SetImageBitmap(bitmap);
//And also not working:
//Android.Net.Uri andrUri = Android.Net.Uri.Parse(imageFilePath);
//Bitmap bmp = BitmapFactory.DecodeStream(Android.App.Application.Context.ContentResolver.OpenInputStream(andrUri));
//ImageIcon.SetImageBitmap(bmp);
}
The Output windows shows the following when the image should be shown:
02-01 23:41:24.770 E/Drawable(19815): Unable to decode stream:
android.graphics.ImageDecoder$DecodeException: Failed to create image
decoder with message 'unimplemented'Input contained an error. 02-01
23:41:24.770 W/ImageView(19815): resolveUri failed on bad bitmap uri:
/data/user/0/myapp/files/hns/hvvstoerungen_facebook.png
But I cannot figured out what exactly this means.
One additional thing is: If I run the app in a brand new Android Emulator instance, this image and all other of its kind are not shown.
If I run the app in an old Android Emulator instance, where the app was already running before but on Android.Forms basis, the old images that were known by the old project are shown while the newly downloaded images are not. All images are in the same folder and I cannot see any differences between them.
Does anyone has an Idea?
Edit:
My working version has the following Download() Method instead:
private void Download()
{
var noCompression = new string[] { ".png", ".jpg", ".jpeg", ".gif", ".zip", ".7z", ".mp3", ".mp4" };
var url = "https://hns.d7u.de/v4/images/hvvstoerungen_facebook.png";
var directory = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "/myapp/";
var fileName = url.Substring(url.LastIndexOf("/") +1);
var path = directory + fileName;
System.Net.WebClient wC = new System.Net.WebClient();
if (!noCompression.Contains(url.Substring(url.LastIndexOf('.'))))
wC.Headers.Add(System.Net.HttpRequestHeader.AcceptEncoding, "gzip");
wC.DownloadDataCompleted += WC_DownloadDataCompleted;
wC.DownloadDataAsync(new Uri(url), path);
}
You could try the code below.
Download the image from Url:
public Bitmap GetImageBitmapFromUrl(string url)
{
Bitmap imageBitmap = null;
using (var webClient = new WebClient())
{
var imageBytes = webClient.DownloadData(url);
if (imageBytes != null && imageBytes.Length > 0)
{
imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
}
}
return imageBitmap;
}
Usage:
bitmap = GetImageBitmapFromUrl("https://hns.d7u.de/v4/images/hvvstoerungen_facebook.png");
And save the image as png:
void ExportBitmapAsPNG(Bitmap bitmap)
{
var folderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
filePath = System.IO.Path.Combine(folderPath, "test.png");
var stream = new FileStream(filePath, FileMode.Create);
bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
stream.Close();
}
Usage:
ExportBitmapAsPNG(bitmap);
Check the file exists or not and set into the imageview:
if (File.Exists(filePath))
{
Bitmap myBitmap = BitmapFactory.DecodeFile(filePath);
imageview.SetImageBitmap(myBitmap);
}
I can't load image from an ImageCached object on iOS.
I got an asset-library url from the ALAsset but when I create the ImageCached object it d'ont show the image, ending with error that file is not found.
I take the image url from (ALAsset)asset.AssetUrl.AbsoluteString
Here is the bit of code that init my CachedObject
CachedImage image = new CachedImage();
image.Source = url;
image.HeightRequest = heightRequest;
image.WidthRequest = widthRequest;
image.DownsampleWidth = downSample;
image.CacheDuration = TimeSpan.FromDays(30);
image.LoadingPlaceholder = "Images/loading_grey.png";
image.ErrorPlaceholder = "Images/placeholder.png";
image.Transformations = new System.Collections.Generic.List<ITransformation>() {
new CropTransformation(1, image.Width / 2 - heightRequest, image.Height / 2 - widthRequest)
};
image.CacheType = FFImageLoading.Cache.CacheType.Disk;
image.GestureRecognizers.Add(new TapGestureRecognizer
{
Command = new Command(() => {
Navigation.PushModalAsync(new ViewImagePage(url));
}),
NumberOfTapsRequired = 1
});
return image;
From an another posts i got a trail with this when i browse my image
ImagesNames.Add(asset.DefaultRepresentation.Url.AbsoluteString);
But image Path just link to asset.JPG that doesn't point to the file
In a Rails environment, I want to manipulate an image after it has been uploaded. I do not need to change the image, but I want to present it to the user based on parameters in the URL.
Any suggestions?
It means after uploading completed you need to show image, you can show imaged using javascript and jquery, i have done this in sample project like following way
function showThumbnail(files){
for(var i=0;i< files.length;i++){
var file = files[i];
var imageType = /image.*/
if(!file.type.match(imageType)){
alert("Not an image");
}
var image = document.createElement("img");
var thumbnail = document.getElementById("thumbnail");
image.file = file;
if(file.type.match(imageType)) {
if((thumbnail.hasChildNodes() == true)){
thumbnail.removeChild(thumbnail.firstChild);
thumbnail.appendChild(image);
}else
{
thumbnail.appendChild(image);
}
}
var reader = new FileReader()
reader.onload = (function(aImg){
return function(e){
aImg.src = e.target.result;
};
}(image))
var ret = reader.readAsDataURL(file);
var canvas = document.createElement("canvas");
ctx = canvas.getContext("2d");
image.onload= function(){
ctx.drawImage(image,75,75);
$(".uploadedPhoto").addClass('whiteBG');
$(".uploadtxt").html(file.name);
}
}
}
I have taken a photo and saved it, but I need to do image processing on it. I tried using the following code, but WritableBitmap does not accept a bitmap, it needs a stream.
var writeableBitmap = new WritableBitmap(bitmap);
Here is the code:
CameraCaptureUI cam = new CameraCaptureUI();
var capturedImage = await cam.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (capturedImage != null)
{
var img = new BitmapImage();
img.SetSource(await capturedImage.OpenReadAsync());
}
This should do the trick - You need to use the OpenAsync instead.
var dialog = new CameraCaptureUI();
var file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (file != null)
{
var stream = await file.OpenAsync(FileAccessMode.Read);
var img = new BitmapImage();
img.SetSource(stream);
AccountPictureImage.Source = img;
}