I have a series of images stored online which I try to reach programmatically in my Universal Windows Phone App.
Is there a way to find out if the Image exists for the selected parameter and, if not, use an Image placeholder instead?
var img = new BitmapImage(new Uri("url?ID_IMMAGINE=" + idImg1 + "&HEIGHT=100", UriKind.Absolute))
this is how I get the image.
You could attach a handler for the ImageFailed event to set a fallback value for the URI:
var defaultImageUri = new Uri("ms-appx:///Assets/DefaultImage.png");
var bitmap = new BitmapImage();
bitmap.ImageFailed += (s, e) => bitmap.UriSource = defaultImageUri;
bitmap.UriSource = new Uri(...);
You cant use async for Converter. return type of async method must be void,Task,Task<T>. That cant be given to Convert method
try
{
var img = new BitmapImage(new Uri("url?ID_IMMAGINE=" + idImg1 + "&HEIGHT=100", UriKind.Absolute));
if (img == null)
{
img = new BitmapImage(new Uri("defaultImage.png",UriKind.RelativeOrAbsolute));
}
}
catch
{
img = new BitmapImage(new Uri("defaultImage.png", UriKind.RelativeOrAbsolute));
}
If you really want to use converter please go through this Stackoverflow link
Related
In the viewmodel of my page, I try to get an image to show. This image I get from one of our webservice. The image source is stored in ProductImage
string path = $#"http://www.MyCompany.be/cdn-cgi/image/width=150,quality=75/images/products/{CurrentProduct.Image}".Replace($"\\", $"/");
ProductImage = ImageSource.FromUri(new Uri(path));
I of course make sure when I update the Image source for the view when I change it
public ImageSource ProductImage
{
get
{
return _productImage;
}
set
{
if (_productImage != value)
{
_productImage = value;
OnPropertyChanged();
}
}
}
Sadly, for some reason it doesn't work. I checked the URL called and it does lead to an image.
It used to work when I was using streams instead of calling an URL. I know I haven't put the safety checks around yet to make sure the image exist, but other than that nothing has changed. I save the image the exact same way, i just use fromUri rather than fromStream.
//Get target's SmbFile.
var file = new SmbFile(path, auth);
try
{
if (file.Exists())
{
// Get readable stream.
var readStream = file.GetInputStream();
//Create reading buffer.
MemoryStream memStream = new MemoryStream();
//Get bytes.
((Stream)readStream).CopyTo(memStream);
var stream1 = new MemoryStream(memStream.ToArray());
if (stream1.Length < 30000000)
{
//Save image
//ProductImage = ImageSource.FromStream(() => stream1);
//Dispose readable stream.
readStream.Dispose();
InfoColSpan = 1;
}
else
{
Common.AlertError("Image trop lourde pour l'affichage");
}
}
}
catch (Exception ex)
{
Common.AlertError(ex, "Impossible de charger l'image");
}
Since I changed my method, I no longer load anything.
Im trying to get the image url from a photo I uploaded to firebase storage so I can store the reference to the image in another table which will allow me to be able to display the image elsewhere throughout the app.
I currently have
private void UploadPhoto()
{
if (filePAth != null)
{
progressDialog = new ProgressDialog(this);
progressDialog.SetTitle("Uploading...");
progressDialog.Window.SetType(Android.Views.WindowManagerTypes.SystemAlert);
progressDialog.Show();
var images = storageRef.Child("images/" + Guid.NewGuid().ToString());
images.PutFile(filePAth)
.AddOnProgressListener(this)
.AddOnSuccessListener(this)
.AddOnFailureListener(this);
}
}
public async void OnSuccess(Java.Lang.Object result)
{
try
{
var newImageDetails = storageRef.Child("images" + "/" + filePAth);
Photo photos = new Photo();
photos.categoryName = spinner.SelectedItem.ToString();
photos.photoId = newImageDetails.Name;
photos.ImageUrl = storageRef.DownloadUrl.ToString();
photos.tagName = addTag.Text;
if (user != null)
{
var uid = user.Uid;
//set the users id to the category
photos.uid = uid;
}
var firebase = new FirebaseClient(FirebaseURL);
var item = await firebase.Child("photos").PostAsync(photos);
}
}
The storageref.DownloadUrl does not give me the correct url
This is what I am looking for
I think that the trouble is Guid.NewGuid().
It generates a new code each time you insert a new image and your storageref points to it (image/GUID code).
In your OnSuccess you get image info from "images/" + filePath, that's different from upload path.
Why you use a new GUID? You can't determinate it, it will create a different path each time.
Consider then that your download url contains the media token too, it's not simply the clean path as you expect
I got a solution in debugging mode
i saw the downloadurl's properties and found the Scheme and SchemeSpecificPart
Scheme = "https"
SchemeSpecificPart = "//firebasestorage.googleapis.com/v0/b/maplog-e4ba5.appspot.com/o/-L0AMbihF23YKxsL1uss?alt=media&token=5c7ccef1-c857-4982-a288-fded2f0ff1aa"
so here is my code:
void IOnSuccessListener.OnSuccess(Java.Lang.Object result)
{
var snapShot = (UploadTask.TaskSnapshot)result;
string imgUrl = snapShot.DownloadUrl.Scheme
+ ":"
+ snapShot.DownloadUrl.SchemeSpecificPart;
}
and it works! i was looking for the solution :(( but i finally found it myself XD
I want to read exif info from Photo using ExifLib, first way is finished because I used PhotoChooserTask and photoChooserTask.Completed += (s, e) => {PhotoConverter.GetMetaData(e);}
and method for get exif info
public static void GetMetaDate(PhotoResult e)
{
ExifLib.JpegInfo info = ExifLib.ExifReader.ReadJpeg(e.ChosenPhoto);
var img = new BitmapImage();
img.SetSource(e.ChosenPhoto);
App.MainViewModel.MetaDate = ReadExif(info);
}
private static string ReadExif(JpegInfo info)
{
JsonObject metaDate = new JsonObject();
metaDate.Add("FNumber", info.FNumber);
return metaDate.ToString();
}
and it is work great, but the problem is when I want to share picture from phone's gallery. My way to get picture looks like this
if (queryStrings.ContainsKey("FileId"))
{
MediaLibrary library = new MediaLibrary();
Picture photoFromLibrary = library.GetPictureFromToken(queryStrings["FileId"]);
BitmapImage bitmapFromPhoto = new BitmapImage();
bitmapFromPhoto.SetSource(photoFromLibrary.GetImage());
}
So, how I should change my GetMetaDate to read photoFromLibrary.GetImage
Ok, I find easy way
public static void GetMetaData(Stream photo)//change to stream
{
ExifLib.JpegInfo info = ExifLib.ExifReader.ReadJpeg(photo);
var img = new BitmapImage();
img.SetSource(photo);
App.MainViewModel.MetaDate = ReadExif(info);
}
and in place with MediaLiblary add stream
if (queryStrings.ContainsKey("FileId"))
{
// Retrieve the photo from the media library using the FileID passed to the app.
MediaLibrary library = new MediaLibrary();
Picture photoFromLibrary = library.GetPictureFromToken(queryStrings["FileId"]);
//Get metadate
Stream stream = photoFromLibrary.GetImage();
PhotoConverter.GetMetaData(stream);
}
How can I take a screenshot with Xamarin.iOS and stored this image in UIImage.
I have seen several examples of Obj-C, but no C #
Even more elegant:
UIScreen.MainScreen.Capture ();
from Craig Dunn's site:
public void ScreenCapture()
{
var documentsDirectory = Environment.GetFolderPath
(Environment.SpecialFolder.Personal);
Console.WriteLine("start capture of frame: " + this.View.Frame.Size);
UIGraphics.BeginImageContext(View.Frame.Size);
var ctx = UIGraphics.GetCurrentContext();
if (ctx != null)
{
View.Layer.RenderInContext(ctx);
UIImage img = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
// Set to display in a UIImage control _on_ the view
imageLogo.Image = img;
// Save to Photos
img.SaveToPhotosAlbum(
(sender, args)=>{Console.WriteLine("image saved to Photos");}
);
// Save to application's Documents folder
string png = Path.Combine (documentsDirectory, "Screenshot.png");
// HACK: overwrite the splash screen. iSOFlair is the application name
//string png = Path.Combine (documentsDirectory, "../iSOFlair.app/Default.png");
NSData imgData = img.AsPNG();
NSError err = null;
if (imgData.Save(png, false, out err))
{
Console.WriteLine("saved as " + png);
} else {
Console.WriteLine("NOT saved as" + png +
" because" + err.LocalizedDescription);
}
}
else
{
Console.WriteLine("ctx null - doesn't seem to happen");
}
}
More elegant:
public static class UIViewExtensions {
public static UIImage AsImage(this UIView view) {
UIGraphics.BeginImageContextWithOptions(view.Bounds.Size, view.Opaque, 0);
view.Layer.RenderInContext(UIGraphics.GetCurrentContext());
UIImage img = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return img;
}
public static UIImage TakeScreenshot() {
return UIApplication.SharedApplication.KeyWindow.AsImage();
}
}
Call UIViewExtensions.TakeScreenshot() to take a screenshot of the whole screen or you can call AsImage() to any view to get an UIImage representation of the view. It would be better to put the TakeScreenshot() method somewhere else as it is not an extension of the UIView class.
This code will take a screenshot and save the photo into the camera roll.
UIGraphics.BeginImageContext(UIScreen.MainScreen.Bounds.Size);
try
{
var mainLayer = UIApplication.SharedApplication.KeyWindow.Subviews[0].Layer;
mainLayer.RenderInContext(UIGraphics.GetCurrentContext());
var img = UIGraphics.GetImageFromCurrentImageContext();
img.SaveToPhotosAlbum((iRef, status) =>
{
if (status != null)
{
new UIAlertView("Problem", status.ToString(), null, "OK", null).Show();
}
else
{
new UIAlertView("Saved", "Saved", null, "OK", null).Show();
}
});
}
finally
{
UIGraphics.EndImageContext();
}
But to make sure your app didn't crash is to ask the user for the permission to save the images to the camera roll.
Add this into info.plist
<key>NSPhotoLibraryAddUsageDescription</key>
<string>This app requires read and write permission from the user.</string>
if you're adding it from the generic editor then "Privacy - Photo Library Additions Usage Description" will be the given option you will find out instead of "NSPhotoLibraryAddUsageDescription".
I want to load the image like this:
void info(string channel)
{
//Something like that
channelPic.Image = Properties.Resources.+channel
}
Because I don't want to do
void info(string channel)
{
switch(channel)
{
case "chan1":
channelPic.Image = Properties.Resources.chan1;
break;
case "chan2":
channelPic.Image = Properties.Resources.chan2;
break;
}
}
Is something like this possible?
You can always use System.Resources.ResourceManager which returns the cached ResourceManager used by this class. Since chan1 and chan2 represent two different images, you may use System.Resources.ResourceManager.GetObject(string name) which returns an object matching your input with the project resources
Example
object O = Resources.ResourceManager.GetObject("chan1"); //Return an object from the image chan1.png in the project
channelPic.Image = (Image)O; //Set the Image property of channelPic to the returned object as Image
Notice: Resources.ResourceManager.GetObject(string name) may return null if the string specified was not found in the project resources.
Thanks,
I hope you find this helpful :)
You can do this using the ResourceManager:
public bool info(string channel)
{
object o = Properties.Resources.ResourceManager.GetObject(channel);
if (o is Image)
{
channelPic.Image = o as Image;
return true;
}
return false;
}
Try this for WPF
StreamResourceInfo sri = Application.GetResourceStream(new Uri("pack://application:,,,/WpfGifImage001;Component/Images/Progess_Green.gif"));
picBox1.Image = System.Drawing.Image.FromStream(sri.Stream);
ResourceManager will work if your image is in a resource file. If it is just a file in your project (let's say the root) you can get it using something like this:
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = assembly .GetManifestResourceStream("AssemblyName." + channel);
this.pictureBox1.Image = Image.FromStream(file);
Or if you're in WPF:
private ImageSource GetImage(string channel)
{
StreamResourceInfo sri = Application.GetResourceStream(new Uri("/TestApp;component/" + channel, UriKind.Relative));
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = sri.Stream;
bmp.EndInit();
return bmp;
}
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(444, 25);
this.toolStrip1.TabIndex = 0;
this.toolStrip1.Text = "toolStrip1";
object O = global::WindowsFormsApplication1.Properties.Resources.ResourceManager.GetObject("best_robust_ghost");
ToolStripButton btn = new ToolStripButton("m1");
btn.DisplayStyle = ToolStripItemDisplayStyle.Image;
btn.Image = (Image)O;
this.toolStrip1.Items.Add(btn);
this.Controls.Add(this.toolStrip1);
You can add an image resource in the project then (right click on the project and choose the Properties item) access that in this way:
this.picturebox.image = projectname.properties.resources.imagename;