Getting NotSupportedException when trying to set image from isolated storage - c#

I'm trying to set image for my tile in the background agent for my application:
ShellTile t = ShellTile.ActiveTiles.First();
if (t != null)
{
var filePath = Path.Combine("Tiles", "test1.jpg");
StandardTileData tile = new StandardTileData();
tile.Title = "Title text here";
tile.BackgroundImage = new Uri(#"isostore:\" + filePath, UriKind.Absolute);
t.Update(tile);
}
but then on t.Update(tile) it throws NotSupportedException :-( Isnt the path ("isostore:\") correct?

new Uri(#"isostore:" + filePath, UriKind.Absolute);
Without the backslash.

Related

how to get image name within a base directory(Application Directory) in wpf C#

In my base directory I have a folder for image. In this folder I have only one image and I want to get image name from it and load it to Image Source. In this code I get an error:
Could not find file'C:\Users\Santhosh\Documents\Visual Studio
2012\imagetest\imagetest\bin\Debug\ImageFolder\System.Linq.Enumerable+WhereSelectArrayIterator`2[System.String,System.String].jpg'
How can I get an image name from base directory?
This is my code
string ImageFiles=Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory+"\\ImageFolder\\",".jpg").Select(System.IO.Path.GetFileName).ToString();
image1.source== new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + "\\ImageFolder\\"+ImageFiles+".jpg"));
Call FirstOrDefault to get the first file from the directory, or null if the directory does not contain a file with a matching name:
var imageFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ImageFolder");
var firstFile = Directory.EnumerateFiles(imageFolder, "*.jpg")
.Select(Path.GetFileName)
.FirstOrDefault();
if (firstFile != null)
{
image.Source = new BitmapImage(new Uri(Path.Combine(imageFolder, firstFile)));
}
Or shorter, without GetFileName and a subsequent Combine:
var imageFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ImageFolder");
var firstFile = Directory.EnumerateFiles(imageFolder, "*.jpg")
.FirstOrDefault();
if (firstFile != null)
{
image.Source = new BitmapImage(new Uri(firstFile));
}
This is how youre first line should look like :
string ImageFiles = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "\\ImageFolder\\").Select(System.IO.Path.GetFileName).First();

Set Image.Source from photo in camera roll

In my Windows Phone 8.1 App i take a photo with the camera and save this in the camera roll and save the image path in a temporary object:
var picture = library.SavePictureToCameraRoll(fileName, e.ImageStream);
geophoto.ImagePath = picture.GetPath();
In another page of my app i want to load this photo from the camera roll and set the saved path as the source of an Image object:
Uri uri = new Uri(App.Current.Geophoto.ImagePath, UriKind.Absolute);
ImageSource imgSource = new BitmapImage(uri);
this.ShutterImage.Source = imgSource;
The saved path of the image is e.g. "file:///C:/Data/Users/Public/Pictures/Camera Roll/201506191442443805.jpg"
In runtime the image goes blank when i try to set a new source. Is there something wrong with the path or with the code?
I figured out that i have no direct access to camera roll through the path. So i solved my problem with the following code:
private BitmapImage GetThumbnailFromCameraRoll(string path)
{
MediaLibrary mediaLibrary = new MediaLibrary();
var pictures = mediaLibrary.Pictures;
foreach (var picture in pictures)
{
var camerarollPath = picture.GetPath();
if (camerarollPath == path)
{
BitmapImage image = new BitmapImage();
image.SetSource(picture.GetThumbnail());
return image;
}
}
return null;
}

Fast way to check is image exists in assets in Windows Phone

My application for Windows Phone 8.1.
I need to find a way to check image aviability in assets resources in my application.
At first, I had following solution:
var package = Windows.ApplicationModel.Package.Current.InstalledLocation;
var folder = await package.GetFolderAsync("Assets\\Makes");
var files = await folder.GetFilesAsync();
var result = files.FirstOrDefault(p => p.Name == imageName);
if (result != null)
{
Uri imageUri = new Uri("ms-appx:///Assets/Makes/" + imageName);
Image img = new Image();
BitmapImage bi = new BitmapImage(imageUri);
img.Source = bi;
btn.Content = img;
}
else
{
TextBlock label = new TextBlock();
label.Text = text;
btn.Content = label;
}
It works. But, unfortunately, very very slow.
Anyway, next part of code working even in case if asset is not existing:
Uri imageUri = new Uri("ms-appx:///Assets/Makes/" + imageName);
BitmapImage bi = new BitmapImage(imageUri);
In case if file not existing, the image is empty, but not null.
Is there are any good way, to check, if image created empty from resource?
Or a really fast way to check existing of packaged resource file?
Thank you

Not getting image from Persistant path in Android using unity3d

I have a script to take screenshot and save that image and to show that image in a plane.
But not getting that texture.
private string path;
void Start () {
path = Application.persistentDataPath;
}
void captureScreeshot(){
Application.CaptureScreenshot(path + "/Screenshot.png");
alert.text = "saved";
StartCoroutine(saveScreenshot());
onetimeBool = false;
}
IEnumerator saveScreenshot(){
WWW imgfile = new WWW("file://" + path+ "/Screenshot.png");
yield return imgfile;
if(imgfile.error==null){
img = new Texture2D(200, 200, TextureFormat.RGB24, false);
imgfile.LoadImageIntoTexture(img);
alert.text = "got image";
GameObject.Find("plane").renderer.material.mainTexture = img;
}else{
print(imgfile.error);
}
}
Images gets saved.
But I cannot open (load) image in system and android.
Getting error in system :
Couldn't open file /Users/ether/AppData/LocalLow/Company/Project/Screenshot.png
How to get it ???
This is because your path is wrong, you should use: application.dataPath, instead of Application.persistentDataPath.

how to close a file and delete that image file?

Imagename = objUser.UserID + filename;
Imagepath = "D:\\Shop\\ShopMonkey\\Images" + Imagename;
FileUpload.SaveAs(Path.Combine(#"D:\ShopMonkey_Web_21-6-12\ShopMonkey\Images", Imagename));
objUser.UploadImagePath = Imagepath;
objUser.UploadImagename = Imagename;
System.Drawing.Image img1 = System.Drawing.Image.FromFile(Imagepath);
System.Drawing.Image bmp1 = img1.GetThumbnailImage(50, 50, null, IntPtr.Zero);
ThumbNailPath = "D:\\ShopMonkey_Web_21-6-12\\ShopMonkey\\ThumbNails" + Imagename;
bmp1.Save(Path.Combine(#"D:\ShopMonkey_Web_21-6-12\ShopMonkey\ThumbNails", Imagename));
objUser.UploadThumbnailPath = ThumbNailPath;
How to delete the image and thumbnail in other function? (Is it necessary to close that first or not?)
I'm guessing that you're trying to delete a file on disk if it already exists for this user upon asp.net upload of an image by that same user again.
This method will delete both image and thumbnail if they exist.
Keep your image creation activities separate from your image clean up activities to keep your intent clear and your code maintainable.
// replace with an entry loaded from a config file
const string ImageRoot = #"D:\ShopMonkey_Web_21-6-12\ShopMonkey";
// replace this is your user instance
object user = new object();
string Imagename = objUser.UserID + filename;
string uploadImagePath = Path.Combine(ImageRoot, "Images", Imagename);
string thumbnailPath = Path.Combine(ImageRoot, "ThumbNails", Imagename);
objUser.UploadImagePath = uploadImagePath;
objUser.UploadImagename = Imagename;
objUser.UploadThumbnailPath = thumbnailPath;
// delete both if they exist
if (File.Exists(uploadImagePath))
File.Delete(uploadImagePath);
if (File.Exists(thumbnailPath))
File.Delete(thumbnailPath);
// replace this with your uploaded file details
object fileInfo = new object();
using (System.Drawing.Image img1 = System.Drawing.Image.FromFile(uploadImagePath)) {
img1.Save(uploadImagePath);
using (System.Drawing.Image bmp1 = img1.GetThumbnailImage(50, 50, null, IntPtr.Zero)) {
bmp1.Save(thumbnailPath);
}
FileUpload.SaveAs(uploadImagePath);
}

Categories